Sierpinski Triangles. Have a few questions.

So I was working on this program call Sierpinski Triangles. The program is working but doesn't seems to be right.

When I run the program "java Sierpinski Triangles 4" I got one big triangle as the output. Am I doing something wrong here?

Code:

`// SierpinskiTriangles.java`

public class SierpinskiTriangles

{

// Recursively draws Sierpinski triangles. The argument n specifies the

// recursion depth. The rest of the arguments are the (x, y) coordinates

// of the vertices of an equilateral triangle; a being the bottom left vertex,

// b the bottom right vertex, and c the top vertex.

private static void draw(int n, double ax, double ay, double bx, double by,

double cx, double cy)

{

// TBD

double x = 0.0;

double y = 0.0;

double[] xs = {ax , bx , cx};

double[] ys = {ay , by , cy};

for(int i =0; i < n; i++);

{

int k = StdRandom.uniform(1);

if(n ==0)

{

double x1 = (xs[k] * x) + (xs[k] * y) + (xs[k]);

double y1 = (ys[k] * x) + (ys[k] * y) + (ys[k]);

}

}

StdDraw.polygon(xs,ys);

}

// Entry point.

public static void main(String[] args)

{

int n = Integer.parseInt(args[0]); // Recursion depth.

StdDraw.setXscale(0.0, 1.0);

StdDraw.setYscale(0.0, 1.0);

StdDraw.show(0);

draw(n, 0.0, 0.0, 1.0, 0.0, 0.5, 0.866);

StdDraw.show(0);

}

}

Re: Sierpinski Triangles. Have a few questions.

Quote:

Originally Posted by

**xdrazkalnytex** So I was working on this program call Sierpinski Triangles. The program is working but doesn't seems to be right.

When I run the program "java Sierpinski Triangles 4" I got one big triangle as the output. Am I doing something wrong here?

Code:

`// SierpinskiTriangles.java`

public class SierpinskiTriangles

{

// ****Recursively**** draws Sierpinski triangles. The argument n specifies the

// recursion depth. The rest of the arguments are the (x, y) coordinates

// of the vertices of an equilateral triangle; a being the bottom left vertex,

// b the bottom right vertex, and c the top vertex.

private static void draw(int n, double ax, double ay, double bx, double by,

double cx, double cy)

{

// TBD

double x = 0.0;

double y = 0.0;

double[] xs = {ax , bx , cx};

double[] ys = {ay , by , cy};

for(int i =0; i < n; i++);

{

int k = StdRandom.uniform(1);

if(n ==0)

{

double x1 = (xs[k] * x) + (xs[k] * y) + (xs[k]);

double y1 = (ys[k] * x) + (ys[k] * y) + (ys[k]);

}

}

StdDraw.polygon(xs,ys);

}

// Entry point.

public static void main(String[] args)

{

int n = Integer.parseInt(args[0]); // Recursion depth.

StdDraw.setXscale(0.0, 1.0);

StdDraw.setYscale(0.0, 1.0);

StdDraw.show(0);

draw(n, 0.0, 0.0, 1.0, 0.0, 0.5, 0.866);

StdDraw.show(0);

}

}

Note the word highlighted by the **** **** -- **your** comments state that this is supposed to be an example of recursion. So where is your recursion?

Re: Sierpinski Triangles. Have a few questions.

First, I don't see any recursive calls. Also, in the loop in draw you create local variables in the loops body and never use them.

Re: Sierpinski Triangles. Have a few questions.

So does that mean I dont need a loop?

I didn't know there was a recursive, when I first got the file for this program I just fill out the body.

Also, recursive means the triangles supposed to draw 3 more triangles inside of the large one right?

Can someone help me with the recursive calls since I have no idea how to get started.

Thanks!

Re: Sierpinski Triangles. Have a few questions.

Using recursion is a general practice, which is common in mathematics, as well as computer science. Recursion is the process of defining a base case and giving a rule which uses previous terms. As a concrete example, here is a recursive definition of n! (n factorial)

Code:

`public int factorial(int n){`

if(n == 0){

return 1;

}

return n * factorial(n - 1);

}

With each iteration you perform a smaller sub task. Generally, loops aren't used in recursion. Perhaps for the recursive definition of Sierpinski, you may stop when n reaches a certain depth, and each depth will call on itself to draw three sub Sierpinski triangles inside of the initial one.

Re: Sierpinski Triangles. Have a few questions.

how would I start my recursive for this program?

the public int......can't be place at the beginning of the program.