Issues with arrayLists2

Hi! I am doing a customizable fractal tree program. I have some issues; Why does this happen?

why this (“kinda”) works:

ArrayList<Float> x = new ArrayList<Float>();
ArrayList<Float> y = new ArrayList<Float>();
ArrayList<Float> a = new ArrayList<Float>();

ArrayList<Float> nx = new ArrayList<Float>();
ArrayList<Float> ny = new ArrayList<Float>();
ArrayList<Float> na = new ArrayList<Float>();
int n = 3, it = 0, mit = 5;
float spin = PI/6, br = -PI/2, bl = 100, bm = 0.9;
float spn[] = new float[n];

void setup() {
  size(600,600);
  
  for(int i = 0; i < n; i++) {
    spn[i] = map(i,0,n-1,-spin/2,spin/2);
  }
  newBranch(300,500,br);
}

void draw() {
  
  
  
  //x = nx;
  //y = ny;
  //a = na;
  
  //nx.clear();
  //ny.clear();
  //na.clear();
  
  background(0);
  for(int i = 0; i < pow(n,it); i++) {
    branch(x.get(i),y.get(i),a.get(i), bl * pow(bm,1));
  }
  println(frameCount);
  delay(1000);
  it++;
  
}

void branch(float x_, float y_, float r_, float l_) {
  stroke(255);
  for(int i = 0; i < n; i++) {
    line(x_,y_, x_ + l_ * cos(r_ + spn[i]), y_ + l_ * sin(r_ + spn[i]));
    newBranch(x_ + l_ * cos(r_ + spn[i]), y_ + l_ * sin(r_ + spn[i]), r_ + spn[i]);
  }
}

void newBranch(float x_, float y_, float r_) {
  x.add(x_);
  y.add(y_);
  a.add(r_);
}

while this doesn’t:

ArrayList<Float> x = new ArrayList<Float>();
ArrayList<Float> y = new ArrayList<Float>();
ArrayList<Float> a = new ArrayList<Float>();

ArrayList<Float> nx = new ArrayList<Float>();
ArrayList<Float> ny = new ArrayList<Float>();
ArrayList<Float> na = new ArrayList<Float>();
int n = 3, it = 0, mit = 5;
float spin = PI/6, br = -PI/2, bl = 100, bm = 0.9;
float spn[] = new float[n];

void setup() {
  size(600,600);
  
  for(int i = 0; i < n; i++) {
    spn[i] = map(i,0,n-1,-spin/2,spin/2);
  }
  newBranch(300,500,br);
}

void draw() {
  
  
  
  x = nx;
  y = ny;
  a = na;
  
  nx.clear();
  ny.clear();
  na.clear();
  
  background(0);
  for(int i = 0; i < pow(n,it); i++) {
    branch(x.get(i),y.get(i),a.get(i), bl * pow(bm,1));
  }
  println(frameCount);
  delay(1000);
  it++;
  
}

void branch(float x_, float y_, float r_, float l_) {
  stroke(255);
  for(int i = 0; i < n; i++) {
    line(x_,y_, x_ + l_ * cos(r_ + spn[i]), y_ + l_ * sin(r_ + spn[i]));
    newBranch(x_ + l_ * cos(r_ + spn[i]), y_ + l_ * sin(r_ + spn[i]), r_ + spn[i]);
  }
}

void newBranch(float x_, float y_, float r_) {
  nx.add(x_);
  ny.add(y_);
  na.add(r_);
}

image

Can you check if the handover of the values to x, y and a works, before you go into the first for loop in draw by printing the values of the lists?

I thought first your ArrayList is empty, but you create some values in setup for the first run.

Could it be your lists don’t copy, because your x, y, a is empty?

it works now but I still have the problem of uneven spawning : (

the new code looks like this

ArrayList<Float> x = new ArrayList<Float>();
ArrayList<Float> y = new ArrayList<Float>();
ArrayList<Float> a = new ArrayList<Float>();

ArrayList<Float> nx = new ArrayList<Float>();
ArrayList<Float> ny = new ArrayList<Float>();
ArrayList<Float> na = new ArrayList<Float>();
int n = 3, it = 0, mit = 5;
float spin = PI/2, br = -PI/2, bl = 100, bm = 0.9;
float spn[] = new float[n];

void setup() {
  size(600,600);
  
  for(int i = 0; i < n; i++) {
    spn[i] = map(i,0,n-1,-spin/2,spin/2);
  }
  newBranch(300,500,br);
}

void draw() {
  
  println("1. ",frameCount,x.size(),nx.size());
  
  for(int i = 0; i < nx.size() && x.size() > 0; i++) {
    x.set(i,nx.get(i));
    y.set(i,ny.get(i));
    a.set(i,na.get(i));
  }
  
  println("2. ",frameCount,x.size(),nx.size());
  
  background(0);
  for(int i = 0; i < pow(n,it); i++) {
    branch(x.get(i),y.get(i),a.get(i), bl * pow(bm,1));
  }
  println(frameCount);
  delay(1000);
  it++;
  
  println("3. ",x.size(),nx.size());
  
}

void branch(float x_, float y_, float r_, float l_) {
  stroke(255);
  for(int i = 0; i < n; i++) {
    line(x_,y_, x_ + l_ * cos(r_ + spn[i]), y_ + l_ * sin(r_ + spn[i]));
    if(x.contains(x_ + l_ * cos(r_ + spn[i])) && y.contains(y_ + l_ * sin(r_ + spn[i]))) {
    } else {
      newBranch(x_ + l_ * cos(r_ + spn[i]), y_ + l_ * sin(r_ + spn[i]), r_ + spn[i]);
    }

  }
}

void newBranch(float x_, float y_, float r_) {
  //nx.add(x_);
  //ny.add(y_);
  //na.add(r_);
  x.add(x_);
  y.add(y_);
  a.add(r_);
}

image

Your for-loop for the branches goes from 0 to pow(3,0), which is 1.
If you do the loop from pow(3,0)-1 you should see all three branches. For it=1 it then goes from 0-2 and not 0-1 a.s.o.

Edit: Ignore above, that shouldn’t make a difference. It must have something to do with your new branch generation but I would need to run it to see it. I look, if I can do that later tonight.

I think I found the issue. I think it was, that you, when calculating new coordinates went from 0 to pow(n,it). In the second round you go from x(0), x(1), x(2) but x(3), right side coordinate is not used, because the source coordinate is counted again.

Here some the code that fixes that:

ArrayList<Float> x = new ArrayList<Float>();
ArrayList<Float> y = new ArrayList<Float>();
ArrayList<Float> a = new ArrayList<Float>();

ArrayList<Float> nx = new ArrayList<Float>();
ArrayList<Float> ny = new ArrayList<Float>();
ArrayList<Float> na = new ArrayList<Float>();
int n = 3, it = 0, mit = 5;
float spin = PI/2, br = -PI/2, bl = 30, bm = 0.9;
float spn[] = new float[n];

void setup() {
  size(600,600);
  
  for(int i = 0; i < n; i++) {
    spn[i] = map(i,0,n-1,-spin/2,spin/2);
  }
  newBranch(300,500,br);
}

void draw() {
  background(0);
  
  for(int i = x.size()-int(pow(n,it)); i < x.size(); i++) {
    branchupdate(x.get(i),y.get(i),a.get(i), bl * pow(bm,1));
  }
  
  for(int i = 0; i < x.size(); i++) {
    branch(x.get(i),y.get(i),a.get(i), bl * pow(bm,1));
  }
  delay(1000);
  it++;
  
  for (int i = 0; i < nx.size();i++){
  x.add(nx.get(i));
  y.add(ny.get(i));
  a.add(na.get(i));
  }
  
  na.clear();
  nx.clear();
  ny.clear();
  
}

void branch(float x_, float y_, float r_, float l_) {
  stroke(255);
  for(int i = 0; i < n; i++) {
    line(x_,y_, x_ + l_ * cos(r_ + spn[i]), y_ + l_ * sin(r_ + spn[i]));
  }
}

void branchupdate(float x_, float y_, float r_, float l_){
  for(int i = 0; i < n; i++) {
    nx.add(x_ + l_ * cos(r_ + spn[i]));
    ny.add(y_ + l_ * sin(r_ + spn[i]));
    na.add(r_ + spn[i]);
  }
}

void newBranch(float x_, float y_, float r_) {
  x.add(x_);
  y.add(y_);
  a.add(r_);
}

As you see, the new coordinate calculation starts now at x.size-pow(n,it) and it ends at x.size.
To be able to do that, I have to use the n ArrayList as temporary memory, as you had planned.

thanks. I tried to fix the changing of length issue but I realized I can just create a vertified_x variable and place all the old branches in it. Thanks : )