We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I am trying to solve how to do the following:
I have an array of numbers I want to map each value in the array between 15 and 60 I want to draw a number of rects side by side where each rect represents a number in the array of numbers The width of each rect will be the mapped value of it's corresponding number in the array.
How do I solve the Y position of each rect? I assume I need to find a way of saving the accumulative width of each rect and then place the new rect at the position?
float[] rectWidths = {12.5, 23.7, 22.9, 77.3, 62.1};
void setup() {
size(500, 375);
}
void draw() {
background(255);
for (int i = 0; i<rectWidths.length; i++) {
rect(H, height/2, map(rectWidths[i], min(rectWidths), max(rectWidths), 15, 60), 20); //H needs to be the accumulative width of each rect
}
}
Answers
Here is my progress - for some reason it draws more than 5 rects despite the for loop containing i<5
That's because the "for" loop is executed everytime draw() loops. And if you look at your code carefully, totalWidth is incrementing infinitely!
You need to reset this variable's value upon completion of the "for" loop to avoid that, as I have show in the code above.