flaw600
flaw600

Reputation: 181

Recursive Sierpinski's Triangle Java

I'm trying to draw Sierpinski's Triangle recursively in Java, but it doesn't work, though to me the logic seems fine. The base case is when the triangles are within 2 pixels of each other, hence the use of the Distance Formula.

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.awt.Canvas;

public class Triangle extends Canvas implements Runnable
{
    private static final int WIDTH = 800;
    private static final int HEIGHT = 600;


public Triangle()
{
    setBackground(Color.WHITE);
}

public void paint( Graphics window )
{
    window.setColor(Color.BLUE);
    window.setFont(new Font("ARIAL",Font.BOLD,24));     
    window.drawString("Serpinski's Gasket", 25, 50);

    triangle(window, (WIDTH-10)/2, 20, WIDTH-40, HEIGHT-20, 40, HEIGHT-20, 4);
}

public void triangle(Graphics window, int x1, int y1, int x2, int y2, int x3, int y3, int r)
{


    //if statement base case
        //midpoint = (x1 + x2 / 2), (y1 + y2/ 2)
        if(Math.sqrt((double)(Math.pow(x2-x1, 2)) + (double)(Math.pow(y2-y1, 2))) > 2)
        //if(r==0)
        {
            window.drawLine(x1, y1, x2, y2);     
            window.drawLine(x2, y2, x3, y3);
            window.drawLine(x3, y3, x1, y1);
        }

        int xa, ya, xb, yb, xc, yc;   // make 3 new triangles by connecting the midpoints of
        xa = (x1 + x2) / 2;             //. the previous triangle 
        ya = (y1 + y2) / 2;
        xb = (x1 + x3) / 2;
        yb = (y1 + y3) / 2;
        xc = (x2 + x3) / 2;
        yc = (y2 + y3) / 2;


        triangle(window, x1, y1, xa, ya, xb, yb, r-1);   // recursively call the function using the 3 triangles
        triangle(window, xa, ya, x2, y2, xc, yc, r-1);
        triangle(window, xb, yb, xc, yc, x3, y3, r-1);

}

public void run()
{
    try{
        Thread.currentThread().sleep(3);    
    }
    catch(Exception e)
    {
    }
}
}

The Runner is

import javax.swing.JFrame;

public class FractalRunner extends JFrame
{
    private static final int WIDTH = 800;
    private static final int HEIGHT = 600;

public FractalRunner()
{
    super("Fractal Runner");

    setSize(WIDTH+40,HEIGHT+40);

    getContentPane().add(new Triangle());   

    setVisible(true);
}

public static void main( String args[] )
{
    FractalRunner run = new FractalRunner();
}
}

To me this should work but it causes a runtime/StackOverFlow error that I don't know how to correct. Any help?

Upvotes: 3

Views: 27209

Answers (3)

Guyn
Guyn

Reputation: 3

"StdDraw" was taken from here:

public class Sierpinski {

    public static void sierpinski(int n) {
        sierpinski(n, 0, 0, 1);
    }

    public static void sierpinski(int n, double x, double y, double size) {

        if (n == 0) return;

        //compute triangle points
        double x0 = x;
        double y0 = y;
        double x1 = x0 + size;
        double y1 = y0;
        double x2 = x0 + size / 2;
        double y2 = y0 + (Math.sqrt(3)) * size / 2;

        // draw the triangle        
        StdDraw.line(x0, y0, x1, y1);
        StdDraw.line(x0, y0, x2, y2);
        StdDraw.line(x1, y1, x2, y2);
        StdDraw.show(100);

        //recursive calls
        sierpinski(n-1, x0, y0, size / 2);
        sierpinski(n-1, (x0 + x1) / 2, (y0 + y1) / 2, size / 2);
        sierpinski(n-1, (x0 + x2) / 2, (y0 + y2) / 2, size / 2);
    }

    // read in a command-line argument n and plot an order Sierpinski Triangle
    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        StdDraw.setPenRadius(0.005);
        sierpinski(n);
    }
}

Guy

Upvotes: 0

x86ris
x86ris

Reputation: 61

Chances are your base case might not be working properly- what if the distance between two triangles is never two pixels? say we star with y1 and x1 being 0 and 200. their midpoint would be 100, then 50, 25, 12, 6, 3, 1<--- never hits the 2 pixel base case...

Upvotes: 0

DrC
DrC

Reputation: 7698

You need to move the recursive calls to triangle, and the associated math, inside the conditional check on the separation. Right now, it will always call it and therefore you get the stack overflow.

Upvotes: 2

Related Questions