public class Sierpinski { // Height of an equilateral triangle whose sides are o
ID: 3723330 • Letter: P
Question
public class Sierpinski {
// Height of an equilateral triangle whose sides are of the specified length.
public static double height(double length) {
double height = length * Math.sqrt(3) / 2;
return height;
}
// 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 x1 = x - (length / 2);
double y1 = y + height(length);
double x2 = x + (length / 2);
double y2 = y + height(length);
double [] xarray = {x, x1, x2};
double [] yarray = {y, y1, y2};
StdDraw.filledPolygon(xarray, yarray);
}
// 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) {
if (n == 0) return;
length /= 2;
filledTriangle(x, y, length);
StdDraw.setPenColor(StdDraw.BLACK);
double x1 = x - (length / 2);
double y1 = y;
double x2 = x + (length / 2);
double y2 = y;
sierpinski(n - 1, x1, y1, length);
sierpinski(n - 1, x2, y2, length);
sierpinski(n - 1, x, y + height(length), length);
}
// 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) {
int n = Integer.parseInt(args[0]);
double x = 0.5;
double y = 0;
double length = 1;
Sierpinski.filledTriangle(x, y, length);
Sierpinski.sierpinski(n, x, y, length);
}
}
I can't see how to fix my sierpinski class.
Also, how do I draw a upwards triangle from my filledTriangle for my main class?
Thank you.
(1/2, v3/2) (1/4, v3/4) (3/4, v3/4) (0, 0) 1/2,0) (3/4, 0)Explanation / Answer
here is the modified code : -------------->>>>>>>>>>>>
public class Sierpinski {
// Height of an equilateral triangle whose sides are of the specified length.
public static double height(double length) {
double height = length * Math.sqrt(3) / 2;
return height;
}
// Draws a filled equilateral triangle whose bottom vertex is (x, y)
// of the specified side length.
//here i added a state parameter whick tell the function to print upward triangle or downward triangle
//state == 1 for upward triangle state == 2 for downward triangle
public static void filledTriangle(double x, double y, double length,int state) {
double x1 = x - (length / 2);
//I changed here to draw the upward triangle
double y1;
if(state == 1)
y1 = y - height(length);
else
y1 = y + height(length);
double x2 = x + (length / 2);
//I also changed here for the same
double y2;
if(state == 1)
y2 = y - height(length);
else
y2 = y + height(length);
double [] xarray = {x, x1, x2};
double [] yarray = {y, y1, y2};
//see this StdDraw if it is a Object of any class then you have to create the object of that class by this name
//or if it is a class and methods of that class is static then it is ok
//if the methods are not static then you have to create object of that class and then call the this filledPolygon
//method from that object you have create it globally to this class and also static
StdDraw.filledPolygon(xarray, yarray);
}
// 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) {
if (n == 0) return;
length /= 2;
StdDraw.setPenColor(StdDraw.BLACK);
filledTriangle(x, y, length,1);
double x1 = x - (length / 2);
double y1 = y;
double x2 = x + (length / 2);
double y2 = y;
sierpinski(n - 1, x1, y1, length);
sierpinski(n - 1, x2, y2, length);
sierpinski(n - 1, x, y + height(length), length);
}
// 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) {
int n = Integer.parseInt(args[0]);
double x = 0.5;
double y = 0;
double length = 1;
Sierpinski.filledTriangle(x, y, length,2);
Sierpinski.sierpinski(n, x, y, length);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.