import gpdraw.*;
import javax.swing.*;
import java.lang.Math;
public class Koch2 extends JFrame {
SketchPad paper;
DrawingTool pen;
public Koch2() {
paper = new SketchPad(600, 600);
pen = new DrawingTool(paper);
}
public void drawKoch(double sL, int level, double length) {
int x = level - 1;
double y = length / 3;
double z = -1.5 * sL;
if (level < 1) {
pen.up();
pen.move(z, 0);
pen.down();
pen.setDirection(0);
pen.forward(sL);
}
else {
pen.up();
pen.move(z, 0);
pen.down();
pen.setDirection(0);
pen.forward(length / (Math.pow(3, length)));
pen.setDirection(60);
pen.forward(length / (Math.pow(3, length)));
pen.turnRight(120);
pen.forward(length / (Math.pow(3, length)));
pen.turnLeft(60);
pen.forward(length / (Math.pow(3, length)));
pen.setDirection(60);
pen.forward(length / (Math.pow(3, length)));
pen.turnRight(120);
pen.forward(length / (Math.pow(3, length)));
pen.turnRight(120);
pen.forward(length / (Math.pow(3, length)));
pen.setDirection(60);
pen.forward(length / (Math.pow(3, length)));
pen.turnRight(120);
pen.forward(length / (Math.pow(3, length)));
pen.turnLeft(60);
pen.forward(length / (Math.pow(3, length)));
pen.setDirection(60);
pen.forward(length / (Math.pow(3, length)));
pen.turnRight(120);
pen.forward(length / (Math.pow(3, length)));
/*pen.setDirection(0);
pen.forward(length / (Math.pow(3, length)));
*/
drawKoch((sL), (x) , (y));
}
}
public static void main(String[] args) {
new Koch2().drawKoch(300, 6, 300);
}
}
Which sections of this code are faulty? I am trying to figure out how to generate a single template curve and then repeat that many times to make the actual curve. I don’t need to make the actual snowflake just yet, that can wait until after I figure the curve out.
Assuming
pen.forward(length / (Math.pow(3, length)));draws a straight line :The formation of a Koch curve is recursive. To draw a level n Koch curve, you’ll need to draw four level (n-1) curves. The pseudo code for drawing Koch curves is like this:
In your code, I see there’s one recursive call. Instead of drawing straight lines, you need to draw smaller Koch curves. Draw Straight lines only at the base cases.