Мне нужно создать программу, рисующую треугольник Серпинского порядка n. Для этого у меня есть несколько методов. Высота, заполненный треугольник и Серпинский. Я добился некоторого прогресса в рекурсивной печати треугольников слева внизу, но теперь, когда я пытаюсь создать треугольники справа и вверх, ничего не получается. Я думаю, что моя проблема заключается в методе fillTriangle, поскольку я не уверен, как разместить все треугольники с разными вершинами.
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.0)/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 x2 = x - x/2;
double y2 = y + height(length)/2;
double x3 = x + (x/2);
double y3 = y + height(length)/2;
double[] xx = {x, x2, x3};
double[] yy = {y, y2, y3};
StdDraw.filledPolygon(xx, yy);
}
// 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) {
System.out.println("Done!");
} else {
filledTriangle(x, height(length)/2, length/2);
x = x/2;
length = length/2;
filledTriangle(x, y, length);
filledTriangle(3*x, y, length);
n--;
sierpinski(n, x, y, 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 length = 1;
int n = Integer.parseInt(args[0]);
double t = Math.sqrt(3.0) / 2.0;
StdDraw.line(0.0, 0.0, 1.0, 0.0);
StdDraw.line(1.0, 0.0, 0.5, height(length));
StdDraw.line(0.5, height(length), 0.0, 0.0);
double x = 0.5;
double y = 0;
sierpinski(n, x, y, length);
}
}
Мне нужно создать программу, рисующую треугольник Серпинского порядка n. Для этого у меня есть несколько методов. Высота, заполненный треугольник и Серпинский. Я добился некоторого прогресса в рекурсивной печати треугольников слева внизу, но теперь, когда я пытаюсь создать треугольники справа и вверх, ничего не получается. Я думаю, что моя проблема заключается в методе fillTriangle, поскольку я не уверен, как разместить все треугольники с разными вершинами.
[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.0)/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 x2 = x - x/2; double y2 = y + height(length)/2;
double x3 = x + (x/2); double y3 = y + height(length)/2;
// 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) { System.out.println("Done!"); } else { filledTriangle(x, height(length)/2, length/2); x = x/2; length = length/2; filledTriangle(x, y, length); filledTriangle(3*x, y, length); n--; sierpinski(n, x, y, 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 length = 1; int n = Integer.parseInt(args[0]); double t = Math.sqrt(3.0) / 2.0; StdDraw.line(0.0, 0.0, 1.0, 0.0); StdDraw.line(1.0, 0.0, 0.5, height(length)); StdDraw.line(0.5, height(length), 0.0, 0.0);
double x = 0.5; double y = 0;
sierpinski(n, x, y, length);
} } [/code]
Треугольник Серпинского порядка 3 должен выглядеть так: https://i.sstatic.net/7dzJm.jpg Мой результат: https://i.sstatic.net/ZHx0f.jpg