Guest User

Untitled

a guest
Dec 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. /**
  2. * Simple recursive branching system inspired by mycelium growth
  3. *
  4. * This file is part of the SAC 2013 workshop project
  5. * (c) 2013 Karsten Schmidt
  6. * LGPLv3 licensed
  7. */
  8. import toxi.geom.*;
  9. import toxi.processing.*;
  10. import java.util.*;
  11.  
  12. // max segments per branch
  13. int MAX_LEN = 100;
  14. // max recursion limit
  15. int MAX_GEN = 3;
  16. // variance angle for growth direction per time step
  17. float THETA = PI/6;
  18. // branch chance per time step
  19. float BRANCH_CHANCE = 0.05;
  20. // branch angle variance
  21. float BRANCH_THETA = PI/3;
  22.  
  23. ToxiclibsSupport gfx;
  24. Branch root;
  25.  
  26. // switch to ensure growth remains in window bounds
  27. boolean doWrap = true;
  28.  
  29. void setup() {
  30. size(1280, 600);
  31. gfx = new ToxiclibsSupport(this);
  32. root = new Branch(new Vec2D(0, height/2), new Vec2D(1, 0), 10, THETA);
  33. }
  34.  
  35. void draw() {
  36. background(0);
  37. stroke(255);
  38. noFill();
  39. root.grow();
  40. root.draw();
  41. }
  42.  
  43. class Branch {
  44. Vec2D currPos;
  45. Vec2D dir;
  46. List<Vec2D> path = new ArrayList<Vec2D>();
  47. List<Branch> children = new ArrayList<Branch>();
  48.  
  49. float speed;
  50. float theta;
  51.  
  52. Branch(Vec2D p, Vec2D d, float s, float t) {
  53. currPos = p;
  54. dir = d;
  55. speed = s;
  56. theta = t;
  57. path.add(p.copy());
  58. }
  59.  
  60. void grow() {
  61. if (path.size() < MAX_LEN) {
  62. if (doWrap) {
  63. Vec2D newPos = currPos.add(dir.scale(speed));
  64. if (newPos.x < 0 || newPos.x > width) dir.x *= -1;
  65. if (newPos.y < 0 || newPos.y > height) dir.y *= -1;
  66. }
  67. currPos.addSelf(dir.scale(speed));
  68. dir.rotate(random(-0.5, 0.5) * THETA);
  69. path.add(currPos.copy());
  70. if (children.size() < MAX_GEN && random(1) < BRANCH_CHANCE) {
  71. Vec2D branchDir = dir.getRotated(random(-0.5, 0.5) * BRANCH_THETA);
  72. Branch b = new Branch(currPos.copy(), branchDir, speed * 0.99, theta);
  73. children.add(b);
  74. }
  75. }
  76. for (Branch c : children) {
  77. c.grow();
  78. }
  79. }
  80.  
  81. void draw() {
  82. gfx.lineStrip2D(path);
  83. for (Branch c : children) {
  84. c.draw();
  85. }
  86. }
  87. }
Add Comment
Please, Sign In to add comment