Using array index in conditionals--is this good programming practice?
in
Programming Questions
•
2 years ago
I have twelve shapes (int numShapes = 12). The RGB color values of each shape are stored in arrays, cRed[], cGreen[], cBlue[], and are changed every time the program loops through the draw() function, thus animating the color changes.
MacBook Pro 15" 2.4GHz Intel Core i5, QuickTime v.10.0
Processing v. 1.2.1
Right now, I am using a structure like this to draw the shapes:
for (int i = 0; i < numShapes; i ++) {
if( i == 0) {
fillColor(cRed[i], cGreen[i], cBlue[i]);
rect(x1, y1, wdth, ht);
}
if(i == 1) {
fillColor(cRed[i], cGreen[i], cBlue[i]);
rect(x2, y2, wdth, ht);
}
etc.
This works fine, but I have a nagging suspicion this is not good coding. Since each rect has a unique location and a constantly changing RGB value, I could not think of another technique for drawing the shapes.
Is this okay, or should I be using a different method?
Thanks,
George
MacBook Pro 15" 2.4GHz Intel Core i5, QuickTime v.10.0
Processing v. 1.2.1
1