我在這裏是新來的,所以請原諒我,如果我做了不正確的事情。我已經完成了關於我的主題的研究,但似乎無法正確地找到如何準備我的遞歸三角形。Java中的遞歸三角形
我試圖接受一個我能夠做的參數,並創建一個我能夠在Java中做的三角。 當參數= 1時,應創建一個三角形。 參數= 2應創建四個三角形總數。 之後,我的程序混亂了,我很難找出如何使三個三角形出現在三個三角形的中點。我之前見過的一些建議建議我使用中點,但我無法找到雙方應該在多長時間。
下面是代碼:
public class Sierpinski{
public static void main(String args[]) {
String firstArgument = args[0];
int b = Integer.parseInt(firstArgument);
int c = b - 1;
double rt = Math.sqrt(3);
double lol = rt/4;
double[] x = {.25, .5, .75};
double[] y = {lol, 0, lol};
double x1 = .25;
double x2 = .5;
double x3 = .75;
double y1 = lol;
double y2 = 0;
double y3 = lol;
StdDraw.filledPolygon(x,y);
//Small Triangles
while (c > 0){
//Small Triangle 1
double xb1 = (x1)/2/c;
double yb1 = (lol)/2/c;
double xb2 = (x1)/c;
double yb2 = (y2)/2;
double xb3 = (x1 + x2)/2/c;
//double yb30 = (.44301270189)/2 * 2 * i;
double xb4 = 1 - ((x1)/2/c);
double yb4 = ((lol)/2/c);
double xb5 = 1 - ((x1)/c);
double yb5 = (0)/2;
double xb6 = 1 - ((x1 + x2)/2/c);
double xb7 = x2 + ((x1)/2/c);
double yb7 = ((lol)/2/c);
double xb8 = x2 + ((x1)/c);
double yb8 = (0)/2;
double xb9 = x2 + ((x1 + x2)/2/c);
double xb10 = .125 + ((x1)/2/c);
double yb10 = (lol)/2/c;
double xb11 = (x1)/c;
double yb11 = (0)/2;
double xb12 = (x1 + x2)/2/c;
double[] xb0 = {xb1, xb2, xb3, xb4, xb5, xb6, xb7, xb8, xb9};
double[] yb0 = {yb1, yb2, yb1, yb4, yb5, yb4, yb7, yb8, yb7};
StdDraw.filledPolygon(xb0,yb0);
//Small Triangle 2
double xc1 = (.25 + .5)/2;
double yc1 = 1 - (0.44301270189 - .27409407929) * 2 ;
double xc2 = (.5); // 2;
double yc2 = (.44301270189); // 2;
double xc3 = (.5 + .75)/2;
//double yb30 = (.44301270189); //2 * b;
double xc4 = ((.25 + .5)/2);
double yc4 = .5 + (1 - (0.44301270189 - .27409407929) * 2) ;
double xc5 = (.5); // 2;
double yc5 = .5 + (.44301270189); // 2;
double xc6 = (.5 + .75)/2;
double xc7 = x2 + ((x1)/2/c);
double yc7 = ((lol)/2/c);
double xc8 = x2 + ((x1)/c);
double yc8 = (0)/2;
double xc9 = x2 + ((x1 + x2)/2/c);
double xc10 = .125 + ((x1)/2/c);
double yc10 = (lol)/2/c;
double xc11 = (x1)/c;
double yc11 = (0)/2;
double x12 = (x1 + x2)/2/c;
double[] xc0 = {xc1, xc2, xc3};
double[] yc0 = {yc1, yc2, yc1};
StdDraw.filledPolygon(xc0,yc0);
//Small Triangle 3
double xd1 = (.5 + .75)/2;
double yd1 = (.44301270189)/2;
double xd2 = (.75); // 2;
double yd2 = (0)/2;
double xd3 = (.25 + .5)/2 + .5;
double yd30 = (.44301270189) /2;
double[] xd0 = {xd1, xd2, xd3};
double[] yd0 = {yd1, yd2, yd1};
StdDraw.filledPolygon(xd0,yd0);
c--;
}
}
}
現在,當它形成於3它得到的前四個完美,第五重疊(我以爲是因爲我試圖用確切的數字,因爲我已經看到了別人這沒有重疊的問題,但那是另一個問題,似乎飛過我的腦海。)
哪裏是遞歸應該是?我看到的唯一方法是'main()'.. –
遠離遞歸到_me_ –
我認爲while循環是遞歸嗎? – user3316323