Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › JAva assignment (Koch Curves)
New Posts  All Forums:Forum Nav:

JAva assignment (Koch Curves)

post #1 of 7
Thread Starter 

Hi,

 

I'm having a lot of trouble trying to find a recursive function to make Koch Curves. Earlier today I ended up causing a couple of stack overflows and now my ouput sucks to the point I'm lost to what's actually happening.

 

Here's the code:

Full Code (Click to show)

 

 

import java.awt.Color;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Point2D;
import java.util.ArrayList;
 
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
 
import com.sun.opengl.util.Animator;
 
/**
 * A frame for us to draw on using OpenGL.
 *  
 * @author Kang
 *  
 */
public class JOGLFrame extends Frame implements GLEventListener, MouseListener {
    static final long serialVersionUID = 7526471155622776147L;
 
    // Screen size.
    private int screenWidth = 800, screenHeight = 600;
    private float buttonSize = screenHeight / 10.0f;
 
    // A GLCanvas is a component that can be added to a frame. The drawing
    // happens on this component.
    private GLCanvas canvas;
 
    private static final byte DM_POINT = 0;
    private static final byte DM_LINE = 1;
    private static final byte DM_KOCH = 2;
    private byte drawMode = DM_POINT;
 
    private ArrayList<Point2D.Float> points;
 
    /**
     * When instantiating, a GLCanvas is added for us to play with. An animator
     * is created to continuously render the canvas.
     */
    public JOGLFrame() {
        super("IN2710-B Assignment 1");
 
        points = new ArrayList<Point2D.Float>();
 
        // Set the desired size and background color of the frame
        setSize(screenWidth, screenHeight);
        //setBackground(Color.white);
        setBackground(new Color(0.95f, 0.95f, 0.95f));
 
        // When the "X" close button is called, the application should exit.
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
 
        // The OpenGL capabilities should be set before initializing the
        // GLCanvas. We use double buffering and hardware acceleration.
        GLCapabilities caps = new GLCapabilities();
        caps.setDoubleBuffered(true);
        caps.setHardwareAccelerated(true);
 
        // Create a GLCanvas with the specified capabilities and add it to this
        // frame. Now, we have a canvas to draw on using JOGL.
        canvas = new GLCanvas(caps);
        add(canvas);
 
        // Set the canvas' GL event listener to be this class. Doing so gives
        // this class control over what is rendered on the GL canvas.
        canvas.addGLEventListener(this);
 
        // Also add this class as mouse listener, allowing this class to react
        // to mouse events that happen inside the GLCanvas.
        canvas.addMouseListener(this);
 
        // An Animator is a JOGL help class that can be used to make sure our
        // GLCanvas is continuously being re-rendered. The animator is run on a
        // separate thread from the main thread.
        Animator anim = new Animator(canvas);
        anim.start();
 
        // With everything set up, the frame can now be displayed to the user.
        setVisible(true);
    }
 
     @Override
    /**
     * A function defined in GLEventListener. It is called once, when the frame containing the GLCanvas  
     * becomes visible. In this assignment, there is no moving �camera�, so the view and projection can  
     * be set at initialization.  
     */
    public void init(GLAutoDrawable drawable) {
        // Retrieve the OpenGL handle, this allows us to use OpenGL calls.
        GL gl = drawable.getGL();
 
        // Set the matrix mode to GL_PROJECTION, allowing us to manipulate the
        // projection matrix
        gl.glMatrixMode(GL.GL_PROJECTION);
 
        // Always reset the matrix before performing transformations, otherwise
        // those transformations will stack with previous transformations!
        gl.glLoadIdentity();
 
        /*
         * glOrtho performs an "orthogonal projection" transformation on the
         * active matrix. In this case, a simple 2D projection is performed,
         * matching the viewing frustum to the screen size.
         */
        gl.glOrtho(0, screenWidth, 0, screenHeight, -1, 1);
 
        // Set the matrix mode to GL_MODELVIEW, allowing us to manipulate the
        // model-view matrix.
        gl.glMatrixMode(GL.GL_MODELVIEW);
 
        // We leave the model view matrix as the identity matrix. As a result,
        // we view the world 'looking forward' from the origin.
        gl.glLoadIdentity();
 
        // We have a simple 2D application, so we do not need to check for depth
        // when rendering.
        gl.glDisable(GL.GL_DEPTH_TEST);
    }
 
    @Override
    /**
     * A function defined in GLEventListener. This function is called many times per second and should  
     * contain the rendering code.
     */
    public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
 
        // Set the clear color and clear the screen.
        gl.glClearColor(0.95f, 0.95f, 0.95f, 1);
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
 
        // Draw the buttons.
        drawButtons(gl);
 
        // Draw a figure based on the current draw mode and user input
        drawFigure(gl);
 
