Controlling the Levels of Recursion
in
Programming Questions
•
6 months ago
Hello all,
I'd like to ask if there is a way to control the levels of recursion.
For example i want to change the appearance of the first branch. Then the other 3 childs have a different draw method. Then his childs have different draw methods.
How is that possible logically?
My code is down below;
Thanks in advance,
Met.
- int _maxLevels = 3;
- int _numChildren = 3;
- Branch _trunk;
- void setup() {
- size(900, 900);
- smooth();
- _trunk = new Branch(1, 0, width/2, 50);
- _trunk.drawMe();
- }
- class Branch {
- float level, index;
- float x, y;
- float endx, endy;
- Branch[] children = new Branch[0];
- Branch(float lev, float ind, float ex, float why) {
- level = lev;
- index = ind;
- updateMe(ex, why);
- if (level < _maxLevels) {
- children = new Branch[_numChildren];
- for (int x = 0; x < _numChildren; x++) {
- children[x] = new Branch(level+1, x, endx, endy);
- }
- }
- }
- void updateMe(float ex, float why) {
- x = ex;
- y = why;
- endx = x + (level * random(100) - 50);
- endy = y + 50 + (level * random(50));
- }
- void drawMe() {
- strokeWeight(_maxLevels - level + 1);
- line(x, y, endx, endy);
- ellipse(x, y, 5, 5);
- for (int i = 0; i < children.length; i++) {
- children[i].drawMe();
- }
- }
- }
1