2012-10-21 88 views
0

我想在java中使用遞歸和gpdraw做一個科赫雪花。我可以自己製作實際的科赫曲線,但是我不知道如何讓它一直到來並製作一片雪花。科赫雪花Java遞歸

import gpdraw.*; 

public class KochCurve 
{ 
    private SketchPad myPaper; 
    private DrawingTool myPencil; 

    public KochCurve() 
    { 
    myPaper = new SketchPad(600,600); 
    myPencil = new DrawingTool(myPaper); 
    } 

    public void draw() 
    { 
    drawKochCurve(6, 300); 
    } 

    private void drawKochCurve(double level, double sideLength) 
    { 
    if(level < 1) 
     myPencil.forward(sideLength); 

    else 
    { 
     drawKochCurve(level - 1, (sideLength)/3); 
     myPencil.turnLeft(60); 
     drawKochCurve(level - 1, (sideLength)/3); 
     myPencil.turnRight(120); 
     drawKochCurve(level - 1, (sideLength)/3); 
     myPencil.turnLeft(60); 
     drawKochCurve(level - 1, (sideLength)/3); 
    } 
    } 
} 

回答

0

您將不得不在三角形的三個方向的每個方向繪製相同的曲線。你可以通過編寫一個函數drawKochCurve(double level,double sideLength,double additionalAngle)並調用它三次,添加additionalAngle來完成。

維基百科有更多詳細信息:最初由科赫描述的科赫曲線只用原始三角形的三條邊中的一條構成。換句話說,三條科赫曲線構成了科赫雪花。 http://en.wikipedia.org/wiki/Koch_snowflake