        // Flush the OpenGL buffer, outputting the result to the screen.
        gl.glFlush();
    }
 
    /**
     * A method that draws the top left buttons on the screen.
     *  
     * @param gl
     */
    private void drawButtons(GL gl) {
        // Draw the background boxes
        gl.glColor3f(0, 0.5f, 0f);
        boxOnScreen(gl, 0.0f, screenHeight - buttonSize, buttonSize);
 
        gl.glColor3f(0, 0, 0.5f);
        boxOnScreen(gl, buttonSize, screenHeight - buttonSize, buttonSize);
        
        gl.glColor3f(0.5f, 0, 0);
        boxOnScreen(gl, 2*buttonSize, screenHeight - buttonSize, buttonSize);
 
        // Draw a point on top of the first box
        gl.glPointSize(5.0f);
        gl.glColor3f(1.0f, 1.0f, 1.0f);
        pointOnScreen(gl, buttonSize / 2.0f, screenHeight - buttonSize / 2.0f);
 
        // Draw a line on top of the second box
        gl.glLineWidth(3);
        gl.glColor3f(1.0f, 1.0f, 1.0f);
        lineOnScreen(gl, buttonSize + 4.0f, screenHeight - 4.0f,
                2 * buttonSize - 4.0f, screenHeight - buttonSize + 4.0f);
        
        //Draw a triangle on top of the third box
        gl.glLineWidth(2.0f);
        gl.glColor3f(1.0f, 1.0f, 1.0f);
        lineOnScreen(gl, 2 * buttonSize + 4.0f, screenHeight - buttonSize + 4.0f, 2.5f * buttonSize,
                screenHeight - 4.0f);
        lineOnScreen(gl, 2.5f * buttonSize, screenHeight - 4.0f, 3 * buttonSize - 4.0f,
                screenHeight - buttonSize + 4.0f);
        lineOnScreen(gl, 3 * buttonSize - 4.0f, screenHeight - buttonSize + 4.0f, 2 * buttonSize + 4.0f, screenHeight - buttonSize + 4.0f);
        
    }
 
    /**
     * A method that draws a figure, when the user has inputted enough points
     * for the current draw mode.
     *  
     * @param gl
     */
    private void drawFigure(GL gl) {
        // Set line and point size, and set color to black.
        gl.glLineWidth(3);
        gl.glPointSize(10.0f);
        gl.glColor3f(0.0f, 0.0f, 0.0f);
 
        Point2D.Float p1, p2, p3;
        switch (drawMode) {
        case DM_POINT:
            if (points.size() >= 1) {
                // If the draw mode is "point" and the user has supplied at
                // least one point, draw that point.
                p1 = points.get(0);
                pointOnScreen(gl, p1.x, p1.y);
            }
        case DM_LINE:
            if (points.size() >= 2) {
                // If the draw mode is "line" and the user has supplied at least
                // two points, draw a line between those points
                p1 = points.get(0);
                p2 = points.get(1);
                lineOnScreen(gl, p1.x, p1.y, p2.x, p2.y);
            }
        case DM_KOCH:
            if (points.size() >= 3) {
                // If draw mode is "Koch" and the user has supplied at least
                // two points, draw lines between these points
                p1 = points.get(0);
                p2 = points.get(1);
                p3 = points.get(2);
                int n = 1;
                
 
                drawKoch(gl, p1.x, p1.y, p2.x, p2.y, n);
                drawKoch(gl, p2.x, p2.y, p3.x, p3.y, n);
                drawKoch(gl, p3.x, p3.y, p1.x, p1.y, n);
                
            }
            break;
        }
    }
 
    /**
     * Help method that uses GL calls to draw a point.
     */
    private void pointOnScreen(GL gl, float x, float y) {
        gl.glBegin(GL.GL_POINTS);
        gl.glVertex2f(x, y);
        gl.glEnd();
    }
 
    /**
     * Help method that uses GL calls to draw a line.
     */
    private void lineOnScreen(GL gl, float x1, float y1, float x2, float y2) {
        gl.glBegin(GL.GL_LINES);
        gl.glVertex2f(x1, y1);
        gl.glVertex2f(x2, y2);
        gl.glEnd();
    }
    
    private void drawKoch(GL gl, float x1, float y1, float x2, float y2, int n) {
        if (n == 0) {
            gl.glBegin(GL.GL_LINES);
            gl.glVertex2f(x1, y1);
            gl.glVertex2f(x2, y2);
            gl.glEnd();
        }
        else if (n > 0){
            
            float a_x = x1, a_y = y1;
            float b_x = (2.0f/3.0f)*(x1 - x2), b_y = (2.0f/3.0f)*(y1 - y2);
            float c_x = (1.0f/2.0f)*(-(float)Math.sqrt(3.0f)/6.0f)*(y2 - y1), c_y = (1.0f/2.0f)*(y1 + y2) + (float)(Math.sqrt(3.0f)/6.0f) * (x2 - x1);
            float d_x = (1.0f/3.0f)*(x1 + 2.0f * x2), d_y = (1.0f/3.0f) * (y1 + 2.0f * y2);
            float e_x = x2, e_y = y2;
            
            gl.glBegin(GL.GL_LINES);
            gl.glVertex2f(a_x, a_y);
            gl.glVertex2f(b_x, b_y);
            gl.glEnd();
            
            gl.glBegin(GL.GL_LINES);
            gl.glVertex2f(b_x, b_y);
            gl.glVertex2f(c_x, c_y);
            gl.glEnd();
            
            gl.glBegin(GL.GL_LINES);
            gl.glVertex2f(c_x, c_y);
            gl.glVertex2f(d_x, d_y);
            gl.glEnd();
            
            gl.glBegin(GL.GL_LINES);
            gl.glVertex2f(d_x, d_y);
            gl.glVertex2f(e_x, e_y);
            gl.glEnd();    
                        
            --n;
            
            drawKoch(gl, a_x, a_y, b_x, b_y, n);
            drawKoch(gl, b_x, b_y, c_x, c_y, n);
            drawKoch(gl, c_x, c_y, d_x, d_y, n);
            drawKoch(gl, d_x, d_y, e_x, e_y, n);
                        
        }
 
            
    }
 
    /**
     * Help method that uses GL calls to draw a square
     */
    private void boxOnScreen(GL gl, float x, float y, float size) {
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(x, y);
        gl.glVertex2f(x + size, y);
        gl.glVertex2f(x + size, y + size);
        gl.glVertex2f(x, y + size);
        gl.glEnd();
    }
 
    @Override
    /**
     * A function defined in GLEventListener. This function is called when there is a change in certain  
     * external display settings.  
     */
    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
            boolean deviceChanged) {
        // Not needed.
    }
 
    @Override
    /**
     * A function defined in GLEventListener. This function is called when the GLCanvas is resized or moved.  
     * Since the canvas fills the frame, this event also triggers whenever the frame is resized or moved.
     */
    public void reshape(GLAutoDrawable drawable, int x, int y, int width,
            int height) {
        GL gl = drawable.getGL();
 
        // Set the new screen size and adjusting the viewport
        screenWidth = width;
        screenHeight = height;
        buttonSize = height / 10.0f;
        gl.glViewport(0, 0, screenWidth, screenHeight);
 
        // Update the projection to an orthogonal projection using the new screen size
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrtho(0, screenWidth, 0, screenHeight, -1, 1);
    }
 
    @Override
    /**
     * A function defined in MouseListener. Is called when the pointer is in the GLCanvas, and a mouse button is released.
     */
    public void mouseReleased(MouseEvent me) {
        // Check if the coordinates correspond to any of the top left buttons
        boolean buttonPressed = false;
        if (me.getY() < buttonSize) {
            if (me.getX() < buttonSize) {
                // The first button is clicked
                points.clear();
                drawMode = DM_POINT;
                System.out.println("Draw mode: DRAW_POINT");
                buttonPressed = true;
            } else if (me.getX() < 2 * buttonSize) {
                // The second button is clicked
                points.clear();
                drawMode = DM_LINE;
                System.out.println("Draw mode: DRAW_LINE");
                buttonPressed = true;
            } else if (me.getX() < 3 * buttonSize) {
                // The third button is clicked
                points.clear();
                drawMode = DM_KOCH;
                System.out.println("Draw mode: DRAW_KOCH");
                buttonPressed = true;
            }
        }
 
        // Only register a new point, if the click did not hit any button
        if (!buttonPressed) {
 
            if (drawMode == DM_POINT && points.size() >= 1) {
                // If we're drawing points and one point was stored, reset the points list
                points.clear();
            } else if (drawMode == DM_LINE && points.size() >= 2) {
                // If we're drawing lines and two points were already stored, reset the points list
                points.clear();
            } else if (drawMode == DM_KOCH && points.size() >= 3) {
                // If we're drawing Koch curves and three points were already stored, reset the points list
                points.clear();
            }
 
            // Add a new point to the points list.
            points.add(new Point2D.Float(me.getX(), screenHeight - me.getY()));
            System.out.println(me.getX() + " " + (screenHeight - me.getY()));
        }
    }
    
    @Override
    public void mouseClicked(MouseEvent arg0) {
        // Not needed.
    }
 
    @Override
    public void mouseEntered(MouseEvent arg0) {
        // Not needed.
 
    }
 
    @Override
    public void mouseExited(MouseEvent arg0) {
        // Not needed.
 
    }
 
    @Override
    public void mousePressed(MouseEvent arg0) {
        // Not needed.
 
    }
}

 

 

