Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. Tree root;
  2.  
  3. ArrayList<Tree> fullTree = new ArrayList<Tree>();
  4.  
  5. void setup() {
  6. fullScreen();
  7. PVector a = new PVector(width/2, height);
  8. PVector b = new PVector(width/2, height - 100);
  9. root = new Tree(a, b, PI/10, 20);
  10. background(51);
  11. root.show();
  12.  
  13. }
  14.  
  15. //void draw() {
  16. // background(51);
  17. //}
  18.  
  19.  
  20. class Tree {
  21.  
  22. private PVector start, end;
  23. private float angle;
  24. private int depth;
  25. private Tree left, right;
  26.  
  27.  
  28. public Tree(PVector start, PVector end, float angle, int depth) {
  29. this.start = start;
  30. this.end = end;
  31. this.depth = depth;
  32. this.angle = angle;
  33. if (this.depth >= 0) {
  34. this.left = this.branch("left");
  35. this.right = this.branch("right");
  36. }
  37.  
  38. }
  39.  
  40. public void show() {
  41. stroke(255);
  42. line(this.start.x, this.start.y, this.end.x, this.end.y);
  43. if (this.depth > 0) {
  44. this.left.show();
  45. this.right.show();
  46. }
  47.  
  48. }
  49.  
  50. public Tree branch(String direction) {
  51. int sign = -1;
  52. if (direction.equals("right")) {
  53. sign *= sign;
  54. }
  55. PVector dir = PVector.sub(this.end, this.start);
  56. dir.rotate(sign * this.angle);
  57. dir.mult(.82);
  58. PVector newEnd = PVector.add(this.end, dir);
  59. return new Tree(this.end, newEnd, this.angle, this.depth - 1);
  60. }
  61.  
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement