daily pastebin goal
83%
SHARE
TWEET

Untitled

a guest Feb 24th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Ryan Ko
  2. //Period 5
  3. //Lab A9.2
  4.  
  5. import gpdraw.*;
  6. import java.util.Scanner;
  7.  
  8. public class KochCurve{
  9.     private DrawingTool myPencil;
  10.     private SketchPad myPaper;
  11.     private Scanner in;
  12.     private double length;
  13.     private int x,y,count;
  14.    
  15.     //Constructor
  16.     public KochCurve(){
  17.         myPaper = new SketchPad(800,800);
  18.         myPencil = new DrawingTool(myPaper);
  19.         in = new Scanner(System.in);
  20.         length=400;
  21.         x=y=count=0;
  22.     }
  23.    
  24.     //Gets Koch Level
  25.     public void getLevelNum(){
  26.         myPencil.up();
  27.         myPencil.move(-200,199);
  28.         myPencil.move(-200,200);
  29.         myPencil.down();
  30.         myPencil.turnRight();
  31.         System.out.print("Koch Curve Level: ");
  32.         x = in.nextInt();
  33.              
  34.        //For-loop used in order to create full snowflake
  35.         for(count=1; count<=3; count++){
  36.           y=x;
  37.           drawKochCurve(y);
  38.           length=(length*3);
  39.           myPencil.turnRight(120);
  40.        }
  41.     }
  42.    
  43.     //Draws Koch Curve
  44.     public void drawKochCurve(int y){
  45.       //sets new length to 1/3 of the previous one
  46.       length = (length/3);
  47.      
  48.       //base case
  49.       if(y==0){
  50.         myPencil.forward(length);
  51.         myPencil.turnLeft(60);
  52.         myPencil.forward(length);
  53.         myPencil.turnRight(120);
  54.         myPencil.forward(length);
  55.         myPencil.turnLeft(60);
  56.         myPencil.forward(length);
  57.       }
  58.       //recursion initiators
  59.       else{
  60.         drawKochCurve(y-1);
  61.         length=(length*3);
  62.         myPencil.turnLeft(60);
  63.         drawKochCurve(y-1);
  64.         length=(length*3);
  65.         myPencil.turnRight(120);
  66.         drawKochCurve(y-1);
  67.         length=(length*3);
  68.         myPencil.turnLeft(60);
  69.         drawKochCurve(y-1);
  70.         length=(length*3);
  71.       }
  72.     }
  73. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top