And here's the function for drawing the Koch Curve (Is being called 3 times in the program):

 

private void drawKoch(GL gl, float x1, float y1, float x2, float y2, int n) {
        if (n == 0) {
            gl.glBegin(GL.GL_LINES);
            gl.glVertex2f(x1, y1);
            gl.glVertex2f(x2, y2);
            gl.glEnd();
        }
        else if (n > 0){
            
            float a_x = x1, a_y = y1;
            float b_x = (2.0f/3.0f)*(x1 - x2), b_y = (2.0f/3.0f)*(y1 - y2);
            float c_x = (1.0f/2.0f)*(-(float)Math.sqrt(3.0f)/6.0f)*(y2 - y1), c_y = (1.0f/2.0f)*(y1 + y2) + (float)(Math.sqrt(3.0f)/6.0f) * (x2 - x1);
            float d_x = (1.0f/3.0f)*(x1 + 2.0f * x2), d_y = (1.0f/3.0f) * (y1 + 2.0f * y2);
            float e_x = x2, e_y = y2;
            
            gl.glBegin(GL.GL_LINES);
            gl.glVertex2f(a_x, a_y);
            gl.glVertex2f(b_x, b_y);
            gl.glEnd();
            
            gl.glBegin(GL.GL_LINES);
            gl.glVertex2f(b_x, b_y);
            gl.glVertex2f(c_x, c_y);
            gl.glEnd();
            
            gl.glBegin(GL.GL_LINES);
            gl.glVertex2f(c_x, c_y);
            gl.glVertex2f(d_x, d_y);
            gl.glEnd();
            
            gl.glBegin(GL.GL_LINES);
            gl.glVertex2f(d_x, d_y);
            gl.glVertex2f(e_x, e_y);
            gl.glEnd();    
                        
            --n;
            
            drawKoch(gl, a_x, a_y, b_x, b_y, n);
            drawKoch(gl, b_x, b_y, c_x, c_y, n);
            drawKoch(gl, c_x, c_y, d_x, d_y, n);
            drawKoch(gl, d_x, d_y, e_x, e_y, n);
                        
        }

            
    }

 

And here's the output I get:

 

 

 

I can specify if you guys need more information but I'm really tired after working so long on this program...

Pernod
(28 items)
 
Oleo
(14 items)
 
The Sidewinder
(18 items)
 
CPUCPUMotherboardGraphics
Intel Xeon E5-4650 Intel Xeon E5-4650 Asus Z9PE-D8 WS GeForce GTX 780 Ti 
RAMRAMHard DriveHard Drive
4 x 4GB Corsair Dominator Platinum 4 x 4GB Corsair Dominator Platinum Samsung Spinpoint F3 2TB Sandisk Ultra 64GB 
Hard DriveCoolingCoolingCooling
mushkin Triactor 240GB EK Supremacy Nickel/Plexi EK Supremacy Nickel/Plexi TFC XChanger 360 
CoolingCoolingCoolingOS
Koolance RP452X2 Swiftech MCP-655 Corsair SP120 "QE" Windows 10 Enterprise x64 
OSMonitorMonitorMonitor
openSUSE Leap 42.1 x64 Dell U2412M Dell U2412M Acer P241W-d 
KeyboardPowerCaseMouse
Ducky 1008 w/ Cherry MX Blue Coolermaster Silent Pro M2 1.5kW Silverstone Raven RV01B-W Microsoft Sidewinder X5 
AudioAudioAudioOther
Asus Xonar DX Beyerdynamic DT 880 Premium AudioEngine A2 (2.0) Wacom Intuos Pen & Touch 
CPUMotherboardGraphicsRAM
Core i7 2600K @5.2  MSI MPower Z77 GeForce GTX 470 Corsair Vengeance 2000MHz C10 
Hard DriveCoolingCoolingCooling
Samsung Spinpoint F1 XSPC Raystorm EK Spin Bay Res Clear mcp-350 (ddc1) 
CoolingCoolingOSOS
XSPC EX120 XSPC EX240 openSUSE 12.3 x64 elementaryOS 0.3 (Freya) x64 
PowerCase
Corsair TX750 750W Lian Li A05NB 
CPUMotherboardGraphicsRAM
Intel C2Q Q9550 Asus P5G41C-M LX GeForce GTX 470 Corsair XMS2 DDR2 800 
Hard DriveHard DriveHard DriveHard Drive
Seagate LP 500GB Crucial M4 128GB WD Red 2TB WD Red 4TB 
CoolingOSMonitorPower
Scythe Big Shuriken w/ Silverstone Suscool 121 Windows 10 Home x64 Sony Bravia KDL-37EX500 Antec TP650 New 
CaseOther
Lian Li V351B Silverstone Suscool 121 120mm case fans 
  hide details  
Reply
Pernod
(28 items)
 
Oleo
(14 items)
 
The Sidewinder
(18 items)
 
CPUCPUMotherboardGraphics
Intel Xeon E5-4650 Intel Xeon E5-4650 Asus Z9PE-D8 WS GeForce GTX 780 Ti 
RAMRAMHard DriveHard Drive
4 x 4GB Corsair Dominator Platinum 4 x 4GB Corsair Dominator Platinum Samsung Spinpoint F3 2TB Sandisk Ultra 64GB 
Hard DriveCoolingCoolingCooling
mushkin Triactor 240GB EK Supremacy Nickel/Plexi EK Supremacy Nickel/Plexi TFC XChanger 360 
CoolingCoolingCoolingOS
Koolance RP452X2 Swiftech MCP-655 Corsair SP120 "QE" Windows 10 Enterprise x64 
OSMonitorMonitorMonitor
openSUSE Leap 42.1 x64 Dell U2412M Dell U2412M Acer P241W-d 
KeyboardPowerCaseMouse
Ducky 1008 w/ Cherry MX Blue Coolermaster Silent Pro M2 1.5kW Silverstone Raven RV01B-W Microsoft Sidewinder X5 
AudioAudioAudioOther
Asus Xonar DX Beyerdynamic DT 880 Premium AudioEngine A2 (2.0) Wacom Intuos Pen & Touch 
CPUMotherboardGraphicsRAM
Core i7 2600K @5.2  MSI MPower Z77 GeForce GTX 470 Corsair Vengeance 2000MHz C10 
Hard DriveCoolingCoolingCooling
Samsung Spinpoint F1 XSPC Raystorm EK Spin Bay Res Clear mcp-350 (ddc1) 
CoolingCoolingOSOS
XSPC EX120 XSPC EX240 openSUSE 12.3 x64 elementaryOS 0.3 (Freya) x64 
PowerCase
Corsair TX750 750W Lian Li A05NB 
CPUMotherboardGraphicsRAM
Intel C2Q Q9550 Asus P5G41C-M LX GeForce GTX 470 Corsair XMS2 DDR2 800 
Hard DriveHard DriveHard DriveHard Drive
Seagate LP 500GB Crucial M4 128GB WD Red 2TB WD Red 4TB 
CoolingOSMonitorPower
Scythe Big Shuriken w/ Silverstone Suscool 121 Windows 10 Home x64 Sony Bravia KDL-37EX500 Antec TP650 New 
CaseOther
Lian Li V351B Silverstone Suscool 121 120mm case fans 
  hide details  
Reply
post #2 of 7
Thread Starter 
Anyone?
Pernod
(28 items)
 
Oleo
(14 items)
 
The Sidewinder
(18 items)
 
CPUCPUMotherboardGraphics
Intel Xeon E5-4650 Intel Xeon E5-4650 Asus Z9PE-D8 WS GeForce GTX 780 Ti 
RAMRAMHard DriveHard Drive
4 x 4GB Corsair Dominator Platinum 4 x 4GB Corsair Dominator Platinum Samsung Spinpoint F3 2TB Sandisk Ultra 64GB 
Hard DriveCoolingCoolingCooling
mushkin Triactor 240GB EK Supremacy Nickel/Plexi EK Supremacy Nickel/Plexi TFC XChanger 360 
CoolingCoolingCoolingOS
Koolance RP452X2 Swiftech MCP-655 Corsair SP120 "QE" Windows 10 Enterprise x64 
OSMonitorMonitorMonitor
openSUSE Leap 42.1 x64 Dell U2412M Dell U2412M Acer P241W-d 
KeyboardPowerCaseMouse
Ducky 1008 w/ Cherry MX Blue Coolermaster Silent Pro M2 1.5kW Silverstone Raven RV01B-W Microsoft Sidewinder X5 
AudioAudioAudioOther
Asus Xonar DX Beyerdynamic DT 880 Premium AudioEngine A2 (2.0) Wacom Intuos Pen & Touch 
CPUMotherboardGraphicsRAM
Core i7 2600K @5.2  MSI MPower Z77 GeForce GTX 470 Corsair Vengeance 2000MHz C10 
Hard DriveCoolingCoolingCooling
Samsung Spinpoint F1 XSPC Raystorm EK Spin Bay Res Clear mcp-350 (ddc1) 
CoolingCoolingOSOS
XSPC EX120 XSPC EX240 openSUSE 12.3 x64 elementaryOS 0.3 (Freya) x64 
PowerCase
Corsair TX750 750W Lian Li A05NB 
CPUMotherboardGraphicsRAM
Intel C2Q Q9550 Asus P5G41C-M LX GeForce GTX 470 Corsair XMS2 DDR2 800 
Hard DriveHard DriveHard DriveHard Drive
Seagate LP 500GB Crucial M4 128GB WD Red 2TB WD Red 4TB 
CoolingOSMonitorPower
Scythe Big Shuriken w/ Silverstone Suscool 121 Windows 10 Home x64 Sony Bravia KDL-37EX500 Antec TP650 New 
CaseOther
Lian Li V351B Silverstone Suscool 121 120mm case fans 
  hide details  
Reply
Pernod
(28 items)
 
Oleo
(14 items)
 
The Sidewinder
(18 items)
 
CPUCPUMotherboardGraphics
Intel Xeon E5-4650 Intel Xeon E5-4650 Asus Z9PE-D8 WS GeForce GTX 780 Ti 
RAMRAMHard DriveHard Drive
4 x 4GB Corsair Dominator Platinum 4 x 4GB Corsair Dominator Platinum Samsung Spinpoint F3 2TB Sandisk Ultra 64GB 
Hard DriveCoolingCoolingCooling
mushkin Triactor 240GB EK Supremacy Nickel/Plexi EK Supremacy Nickel/Plexi TFC XChanger 360 
CoolingCoolingCoolingOS
Koolance RP452X2 Swiftech MCP-655 Corsair SP120 "QE" Windows 10 Enterprise x64 
OSMonitorMonitorMonitor
openSUSE Leap 42.1 x64 Dell U2412M Dell U2412M Acer P241W-d 
KeyboardPowerCaseMouse
Ducky 1008 w/ Cherry MX Blue Coolermaster Silent Pro M2 1.5kW Silverstone Raven RV01B-W Microsoft Sidewinder X5 
AudioAudioAudioOther
Asus Xonar DX Beyerdynamic DT 880 Premium AudioEngine A2 (2.0) Wacom Intuos Pen & Touch 
CPUMotherboardGraphicsRAM
Core i7 2600K @5.2  MSI MPower Z77 GeForce GTX 470 Corsair Vengeance 2000MHz C10 
Hard DriveCoolingCoolingCooling
Samsung Spinpoint F1 XSPC Raystorm EK Spin Bay Res Clear mcp-350 (ddc1) 
CoolingCoolingOSOS
XSPC EX120 XSPC EX240 openSUSE 12.3 x64 elementaryOS 0.3 (Freya) x64 
PowerCase
Corsair TX750 750W Lian Li A05NB 
CPUMotherboardGraphicsRAM
Intel C2Q Q9550 Asus P5G41C-M LX GeForce GTX 470 Corsair XMS2 DDR2 800 
Hard DriveHard DriveHard DriveHard Drive
Seagate LP 500GB Crucial M4 128GB WD Red 2TB WD Red 4TB 
CoolingOSMonitorPower
Scythe Big Shuriken w/ Silverstone Suscool 121 Windows 10 Home x64 Sony Bravia KDL-37EX500 Antec TP650 New 
CaseOther
Lian Li V351B Silverstone Suscool 121 120mm case fans 
  hide details  
Reply
post #3 of 7
On it:

Give me a moment.



Okay:
So just tracing through your code here with Processing, this is the current output for

p1: (300, 300)
p2: (500, 300)

in a window of dimensions (800 x 600)


A: 300.0 , 300.0
B: -133.33334 , 0.0
C: -0.0 , 357.73502
D: 433.33334 , 300.0
E: 500.0 , 300.0

So, you can see that points (a), (d), and (e) are all correct, but (b) and (c) are not.

I'm checking those lines now.
Once those two points are calculated correctly, the GL code for drawing the four lines can be removed and the algorithm will function with solely the base case.

ie:
Code:
// -- Method take in two points and attempts to recursively call
// -- itself 4 times in the Koch pattern by transforming the two
// -- points into 4 until iteration 0 is reached.

private void drawKoch(GL gl, float x1, float y1, float x2, float y2, int n) {
        if (n == 0) { //draw the base case (a line)
            gl.glBegin(GL.GL_LINES);
            gl.glVertex2f(x1, y1);
            gl.glVertex2f(x2, y2);
            gl.glEnd();
        }
        else if (n > 0){
            
            //calculate a_x, a_y;
            //calculate b_x, b_y;
            //calculate c_x, c_y;
            //calculate d_x, d_y;
            //calculate e_x, e_y;
            
            --n;
                drawKoch(gl, a_x, a_y, b_x, b_y, n);
                drawKoch(gl, b_x, b_y, c_x, c_y, n);
                drawKoch(gl, c_x, c_y, d_x, d_y, n);
                drawKoch(gl, d_x, d_y, e_x, e_y, n);          
        }
}

For the points, b and d, the calculations are rather simple.
I'll try to talk one out the holistic idea first before listing the EQ's.

ex:
Finding Point (b): Take 1/3 the length between Point 1 and Point 2, and add it to the original Point, which in this case is Point 1.

Thus:
Code:
b_x = x1 + (1.0f/3.0f)*(x2-x1);
b_y = y1 + (1.0f/3.0f)*(y2-y1);

Conversely, we may simply subtract this same segment from Point 2 to get Point (d):
Code:
d_x = x2 - (1.0f/3.0f)*(x2-x1);
d_y = y2 - (1.0f/3.0f)*(y2-y1);


Finding Point (c) is a little more difficult and requires some trig and exception handling.
Edited by stealthybox - 10/7/12 at 4:43am
post #4 of 7
Thread Starter 

Thanks man, I knew I forgot something when drawing vertices b and d. I got those working now. I also fixed vertex c a little bit but I don't really know how to get that right. I understand that I need to seperate the quadrants but getting the geometry right is quite difficult...

Pernod
(28 items)
 
Oleo
(14 items)
 
The Sidewinder
(18 items)
 
CPUCPUMotherboardGraphics
Intel Xeon E5-4650 Intel Xeon E5-4650 Asus Z9PE-D8 WS GeForce GTX 780 Ti 
RAMRAMHard DriveHard Drive
4 x 4GB Corsair Dominator Platinum 4 x 4GB Corsair Dominator Platinum Samsung Spinpoint F3 2TB Sandisk Ultra 64GB 
Hard DriveCoolingCoolingCooling
mushkin Triactor 240GB EK Supremacy Nickel/Plexi EK Supremacy Nickel/Plexi TFC XChanger 360 
CoolingCoolingCoolingOS
Koolance RP452X2 Swiftech MCP-655 Corsair SP120 "QE" Windows 10 Enterprise x64 
OSMonitorMonitorMonitor
openSUSE Leap 42.1 x64 Dell U2412M Dell U2412M Acer P241W-d 
KeyboardPowerCaseMouse
Ducky 1008 w/ Cherry MX Blue Coolermaster Silent Pro M2 1.5kW Silverstone Raven RV01B-W Microsoft Sidewinder X5 
AudioAudioAudioOther
Asus Xonar DX Beyerdynamic DT 880 Premium AudioEngine A2 (2.0) Wacom Intuos Pen & Touch 
CPUMotherboardGraphicsRAM
Core i7 2600K @5.2  MSI MPower Z77 GeForce GTX 470 Corsair Vengeance 2000MHz C10 
Hard DriveCoolingCoolingCooling
Samsung Spinpoint F1 XSPC Raystorm EK Spin Bay Res Clear mcp-350 (ddc1) 
CoolingCoolingOSOS
XSPC EX120 XSPC EX240 openSUSE 12.3 x64 elementaryOS 0.3 (Freya) x64 
PowerCase
Corsair TX750 750W Lian Li A05NB 
CPUMotherboardGraphicsRAM
Intel C2Q Q9550 Asus P5G41C-M LX GeForce GTX 470 Corsair XMS2 DDR2 800 
Hard DriveHard DriveHard DriveHard Drive
Seagate LP 500GB Crucial M4 128GB WD Red 2TB WD Red 4TB 
CoolingOSMonitorPower
Scythe Big Shuriken w/ Silverstone Suscool 121 Windows 10 Home x64 Sony Bravia KDL-37EX500 Antec TP650 New 
CaseOther
Lian Li V351B Silverstone Suscool 121 120mm case fans 
  hide details  
Reply
Pernod
(28 items)
 
Oleo
(14 items)
 
The Sidewinder
(18 items)
 
CPUCPUMotherboardGraphics
Intel Xeon E5-4650 Intel Xeon E5-4650 Asus Z9PE-D8 WS GeForce GTX 780 Ti 
RAMRAMHard DriveHard Drive
4 x 4GB Corsair Dominator Platinum 4 x 4GB Corsair Dominator Platinum Samsung Spinpoint F3 2TB Sandisk Ultra 64GB 
Hard DriveCoolingCoolingCooling
mushkin Triactor 240GB EK Supremacy Nickel/Plexi EK Supremacy Nickel/Plexi TFC XChanger 360 
CoolingCoolingCoolingOS
Koolance RP452X2 Swiftech MCP-655 Corsair SP120 "QE" Windows 10 Enterprise x64 
OSMonitorMonitorMonitor
openSUSE Leap 42.1 x64 Dell U2412M Dell U2412M Acer P241W-d 
KeyboardPowerCaseMouse
Ducky 1008 w/ Cherry MX Blue Coolermaster Silent Pro M2 1.5kW Silverstone Raven RV01B-W Microsoft Sidewinder X5 
AudioAudioAudioOther
Asus Xonar DX Beyerdynamic DT 880 Premium AudioEngine A2 (2.0) Wacom Intuos Pen & Touch 
CPUMotherboardGraphicsRAM
Core i7 2600K @5.2  MSI MPower Z77 GeForce GTX 470 Corsair Vengeance 2000MHz C10 
Hard DriveCoolingCoolingCooling
Samsung Spinpoint F1 XSPC Raystorm EK Spin Bay Res Clear mcp-350 (ddc1) 
CoolingCoolingOSOS
XSPC EX120 XSPC EX240 openSUSE 12.3 x64 elementaryOS 0.3 (Freya) x64 
PowerCase
Corsair TX750 750W Lian Li A05NB 
CPUMotherboardGraphicsRAM
Intel C2Q Q9550 Asus P5G41C-M LX GeForce GTX 470 Corsair XMS2 DDR2 800 
Hard DriveHard DriveHard DriveHard Drive
Seagate LP 500GB Crucial M4 128GB WD Red 2TB WD Red 4TB 
CoolingOSMonitorPower
Scythe Big Shuriken w/ Silverstone Suscool 121 Windows 10 Home x64 Sony Bravia KDL-37EX500 Antec TP650 New 
CaseOther
Lian Li V351B Silverstone Suscool 121 120mm case fans 
  hide details  
Reply
post #5 of 7
Thread Starter 

There was also a way to just turn the direction of a line and altering the length. I don't know if that would be easier to implement...

 

Edit:

 

This is my new code btw

 

        private void drawKoch(GL gl, float x1, float y1, float x2, float y2, int n) {
        if (n == 1) {
            gl.glBegin(GL.GL_LINES);
            gl.glVertex2f(x1, y1);
            gl.glVertex2f(x2, y2);
            gl.glEnd();
        }
        else if (n > 1){
            
            float a_x = x1, a_y = y1;
            float b_x = x1 + (1.0f/3.0f)*(x2 - x1), b_y = y1 + (1.0f/3.0f)*(y2 - y1);
            float c_x, c_y;
            float d_x = x2 - (1.0f/3.0f)*(x2 - x1), d_y = y2 - (1.0f/3.0f)*(y2 - y1);
            float e_x = x2, e_y = y2;
            
            //First quadrant
            if (a_x <= e_x && e_y >= a_y) {
                c_x = b_x + (float)Math.cos(Math.atan((y2-y1)/(x2-x1)))*(1.0f/2.0f)*(d_x - b_x);
                c_y = b_y + (float)Math.sin(Math.atan((y2-y1)/(x2-x1)))*(1.0f/2.0f)*(d_y - b_y);
            }
            //Second quadrant
            if (a_x > e_x && e_y >= a_y) {
                c_x = b_x - (float)Math.sin(Math.atan((x2-x1)/(y2-y1)))*(-1.0f/2.0f)*(b_x - d_x);
                c_y = b_y - (float)Math.cos(Math.atan((x2-x1)/(y2-y1)))*(1.0f/2.0f)*(b_y - d_y);
            }
            //Third quadrant
            if (a_x > e_x && a_y > e_y) {
                c_x = b_x - (float)Math.cos(Math.atan((y2-y1)/(x2-x1)))*(-1.0f/2.0f)*(b_x - d_x);
                c_y = b_y - (float)Math.sin(Math.atan((y2-y1)/(x2-x1)))*(-1.0f/2.0f)*(b_y - d_y);
            }
            //Fourth quadrant
            else /* a_x <= e_x && a_y > e_y */ {
                c_x = b_x + (float)Math.sin(Math.atan((x2-x1)/(y2-y1)))*(1.0f/2.0f)*(d_x - b_x);
                c_y = b_y + (float)Math.cos(Math.atan((x2-x1)/(y2-y1)))*(-1.0f/2.0f)*(d_y - b_y);
            }

            
            drawKoch(gl, a_x, a_y, b_x, b_y, n-1);
            drawKoch(gl, b_x, b_y, c_x, c_y, n-1);
            drawKoch(gl, c_x, c_y, d_x, d_y, n-1);
            drawKoch(gl, d_x, d_y, e_x, e_y, n-1);
                        
        }
            
    }

Edited by TheBlademaster01 - 10/8/12 at 9:28am
Pernod
(28 items)
 
Oleo
(14 items)
 
The Sidewinder
(18 items)
 
CPUCPUMotherboardGraphics
Intel Xeon E5-4650 Intel Xeon E5-4650 Asus Z9PE-D8 WS GeForce GTX 780 Ti 
RAMRAMHard DriveHard Drive
4 x 4GB Corsair Dominator Platinum 4 x 4GB Corsair Dominator Platinum Samsung Spinpoint F3 2TB Sandisk Ultra 64GB 
Hard DriveCoolingCoolingCooling
mushkin Triactor 240GB EK Supremacy Nickel/Plexi EK Supremacy Nickel/Plexi TFC XChanger 360 
CoolingCoolingCoolingOS
Koolance RP452X2 Swiftech MCP-655 Corsair SP120 "QE" Windows 10 Enterprise x64 
OSMonitorMonitorMonitor
openSUSE Leap 42.1 x64 Dell U2412M Dell U2412M Acer P241W-d 
KeyboardPowerCaseMouse
Ducky 1008 w/ Cherry MX Blue Coolermaster Silent Pro M2 1.5kW Silverstone Raven RV01B-W Microsoft Sidewinder X5 
AudioAudioAudioOther
Asus Xonar DX Beyerdynamic DT 880 Premium AudioEngine A2 (2.0) Wacom Intuos Pen & Touch 
CPUMotherboardGraphicsRAM
Core i7 2600K @5.2  MSI MPower Z77 GeForce GTX 470 Corsair Vengeance 2000MHz C10 
Hard DriveCoolingCoolingCooling
Samsung Spinpoint F1 XSPC Raystorm EK Spin Bay Res Clear mcp-350 (ddc1) 
CoolingCoolingOSOS
XSPC EX120 XSPC EX240 openSUSE 12.3 x64 elementaryOS 0.3 (Freya) x64 
PowerCase
Corsair TX750 750W Lian Li A05NB 
CPUMotherboardGraphicsRAM
Intel C2Q Q9550 Asus P5G41C-M LX GeForce GTX 470 Corsair XMS2 DDR2 800 
Hard DriveHard DriveHard DriveHard Drive
Seagate LP 500GB Crucial M4 128GB WD Red 2TB WD Red 4TB 
CoolingOSMonitorPower
Scythe Big Shuriken w/ Silverstone Suscool 121 Windows 10 Home x64 Sony Bravia KDL-37EX500 Antec TP650 New 
CaseOther
Lian Li V351B Silverstone Suscool 121 120mm case fans 
  hide details  
Reply
Pernod
(28 items)
 
Oleo
(14 items)
 
The Sidewinder
(18 items)
 
CPUCPUMotherboardGraphics
Intel Xeon E5-4650 Intel Xeon E5-4650 Asus Z9PE-D8 WS GeForce GTX 780 Ti 
RAMRAMHard DriveHard Drive
4 x 4GB Corsair Dominator Platinum 4 x 4GB Corsair Dominator Platinum Samsung Spinpoint F3 2TB Sandisk Ultra 64GB 
Hard DriveCoolingCoolingCooling
mushkin Triactor 240GB EK Supremacy Nickel/Plexi EK Supremacy Nickel/Plexi TFC XChanger 360 
CoolingCoolingCoolingOS
Koolance RP452X2 Swiftech MCP-655 Corsair SP120 "QE" Windows 10 Enterprise x64 
OSMonitorMonitorMonitor
openSUSE Leap 42.1 x64 Dell U2412M Dell U2412M Acer P241W-d 
KeyboardPowerCaseMouse
Ducky 1008 w/ Cherry MX Blue Coolermaster Silent Pro M2 1.5kW Silverstone Raven RV01B-W Microsoft Sidewinder X5 
AudioAudioAudioOther
Asus Xonar DX Beyerdynamic DT 880 Premium AudioEngine A2 (2.0) Wacom Intuos Pen & Touch 
CPUMotherboardGraphicsRAM
Core i7 2600K @5.2  MSI MPower Z77 GeForce GTX 470 Corsair Vengeance 2000MHz C10 
Hard DriveCoolingCoolingCooling
Samsung Spinpoint F1 XSPC Raystorm EK Spin Bay Res Clear mcp-350 (ddc1) 
CoolingCoolingOSOS
XSPC EX120 XSPC EX240 openSUSE 12.3 x64 elementaryOS 0.3 (Freya) x64 
PowerCase
Corsair TX750 750W Lian Li A05NB 
CPUMotherboardGraphicsRAM
Intel C2Q Q9550 Asus P5G41C-M LX GeForce GTX 470 Corsair XMS2 DDR2 800 
Hard DriveHard DriveHard DriveHard Drive
Seagate LP 500GB Crucial M4 128GB WD Red 2TB WD Red 4TB 
CoolingOSMonitorPower
Scythe Big Shuriken w/ Silverstone Suscool 121 Windows 10 Home x64 Sony Bravia KDL-37EX500 Antec TP650 New 
CaseOther
Lian Li V351B Silverstone Suscool 121 120mm case fans 
  hide details  
Reply
post #6 of 7
Thread Starter 

Success!

 

 

Final Code thumbsupsmiley.png

 

    private void lineOnScreen(GL gl, float x1, float y1, float x2, float y2) {
        gl.glBegin(GL.GL_LINES);
        gl.glVertex2f(x1, y1);
        gl.glVertex2f(x2, y2);
        gl.glEnd();
    }
    
    private void drawKoch(GL gl, float x1, float y1, float x2, float y2, int n) {
        if (n <= 1) {
            gl.glBegin(GL.GL_LINES);
            gl.glVertex2f(x1, y1);
            gl.glVertex2f(x2, y2);
            gl.glEnd();
        }
        else if (n > 1){
            
            float a_x = x1, a_y = y1;
            float b_x = x1 + (1.0f/3.0f)*(x2 - x1), b_y = y1 + (1.0f/3.0f)*(y2 - y1);
            float c_x = (float)((1.0f/2.0f) * (x1 + x2)) + ((float)(Math.sqrt(3.0f)/6.0f) * (y1 - y2));
            float c_y = (float)((1.0f/2.0f) * (y1 + y2)) + ((float)(Math.sqrt(3.0f)/6.0f) * (x2 - x1));
            float d_x = x2 - (1.0f/3.0f)*(x2 - x1), d_y = y2 - (1.0f/3.0f)*(y2 - y1);
            float e_x = x2, e_y = y2;        

            
            drawKoch(gl, a_x, a_y, b_x, b_y, n-1);
            drawKoch(gl, b_x, b_y, c_x, c_y, n-1);
            drawKoch(gl, c_x, c_y, d_x, d_y, n-1);
            drawKoch(gl, d_x, d_y, e_x, e_y, n-1);
                        
        }
            
    }
