2012-10-21 87 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