Рекурсивный треугольник Сирпинского в Яве?JAVA

Программисты JAVA общаются здесь
Anonymous
Рекурсивный треугольник Сирпинского в Яве?

Сообщение Anonymous »

Мне нужно создать программу, которая привлекает треугольник Sierpinski of n. Для этого у меня есть несколько методов. Высота, заполненный и Сьерпински. Я добился некоторого прогресса в печати треугольников рекурсивно в левом нижнем углу, но теперь, когда я пытаюсь создать треугольники справа, и все не работает. Я думаю, что моя проблема заключается в методе Fulltriangle, так как я не уверен, как разместить все треугольники с разными вершинами. < /P>

Код: Выделить всё

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);

}
}
a Sierpinski Triangle of Order 3 должен выглядеть следующим образом: https://i.sstatic.net/7dzjm.jpg
Мой вывод: https://i.sstatic.net/zhx0f.jpg

Подробнее здесь: https://stackoverflow.com/questions/585 ... le-in-java

Вернуться в «JAVA»