Pernod
(28 items)
 
Oleo
(14 items)
 
The Sidewinder
(18 items)
 
CPUCPUMotherboardGraphics
Intel Xeon E5-4650 Intel Xeon E5-4650 Asus Z9PE-D8 WS GeForce GTX 780 Ti 
RAMRAMHard DriveHard Drive
4 x 4GB Corsair Dominator Platinum 4 x 4GB Corsair Dominator Platinum Samsung Spinpoint F3 2TB Sandisk Ultra 64GB 
Hard DriveCoolingCoolingCooling
mushkin Triactor 240GB EK Supremacy Nickel/Plexi EK Supremacy Nickel/Plexi TFC XChanger 360 
CoolingCoolingCoolingOS
Koolance RP452X2 Swiftech MCP-655 Corsair SP120 "QE" Windows 10 Enterprise x64 
OSMonitorMonitorMonitor
openSUSE Leap 42.1 x64 Dell U2412M Dell U2412M Acer P241W-d 
KeyboardPowerCaseMouse
Ducky 1008 w/ Cherry MX Blue Coolermaster Silent Pro M2 1.5kW Silverstone Raven RV01B-W Microsoft Sidewinder X5 
AudioAudioAudioOther
Asus Xonar DX Beyerdynamic DT 880 Premium AudioEngine A2 (2.0) Wacom Intuos Pen & Touch 
CPUMotherboardGraphicsRAM
Core i7 2600K @5.2  MSI MPower Z77 GeForce GTX 470 Corsair Vengeance 2000MHz C10 
Hard DriveCoolingCoolingCooling
Samsung Spinpoint F1 XSPC Raystorm EK Spin Bay Res Clear mcp-350 (ddc1) 
CoolingCoolingOSOS
XSPC EX120 XSPC EX240 openSUSE 12.3 x64 elementaryOS 0.3 (Freya) x64 
PowerCase
Corsair TX750 750W Lian Li A05NB 
CPUMotherboardGraphicsRAM
Intel C2Q Q9550 Asus P5G41C-M LX GeForce GTX 470 Corsair XMS2 DDR2 800 
Hard DriveHard DriveHard DriveHard Drive
Seagate LP 500GB Crucial M4 128GB WD Red 2TB WD Red 4TB 
CoolingOSMonitorPower
Scythe Big Shuriken w/ Silverstone Suscool 121 Windows 10 Home x64 Sony Bravia KDL-37EX500 Antec TP650 New 
CaseOther
Lian Li V351B Silverstone Suscool 121 120mm case fans 
  hide details  
Reply
Pernod
(28 items)
 
Oleo
(14 items)
 
The Sidewinder
(18 items)
 
CPUCPUMotherboardGraphics
Intel Xeon E5-4650 Intel Xeon E5-4650 Asus Z9PE-D8 WS GeForce GTX 780 Ti 
RAMRAMHard DriveHard Drive
4 x 4GB Corsair Dominator Platinum 4 x 4GB Corsair Dominator Platinum Samsung Spinpoint F3 2TB Sandisk Ultra 64GB 
Hard DriveCoolingCoolingCooling
mushkin Triactor 240GB EK Supremacy Nickel/Plexi EK Supremacy Nickel/Plexi TFC XChanger 360 
CoolingCoolingCoolingOS
Koolance RP452X2 Swiftech MCP-655 Corsair SP120 "QE" Windows 10 Enterprise x64 
OSMonitorMonitorMonitor
openSUSE Leap 42.1 x64 Dell U2412M Dell U2412M Acer P241W-d 
KeyboardPowerCaseMouse
Ducky 1008 w/ Cherry MX Blue Coolermaster Silent Pro M2 1.5kW Silverstone Raven RV01B-W Microsoft Sidewinder X5 
AudioAudioAudioOther
Asus Xonar DX Beyerdynamic DT 880 Premium AudioEngine A2 (2.0) Wacom Intuos Pen & Touch 
CPUMotherboardGraphicsRAM
Core i7 2600K @5.2  MSI MPower Z77 GeForce GTX 470 Corsair Vengeance 2000MHz C10 
Hard DriveCoolingCoolingCooling
Samsung Spinpoint F1 XSPC Raystorm EK Spin Bay Res Clear mcp-350 (ddc1) 
CoolingCoolingOSOS
XSPC EX120 XSPC EX240 openSUSE 12.3 x64 elementaryOS 0.3 (Freya) x64 
PowerCase
Corsair TX750 750W Lian Li A05NB 
CPUMotherboardGraphicsRAM
Intel C2Q Q9550 Asus P5G41C-M LX GeForce GTX 470 Corsair XMS2 DDR2 800 
Hard DriveHard DriveHard DriveHard Drive
Seagate LP 500GB Crucial M4 128GB WD Red 2TB WD Red 4TB 
CoolingOSMonitorPower
Scythe Big Shuriken w/ Silverstone Suscool 121 Windows 10 Home x64 Sony Bravia KDL-37EX500 Antec TP650 New 
CaseOther
Lian Li V351B Silverstone Suscool 121 120mm case fans 
  hide details  
Reply
post #7 of 7
I knew you'd eventually get the component projections thumb.gif

Good job man, and thanks for the thought provoking experience.
I've never had to implement Koch without helper turning methods first.
I've also never implemented it with a Point A -> B approach: (always used a length variable instead)

Try Sierpinski's triangle now!
( then draw the triangle with Koch curves! tongue.gif )

Fractals are loads of fun.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › JAva assignment (Koch Curves)