Advertisement
LilFrostay

Untitled

Oct 4th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1.  
  2.  
  3. public class Koch {
  4.  
  5. private static int startingn;
  6.  
  7. // plot Koch curve of order n, with given step size
  8. public static void koch(int n, double step, Turtle turtle) {
  9. if (n == 0) {
  10. turtle.goForward(step);
  11. return;
  12. }
  13. koch(n-1, step, turtle);
  14. turtle.turnLeft(60.0);
  15. koch(n-1, step, turtle);
  16. turtle.turnLeft(-120.0);
  17. koch(n-1, step, turtle);
  18. turtle.turnLeft(60.0);
  19. koch(n-1, step, turtle);
  20. }
  21.  
  22. private static double getArea(double step) {
  23. double output = 0;
  24.  
  25. int pow = startingn-1;
  26.  
  27. double len = step;
  28.  
  29. output += (Math.sqrt(3)/4)*Math.pow(len,2);
  30.  
  31. for(int i = 1; i < startingn; i++) {
  32. int push = 3*(4*i);
  33.  
  34. output += push*(Math.sqrt(3)/4)*Math.pow((len/Math.pow(3,i)),2);
  35.  
  36. System.out.println(i);
  37. }
  38.  
  39.  
  40.  
  41. //Returning the total area.
  42. return output;
  43. }
  44.  
  45. public static void main(String[] args) {
  46. startingn = 3;
  47. double step = 1.0 / Math.pow(3.0, startingn);
  48. Turtle turtle = new Turtle(0.0, 0.0, 0.0);
  49. koch(startingn, step, turtle);
  50. System.out.println(getArea(3));
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement