2014-02-16 119 views
0

我在這裏是新來的,所以請原諒我,如果我做了不正確的事情。我已經完成了關於我的主題的研究,但似乎無法正確地找到如何準備我的遞歸三角形。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它得到的前四個完美,第五重疊(我以爲是因爲我試圖用確切的數字,因爲我已經看到了別人這沒有重疊的問題,但那是另一個問題,似乎飛過我的腦海。)

+0

哪裏是遞歸應該是?我看到的唯一方法是'main()'.. –

+1

遠離遞歸到_me_ –

+0

我認爲while循環是遞歸嗎? – user3316323

回答

0

我猜這裏很多,但假設你想找到原始三角形的每個邊緣的中點,並使用中點與最初的三角形頂點一起在原始中刻出4個三角形,遞歸地遞歸到任意深度。

爲此,您的代碼將是這樣的。

public class TestIt { 

    public static void inscribeTriangles(double xa, double ya, 
             double xb, double yb, 
             double xc, double yc, int depth) { 
     if (depth <= 1) { 
      // We've reached the bottom of the recursion, so draw! 
      // Don't fill it or you won't see the inscribed triangles. 
      StdDraw.polygon(new double[]{xa, xb, xc}, new double[]{ya, yb, yc}); 
     } else { 
      // Else recur to draw 4 inscribed triangles. 
      double xab = (xa + xb)/2, yab = (ya + yb)/2; 
      double xbc = (xb + xc)/2, ybc = (yb + yc)/2; 
      double xca = (xc + xa)/2, yca = (yc + ya)/2; 
      inscribeTriangles(xa, ya, xab, yab, xca, yca, depth - 1); 
      inscribeTriangles(xb, yb, xbc, ybc, xab, yab, depth - 1); 
      inscribeTriangles(xc, yc, xca, yca, xbc, ybc, depth - 1); 
      // For Sierpinksi, don't subdivide 4th triangle: 
      // inscribeTriangles(xab, yab, xbc, ybc, xca, yca, depth - 1); 
      // And in this case you can also fill the polygon if you like. 
     } 
    } 

    public static void main(String [] args) { 
     inscribeTriangles(0, 0, 1, 0, 0.5, 1, 5); 
    } 
} 

這是輕微測試,但應該沒問題。注意我的起始三角形不是等邊的,但算法並不需要它。

+0

我已經在那裏工作,但它並沒有實際印出較小的三角形,有什麼我需要做的嗎?喜歡真正畫出一個圈子,你需要circle.draw,是對的還是還有其他需要的東西? – user3316323

+0

好的。我沒有意識到你的庫假定一個0到1的域而不是像素。所以'int's應該是'double's。它現在是一個可運行的代碼,可以在我的機器上正常工作。 – Gene

+0

啊!你以前從來沒有說過謝爾賓斯基!要做到這一點,只需刪除最後一個電話到'inscribeTriangles'。我是通過評論出來的。 – Gene

0

以下是完整的工作代碼。享受:)

public class voicehovich{ 

     int generations; 
     double borderSideSize; 

    public static void main(String args[]) { 

     voicehovich v =new voicehovich(); 

     String firstArg = args[0]; 
     v.generations = Integer.parseInt(firstArg); 


     double startX=0.25; 
     double startY=0.5; 
     double startSide=0.5; 

     //stop basing on side size 
     v.borderSideSize = startSide/ (Math.pow(2,v.generations-1)); 

     //start creating triangles 
     v.drawTriangle(startX,startY, startSide); 


     } 


      public void drawTriangle(double topLeftX, double topLeftY, double sideSize){ 
      double[] x = {topLeftX, topLeftX+sideSize/2, topLeftX+sideSize}; 

      double lowY = Math.sqrt(sideSize*sideSize-((sideSize/2)*(sideSize/2))); 
      double[] y = {topLeftY, topLeftY-lowY, topLeftY}; 

      StdDraw.filledPolygon(x,y); 


      if (sideSize>borderSideSize){ 
      findCoordinatesOfThree(topLeftX,topLeftY,sideSize); 
      } 

     } 

     public void findCoordinatesOfThree(double topLeftX, double topLeftY, double sideSize){ 
      //clculate coordinate of 3 triangles 
      double tLeftX = topLeftX+sideSize/4-sideSize/2; 
      double tLeftY = topLeftY-Math.sqrt(sideSize*sideSize-((sideSize/2)*(sideSize/2)))/2 ; 

      double tRightX = topLeftX+sideSize*3/4; 
      double tRightY = topLeftY-Math.sqrt(sideSize*sideSize-((sideSize/2)*(sideSize/2)))/2 ; 

      double tTopX = topLeftX+sideSize/4; 
      double tTopY = topLeftY+Math.sqrt(sideSize/2*sideSize/2-((sideSize/4)*(sideSize/4))); 


      drawTriangle(tLeftX, tLeftY, sideSize/2); 
      drawTriangle(tRightX, tRightY, sideSize/2); 
      drawTriangle(tTopX, tTopY, sideSize/2); 

     } 

    }