边长相等但在视觉上看起来不相等

时间:2013-10-27 19:59:23

标签: java division

我有以下代码,正如你所看到的那样,每当我pencil.forward();时,我将相同的值除以3,但当我看到不相等时。代码:

import gpdraw.*;

public class buildShape {
public SketchPad paper = new SketchPad(500,500);
public DrawingTool pencil = new DrawingTool(paper);

public void drawKochCurve(int level, double initialSize){
    if (level < 1){
        pencil.forward(initialSize);
    } else {
        drawKochCurve(level - 1, initialSize);
        pencil.forward(initialSize / 3);
        pencil.turnLeft(60);
        pencil.forward(initialSize / 3);
        pencil.turnRight(120);
        pencil.forward(initialSize / 3);
        pencil.turnLeft(60);
        pencil.forward(initialSize / 3);
    }
  }
}

主要:

public class Main {
public static void main(String[] args){
    buildShape myKoch = new buildShape();
    myKoch.drawKochCurve(1,90);
  }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

您的递归不充分。您需要以递归方式为每个段调用绘制曲线,否则不会创建分形曲线。

将其更改为:

} else {
    drawKochCurve(level - 1, initialSize / 3);
    // pencil.forward(initialSize / 3);
    pencil.turnLeft(60);
    drawKochCurve(level - 1, initialSize / 3);
    // pencil.forward(initialSize / 3);
    pencil.turnRight(120);
    drawKochCurve(level - 1, initialSize / 3);
    // pencil.forward(initialSize / 3);
    pencil.turnLeft(60);
    drawKochCurve(level - 1, initialSize / 3);
    // pencil.forward(initialSize / 3);
}