2017-04-08 74 views
0

我想編寫一個科赫遞歸方法的代碼。我做到了這一點,但我似乎無法弄清楚如何將烏龜變成實際的雪花形狀。使用遞歸繪製科赫雪花

任何形式的解釋將不勝感激,謝謝!

import java.awt.Color; 


public class Triangle { 

    public static void main(String[] args) { 
     World myWorld = new World(900,900,Color.GREEN); 
     Turtle bob = new Turtle(myWorld); 
     bob.setDelay(0); 
     //drawTriangle(bob, 4, -200,-100,405,-100,100,350.75); 

     koch(bob, 3, 12.0); 

    } 

    public static void koch(Turtle t, int n, double size) { 
     if(n==0) 
      t.forward(size); 
     else 
     { 
      koch(t, n-1, size); 
      t.left(60); 
      koch(t, n-1, size); 
      t.right(120); 
      koch(t, n-1, size); 
      t.left(60); 
      koch(t, n-1, size); 
     } 


    } 

回答

0

一個簡單的辦法是更換:

koch(bob, 3, 12.0); 

有:

koch(bob, 3, 12.0); 
bob.right(120); 
koch(bob, 3, 12.0); 
bob.right(120); 
koch(bob, 3, 12.0); 

或等效迴路:

for (int i = 0; i < 3; i++) { 
    koch(bob, 3, 12.0); 
    bob.right(120); 
} 

輸出

enter image description here