2017-04-08 73 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