Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Ryan Ko
- //Period 5
- //Lab A9.2
- import gpdraw.*;
- import java.util.Scanner;
- public class KochCurve{
- private DrawingTool myPencil;
- private SketchPad myPaper;
- private Scanner in;
- private double length;
- private int x,y,count;
- //Constructor
- public KochCurve(){
- myPaper = new SketchPad(800,800);
- myPencil = new DrawingTool(myPaper);
- in = new Scanner(System.in);
- length=400;
- x=y=count=0;
- }
- //Gets Koch Level
- public void getLevelNum(){
- myPencil.up();
- myPencil.move(-200,199);
- myPencil.move(-200,200);
- myPencil.down();
- myPencil.turnRight();
- System.out.print("Koch Curve Level: ");
- x = in.nextInt();
- //For-loop used in order to create full snowflake
- for(count=1; count<=3; count++){
- y=x;
- drawKochCurve(y);
- length=(length*3);
- myPencil.turnRight(120);
- }
- }
- //Draws Koch Curve
- public void drawKochCurve(int y){
- //sets new length to 1/3 of the previous one
- length = (length/3);
- //base case
- if(y==0){
- myPencil.forward(length);
- myPencil.turnLeft(60);
- myPencil.forward(length);
- myPencil.turnRight(120);
- myPencil.forward(length);
- myPencil.turnLeft(60);
- myPencil.forward(length);
- }
- //recursion initiators
- else{
- drawKochCurve(y-1);
- length=(length*3);
- myPencil.turnLeft(60);
- drawKochCurve(y-1);
- length=(length*3);
- myPencil.turnRight(120);
- drawKochCurve(y-1);
- length=(length*3);
- myPencil.turnLeft(60);
- drawKochCurve(y-1);
- length=(length*3);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement