Results 1 to 6 of 6
 03302012, 02:50 AM #1Member
 Join Date
 Mar 2012
 Posts
 34
 Rep Power
 0
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?
Java 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); } }
Last edited by Fubarable; 03302012 at 03:37 AM. Reason: code tags added

Re: Sierpinski Triangles. Have a few questions.
 03302012, 03:41 AM #3
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 12
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.
 03302012, 03:59 AM #4Member
 Join Date
 Mar 2012
 Posts
 34
 Rep Power
 0
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!
 03302012, 05:13 AM #5
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 12
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)
Java Code:public int factorial(int n){ if(n == 0){ return 1; } return n * factorial(n  1); }
 03302012, 06:50 AM #6Member
 Join Date
 Mar 2012
 Posts
 34
 Rep Power
 0
Similar Threads

Two triangles of stars next to each other
By Aero in forum New To JavaReplies: 3Last Post: 09262011, 08:20 PM 
Recursive Sierpinski's Carpet
By baf06 in forum New To JavaReplies: 1Last Post: 04162011, 04:26 PM 
Drawing out triangles
By leiferouis in forum New To JavaReplies: 24Last Post: 01162009, 09:18 PM 
Triangles
By CodeDog in forum New To JavaReplies: 9Last Post: 10142008, 10:18 PM 
asterisks triangles
By Dan121 in forum New To JavaReplies: 1Last Post: 01122008, 08:42 PM
Bookmarks