Reputation: 13
I'm writing a program in Java to make a Recursive Sierpinski Triangle, to the middle and left side of the triangle it draws recursively like a master-stroke.
However, anything to do with the right side of the triangle simply will not draw.
Here is some code, I'll explain how it works so you have an easier time reading it.
The first thing it does is establish a height for the triangle in the height function.
Height is called in filledTriangle which uses length as an argument (which is 1) and is then passed to a variable hei.
I then use a simple set of coordinates.
x0, x1, x2. y0 , y1, y2.
After that I set up my midpoints, because all a midpoint in a Sierpinski Triangle is are the sum of 2 points divided by 2.
After that, I pass my midpoints X and Y into an array, and voila , a triforce is made!
So naturally, I attempt to create a recursive triangle for the left. The left works masterfully.
Then I plug the arguments into the right and it simply doesn't work.
/*************************************************************************
* Compilation: javac Sierpinski.java
* Execution: java Sierpinski
*
* @author:
*
*************************************************************************/
public class Sierpinski {
// Height of an equilateral triangle whose sides are of the specified length.
public static double height(double length) {
return (Math.sqrt(3.0)*length)/2.0;
// WRITE YOUR CODE HERE
}
// Draws a filled equilateral triangle whose bottom vertex is (x, y)
// of the specified side length.
public static void filledTriangle(double x, double y, double length) {
double hei = height(length);
double x0 = x-x;
double x1 = x;
double x2 = x/2;
double y0 = y;
double y1 = y;
double y2 = hei;
double ACx = (x0 + x1)/2;
double ACy = (y0 + y1)/2;
double BCx = (x1 + x2)/2;
double BCy = (y1 + y2)/2;
double BAx = (x0 + x2)/2;
double BAy = (y0 + y2)/2;
double [] X ={ACx, BCx, BAx};
double [] Y ={ACy, BCy, BAy};
//Lines 39-42 Draw our main triangle.
StdDraw.line(x0,y0,x1,y1);
StdDraw.line(x0,y0,x2,y2);
StdDraw.line(x2,y2,x1,y1);
//This fills the triangles displaced by X and Y..
//StdDraw.filledPolygon(X, Y);
//StdDraw.line(BCx, BCy ,ACx ,ACy);
//StdDraw.line(ACx, ACy, BAx, BAy);
//StdDraw.line(BAx, BAy, BCx, BCy);
StdDraw.filledPolygon(X,Y);
//WRITE YOUR CODE HERE
}
// Draws a Sierpinski triangle of order n, such that the largest filled
// triangle has bottom vertex (x, y) and sides of the specified length.
public static void sierpinski(int n, double x, double y, double length) {
filledTriangle(x, y, length);
if(n <= 1)
filledTriangle(x, y, length);
else{
//sierpinski(n--,x/2,y,length/2);
sierpinski(n--, x+x/2,y,length/2);
//sierpinski(n--, x+0.5,y, length/2);
}
// WRITE YOUR CODE HERE
}
// Takes an integer command-line argument n;
// draws the outline of an equilateral triangle (pointed upwards) of length 1;
// whose bottom-left vertex is (0, 0) and bottom-right vertex is (1, 0); and
// draws a Sierpinski triangle of order n that fits snugly inside the outline.
public static void main(String[] args) {
//StdDraw.setScale(-1.5, +1.5);
filledTriangle(1, 0, 1);
sierpinski(Integer.parseInt(args[0]), 1, 0, 1);
//sierpinski(Integer.parseInt(args[0]),1.0,1.0,1);
// WRITE YOUR CODE HERE
}
}
I commented out the left and top triangle to focus solely on the right, what I did for the right sierpinski Triangle was simply do x+x/2 for the x-coordinate.
What I think the answerr should be for the right recursive side is : sierpinski(n--, x+x/2,y,length/2);
I not only thought about this, but I wrote it down on paper, this should definitely work, but it just draws the leftest triangle from the (0,0) coordinate up to a strange height and the rightest triangle on some weird angle outside the bounds. The more I fidget with it, the more I realize that it isn't going to work and my math somewhere was off. But, I'm not sure where.
Can anyone please assist?
Upvotes: 0
Views: 709
Reputation: 143
There seems to be a couple of bugs in your code:
n--
does not return n-1, but rather n. You should replace it with n-1
.filledTriangle
. The (x,y) pairings you create are: (x-x, y), (x, y), and (x/2, height). The (x,y) pairing is a correct point, but the other two are not. Instead you should use (x-length/2,y+height) and (x-length, y) for the top and left points on the triangle.sierpinski
recursively, you should pass it x
instead of x+x/2
. This is because, in this program, you are using the bottom right triangle vertex as the primary vertex. When drawing the right side of the sierpinski triangle, this vertex stays the same.Upvotes: 0