Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. package koch;
  2.  
  3. import fractal.*;
  4.  
  5. public class Koch extends Fractal {
  6. private int length;
  7.  
  8. /** Creates an object that handles Koch's fractal.
  9. * @param length the length of the triangle side
  10. */
  11. public Koch(int length) {
  12. super();
  13. this.length = length;
  14. }
  15.  
  16. /**
  17. * Returns the title.
  18. * @return the title
  19. */
  20. public String getTitle() {
  21. return "Kochs triangel";
  22. }
  23.  
  24. /** Draws the fractal.
  25. * @param turtle the turtle graphic object
  26. */
  27. public void draw(TurtleGraphics turtle) {
  28. turtle.moveTo(turtle.getWidth() / 2.0 - length / 2.0,
  29. turtle.getHeight() / 2.0 + Math.sqrt(3.0) * length / 4.0);
  30. fractalLine(turtle, order,length,0);
  31. fractalLine(turtle, order,length,120);
  32. fractalLine(turtle, order,length,240);
  33. }
  34.  
  35. /*
  36. * Recursive method: Draws a recursive line of the triangle.
  37. */
  38. private void fractalLine(TurtleGraphics turtle, int order, double length, int alpha) {
  39. if (order == 0) {
  40. turtle.setDirection(alpha);
  41. turtle.forward(length);
  42. } else {
  43. fractalLine(turtle, order-1, length/3, alpha);
  44. fractalLine(turtle, order-1, length/3, alpha-60);
  45. fractalLine(turtle, order-1, length/3, alpha+60);
  46. fractalLine(turtle, order-1, length/3, alpha);
  47. }
  48. }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement