문제

I have coded my application to fully draw the Koch snowflake, however no matter how many times I stepped through with the debugger I cant understand the recursion process of this line drawKochCurve (level - 1, segmentSize); and how it brings about a fractal curve.

This was my AP "recursion" project but even though I actually understood most of the recursion examples my teacher gave me I cant get a grasp of this one.

My code:

import gpdraw.*;

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

public buildShape(){
  drawKochCurve(2,100);
  pencil.setDirection(-30);
  drawKochCurve(2,400);
  pencil.setDirection(-150);
  drawKochCurve(2,400);
}

private void drawKochCurve(int level, double initialSize){
    double segmentSize = initialSize / 3;
    if (level < 1){
        pencil.forward(initialSize);
    } else {
        drawKochCurve(level - 1, segmentSize);
        pencil.turnLeft(60);
        drawKochCurve(level - 1, segmentSize);
        pencil.turnRight(120);
        drawKochCurve(level - 1, segmentSize);
        pencil.turnLeft(60);
        drawKochCurve(level - 1, segmentSize);
    }
  }
}
도움이 되었습니까?

해결책

You seem to be drawing a Koch snowflake. You can get a plain Koch curve by eliminating 2 of the calls to drawKochCurve in the buildShape() function. In the remaining function call replace the number 2 with 0. This will cause it to produce a straight line at recursion depth 0. Then run it again using the number 1,2,3,4,5 on successive runs. Each time you increase this number, each line segment in the drawing is replaced by a Koch curve of lesser detail (due to the recursion limit). By putting the other function calls back in, you start with a triangle instead of a single line segment, but it's really doing 3 distinct Koch curves to make a snowflake.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top