Float array "limitation"?

Hi there!

I’m just playing around with arrays, learning how to work with them, but there’s one problem happening and I can’t figure it out why

The program is written as follows:

float[] bar = new float[100];

void setup() {
  size(1080, 1080);
  for (int i = 0; i < bar.length; i++) {
    bar[i] = random(200,height-200);
  }
}

void draw() {
  float x = 0;
  
  background(0);
  fill(255);
  stroke(255);

  for (int i = 0; i < bar.length; i++) {
    float spacing = width / bar.length;
    rect(x, 0, spacing, bar[i]);
    x = x + spacing;
  }
}

void mousePressed() {
  for (int i = 0; i < bar.length; i++) {
    bar[i] = random(200,height-200);
  }
}

it should draw these white bars all around the canvas size, but it is not working well when I set widths and heights higher than 1000 (if it’s 1080, it only works as planned if I change the array’s index to a maximum of 45 elements).

Thats the result with the missing part:

Hi @boytedinho,

Try changing this line

float spacing = width / float(bar.length);

Btw. You don’t need to calculate that inside the loop…

Cheers
— mnse

PS: Troubleshooting · processing/processing Wiki · GitHub

2 Likes

Thank you so much for the answer and tips, @mnse ! You opened my eyes for that kind of problem. Really helpful links too. Saving them.

1 Like