#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# For loop - storing previous value

edited April 2016

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
}
}
``````
Tagged:

## Answers

• Here is my progress - for some reason it draws more than 5 rects despite the for loop containing i<5

``````float[] rectWidths = {12.5, 3.7, 22.9, 77.3, 100.1};
float totalWidth;
float tY;

void setup() {
size(500, 375);
background(255);
colorMode(HSB);
}

void draw() {
for (int i = 0; i<5; i++) {
tY = map(rectWidths[i], min(rectWidths), max(rectWidths), 15, 60);
fill(255, 255, 255, 49);
rect(totalWidth, height/2, tY, random(20, 40));
totalWidth = totalWidth +  tY;
}
}
``````
• ``````float H=0;

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
H += map(rectWidths[i], min(rectWidths), max(rectWidths), 15, 60);
}
H = 0;
}
``````
• 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.

• ``````/**
* Mapped Width Rects (v1.01)
* GoToLoop (2016-Apr-01)
* forum.Processing.org/two/discussion/15806/for-loop-storing-previous-value
*/

static final int WMIN = 15, WMAX = 60, H = 20;
static final float[] WIDTHS = { 12.5, 23.7, 22.9, 77.3, 62.1 };
static final int[] MAPPED = round(map(WMIN, WMAX, WIDTHS));

void setup() {
size(180, 100);
smooth(4);
noLoop();

rectMode(CORNER);
strokeWeight(2.5);
stroke(0);
fill(#FFFF00);

println(WIDTHS);
println();
println(MAPPED);
}

void draw() {
background(0350);

int x = 5, y = height - H >> 1;
for (final int w : MAPPED) {
rect(x, y, w, H);
x += w;
}
}

static final float[] map(final float lo, final float up, final float... arr) {
if (arr == null || arr.length == 0)  return new float;

final float[] mapped = new float[arr.length];
final float mi = min(arr), ma = max(arr);
for (int i = 0; i < arr.length; mapped[i] = map(arr[i++], mi, ma, lo, up));

return mapped;
}

static final int[] round(final float... arr) {
if (arr == null)  return new int;

final int[] rounded = new int[arr.length];
for (int i = 0; i < arr.length; rounded[i] = round(arr[i++]));

return rounded;
}
``````
Sign In or Register to comment.