Quantcast
Viewing all articles
Browse latest Browse all 40524

Using 'for' to loop a loop

I am finally starting to get a hang of this stuff but I am stuck on this one.

I have a series of statements that create a array of balls that grow and shrink depending upon the mouse position. See code:

    for ( a = 0; a < 100 ; a++){
    ax1[a] = 5 + a * 30;
    ay1[a] = 35;
    ax2[a] = (abs(curserx)/30 - a);
    ofSetColor(abs(curserx-300),abs(cursery - 300),180);
    ofSetLineWidth(2);
    ofCircle(ax1[a],ay1[a],ax2[a]);
}
for ( a = 0; a < 100 ; a++){
    ax1[a] = 5 + a * 30;
    ay1[a] = 65;
    ax2[a] = (abs(curserx)/30 - a);
    ofSetColor(abs(curserx-300),abs(cursery - 300),180);
    ofSetLineWidth(2);
    ofCircle(ax1[a],ay1[a],ax2[a]);
}
for ( a = 0; a < 100 ; a++){
    ax1[a] = 5 + a * 30;
    ay1[a] = 95;
    ax2[a] = (abs(curserx)/30 - a);
    ofSetColor(abs(curserx-300),abs(cursery - 300),180);
    ofSetLineWidth(2);
    ofCircle(ax1[a],ay1[a],ax2[a]);
}
for ( a = 0; a < 100 ; a++){
    ax1[a] = 5 + a * 30;
    ay1[a] = 125;
    ax2[a] = (abs(curserx)/30 - a);
    ofSetColor(abs(curserx-300),abs(cursery - 300),180);
    ofSetLineWidth(2);
    ofCircle(ax1[a],ay1[a],ax2[a]);
}
for ( a = 0; a < 100 ; a++){
    ax1[a] = 5 + a * 30;
    ay1[a] = 155;
    ax2[a] = (abs(curserx)/30 - a);
    ofSetColor(abs(curserx-300),abs(cursery - 300),180);
    ofSetLineWidth(2);
    ofCircle(ax1[a],ay1[a],ax2[a]);
}

The problem is of course this is very long and repedative to get every line of circles. The next thing I tried is this:

for ( b = 5; b < 610 ; b + 30){
for ( a = 0; a < 100 ; a++){
    ax1[a] = 5 + a * 30;
    ay1[a] = 5+ax0[b];
    ax2[a] = (abs(curserx)/30 - a);
    ofSetColor(abs(curserx-300),abs(cursery - 300),180);
    ofSetLineWidth(2);
    ofCircle(ax1[a],ay1[a],ax2[a]);
}
}

But this just crashes the program immediately.

So basically my question is. Is there any way to define two variables within one loop? In this case 'a' and 'b' or can I define one variable in one loop then use another loop inside of that with the other variable?


Viewing all articles
Browse latest Browse all 40524

Trending Articles