Loading...
Logo
Processing Forum

ArrayList reverse

in Programming Questions  •  7 months ago  
I can't figure out why my tree won't reverse. After it grows I want it to disappear in the opposite direction. Code is Daniel Shiffmans tree example and I used his Branch class. 




Copy code
  1. ArrayList a;

    float r= random (1,5);
    boolean remove=false; 
    boolean finish=false; 

    void setup() {
      size(600,600); 
      background(255);
      smooth();
     
      // Setup the arraylist and add one branch to it
      a = new ArrayList();

     Branch b= new Branch (new PVector (100, 300), new PVector (0.5f,-0.5f),300);
      // Add to arraylist


      a.add(b);
     
    }

    void draw() {
      strokeWeight(.3f);
      stroke(0);

        for (int i = a.size()-1; i >= 0; i--) {
          // Get the branch, update and draw it
          
          Branch b = (Branch) a.get(i);
          b.update();
          b.render();
          // If it's ready to split
          if (b.timeToBranch()) {
            a.remove(i);             // Delete it
            a.add(b.branch( 40f));   // Add one going right
            a.add(b.branch(-40f));   // Add one going left
            a.add(b.branch(-70f)); 
            a.add(b.branch(50f));
          // strokeWeight(100/a.size(1));
            
          }}
         
          

     if (a.size()>=200)remove =true;

        if (a.size()>=1) {
          if (remove) {
            for(int i=a.size()-1; i>=0; i++) {
    //        
            a.remove(i);
            
            
            
         
      }

      }}}

Replies(6)

Re: ArrayList reverse

7 months ago

maybe you need
Copy code
  1. background (0);
at beginning of draw ()

Re: ArrayList reverse

7 months ago
I can't run the code without the Branch class, can you include it?

Re: ArrayList reverse

7 months ago
class Branch {
  // Each has a location, velocity, and timer 
  // We could implement this same idea with different data
  PVector start;
  PVector end;
  PVector vel;
  float timer;
  float timerstart;
  float endX, endY;
  float bLength;
  float angRot;
//float r=random(100, 500);
  boolean growing = true;

  Branch(PVector l, PVector v, float n) {
    start = l.get();
    end = l.get();
    vel = v.get();
    timerstart = n;
    timer = timerstart;

  }

  // Move location
  void update() {
    if (growing) {
      end.add(vel); PVector start; 
    }
  }

  // Draw a dot at location
  void render() {
    stroke(0);
    line(start.x,start.y,end.x,end.y);
  }
   boolean timeToBranch() {
    timer--;
    if (timer < 0 && growing) {
      growing = false;
      return true;
    } 
    else {
      return false;
    }
  }

  // Create a new branch  the current location, but chge direction by a given angle
  Branch branch(float angle) {
    // What is my current heading
    float theta = vel.heading2D();
    // What is my current speed
    float mag = vel.mag();
    // Turn me
    theta += radians(angle);
    // Look, polar coordinates to cartesian!!
    PVector newvel = new PVector(mag*cos(theta),mag*sin(theta));
 // new PVector = PVector (mag*cos (theta), mag *sin (theta));
    // Return a new Branch
    return new Branch(end,newvel,timerstart*0.6f);
   
    
  }
  
}





Re: ArrayList reverse

7 months ago
This works, although you might notice that keeping 200 causes weird things to happen quickly. Basically certain branches will end up having some number of children and all the other ones are deleted.

It might be better to remove branches that are below some depth instead of some number of them. If the first branch had a depth of 1 and a branch with a depth of 4 is drawn, then maybe it would make sense to delete just the branch with depth 1. When you make branches at depth 5 remove branches at depth 2, etc.
Copy code
  1. ArrayList<Branch> a; // Best to cast as the kind of data the ArrayList holds

  2. void setup() {
  3.   size(600, 600); 
  4.   background(255);
  5.   strokeWeight(.3f);
  6.   smooth();

  7.   a = new ArrayList<Branch>();
  8.   Branch b = new Branch(new PVector(100, 500), new PVector(0.5f, -0.5f), 300);
  9.   a.add(b);
  10. }

  11. void draw() {  
  12.   // Notice how this for loop goes backwards
  13.   for (int i = a.size()-1; i >= 0; i--) {
  14.     Branch b = a.get(i);
  15.     b.update();
  16.     b.render();

  17.     if (b.timeToBranch()) {
  18.       a.remove(i);
  19.       a.add(b.branch(-60));
  20.       a.add(b.branch(-30));
  21.       a.add(b.branch(30)); 
  22.       a.add(b.branch(60));
  23.     }
  24.   }

  25.   // A condition that counts depth would be better
  26.   if (a.size() > 200) {
  27.     int numToRemove = a.size()-200;
  28.     for (int i = numToRemove-1; i >= 0; i--) a.remove(i);
  29.   }
  30. }

  31. class Branch {
  32.   PVector start;
  33.   PVector end;
  34.   PVector vel;
  35.   float timer;
  36.   float timerstart;
  37.   boolean growing = true;

  38.   Branch(PVector l, PVector v, float n) {
  39.     start = l.get();
  40.     end = l.get();
  41.     vel = v.get();
  42.     timerstart = n;
  43.     timer = timerstart;
  44.   }

  45.   void update() {
  46.     if (growing) end.add(vel);
  47.   }

  48.   void render() {
  49.     line(start.x, start.y, end.x, end.y);
  50.   }

  51.   boolean timeToBranch() {
  52.     timer--;
  53.     if (timer < 0 && growing) {
  54.       growing = false;
  55.       return true;
  56.     } 
  57.     else {
  58.       return false;
  59.     }
  60.   }

  61.   Branch branch(float angle) {
  62.     float theta = vel.heading2D();
  63.     float mag = vel.mag();
  64.     theta += radians(angle);
  65.     PVector newvel = new PVector(mag*cos(theta), mag*sin(theta));
  66.     return new Branch(end, newvel, timerstart*0.6f);
  67.   }
  68. }

Re: ArrayList reverse

7 months ago
Thanks for the help! I'll try to play around with it because for some reason it still seems to be growing on top of what I've grown instead of reversing. 

Re: ArrayList reverse

7 months ago
Well, the background is never cleared. If you add background() to the beginning of draw() it might be more like what you want.