Loading...
Logo
Processing Forum
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.
Copy code
  1. int _maxLevels = 3;
  2. int _numChildren = 3; 
  3. Branch _trunk;

  4. void setup() {
  5.   size(900, 900);
  6.   smooth();
  7.   _trunk = new Branch(1, 0, width/2, 50);
  8.   _trunk.drawMe();
  9. }



  10. class Branch {
  11.   float level, index;
  12.   float x, y;
  13.   float endx, endy;
  14.   Branch[] children = new Branch[0];

  15.   Branch(float lev, float ind, float ex, float why) {
  16.     level = lev;
  17.     index = ind;
  18.     updateMe(ex, why);

  19.     if (level < _maxLevels) {
  20.       children = new Branch[_numChildren];
  21.       for (int x = 0; x < _numChildren; x++) {
  22.         children[x] = new Branch(level+1, x, endx, endy);
  23.       }
  24.     }
  25.   }

  26.   void updateMe(float ex, float why) {
  27.     x = ex;
  28.     y = why;
  29.     endx = x + (level * random(100) - 50);
  30.     endy = y + 50 + (level * random(50));
  31.   }

  32.   void drawMe() {
  33.     strokeWeight(_maxLevels - level + 1);
  34.     line(x, y, endx, endy);
  35.     ellipse(x, y, 5, 5);
  36.     for (int i = 0; i < children.length; i++) {
  37.       children[i].drawMe();
  38.     }
  39.   }
  40. }

Replies(3)

Simply make the drawing depend on the level of recursion.
You're actually sort of doing it already, because you use the level to determine the stroke weight.
You can use a switch if things are quite different at different levels:

Copy code
  1. int _maxLevels = 3;
  2. int _numChildren = 3; 
  3. Branch _trunk;

  4. void setup() {
  5.   size(900, 900);
  6.   smooth();
  7.   _trunk = new Branch(1, 0, width/2, 50);
  8.   _trunk.drawMe();
  9. }



  10. class Branch {
  11.   float level, index;
  12.   float x, y;
  13.   float endx, endy;
  14.   Branch[] children = new Branch[0];

  15.   Branch(float lev, float ind, float ex, float why) {
  16.     level = lev;
  17.     index = ind;
  18.     updateMe(ex, why);

  19.     if (level < _maxLevels) {
  20.       children = new Branch[_numChildren];
  21.       for (int x = 0; x < _numChildren; x++) {
  22.         children[x] = new Branch(level+1, x, endx, endy);
  23.       }
  24.     }
  25.   }

  26.   void updateMe(float ex, float why) {
  27.     x = ex;
  28.     y = why;
  29.     endx = x + (level * random(100) - 50);
  30.     endy = y + 50 + (level * random(50));
  31.   }

  32.   void drawMe() {
  33.     switch(int(level)){
  34.       case 1:
  35.         noStroke();
  36.         fill(0);
  37.         ellipse(x,y,20,20);
  38.         stroke(255,0,0);
  39.         strokeWeight(3);
  40.         line(x, y, endx, endy);
  41.         break;
  42.       case 2:
  43.         noStroke();
  44.         fill(255,255,0);
  45.         rect(x-5,y-5,10,10);
  46.         stroke(0,255,0);
  47.         strokeWeight(2);
  48.         line(x, y, endx, endy);
  49.         break;
  50.       case 3:
  51.         stroke(0,0,255);
  52.         strokeWeight(1);
  53.         line(x, y, endx, endy);
  54.         break;
  55.       default:
  56.         stroke(0);
  57.         strokeWeight(1);
  58.         line(x,y,endx,endy);
  59.         break;
  60.     } // End case statement.    
  61.     for (int i = 0; i < children.length; i++) {
  62.       children[i].drawMe();
  63.     }
  64.   }
  65. }
Thank you, i knew that i should use the level on that. The thing that i am confused about was the children[i] is actually a branch so it has the same draw method, and if i needed another children class and so on. I had an infinite loop on my mind myself. (:

Thanks again it worked like a charm.
a more elegant way to do it would be to derive different classes from Branch with different drawing methods. all that would change then is the way you put together your nested list of branches together (which you currently do in the constructor of branch itself (which is a bit unusual) 


www.morishuz.com
www.videoreactive.com
www.electrovision-cinema.com