We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Looping a sequence of numbers
Page Index Toggle Pages: 1
Looping a sequence of numbers (Read 1276 times)
Looping a sequence of numbers
Jan 3rd, 2010, 12:13pm
 
Hi,

I'm looking through the reference and still can't find a handy way to do this:

Basically, I've an array of 8 numbers.
I've got an if clause that states if i is true, check if abs(i-4) is false.
As an example: if array[7] is true, check if array[abs(7-4)], which is array[3] is false.

This works except that once the result of the subtraction goes past 0, it's out of bounds. I want it to loop back to 7. Eg, the sequence, if counting, would go: 3,2,1,0,7,6,5,4, etc...

I can think of a few long winded ways of doing this, but I'm trying to avoid doing these as they may lead to a big mess of code further down the road.

The section of the code this is relavant to is:
Code:

for (int i=0; i < 8; i++) {
         if (layerOne[i] == true) {
           if (layerTwo[i*2] == true) {
             if (layerOne[abs(i-4)] == false) {
               checkOne = true; // theres 3 pixels in a row, starting at currColor, but not going the other way.
               ellipse(x,y,5,5);
             }
           }
         }


Thanks.
Re: Looping a sequence of numbers
Reply #1 - Jan 3rd, 2010, 1:19pm
 
does that work ?

int count = 0;
int count2;
void setup() {
frameRate(5);
}

void draw() {


count2 = count-4;
print(count+ " : ");

if(count2<0)count2 = count2+8;
println(count2);


count++;
if(count>=8)count=0;

}

Re: Looping a sequence of numbers
Reply #2 - Jan 3rd, 2010, 1:42pm
 
Hi Cedric, thanks for the input.

I'm fine with the concept of putting in another if statement that says if i < 0, i = 7 and so on and so forth. I can  solve this problem already in long-winded ways, but that's a problem in itself and why I wrote this question. I'm hoping I've missed a really simple way of doing this before I start doubling up my code just to get numbers to loop back to the end of a sequence.

Thanks again.
Re: Looping a sequence of numbers
Reply #3 - Jan 3rd, 2010, 2:02pm
 
This is the shortest way of doing it that I can think of. It's similar to what Cedric wrote and not as long as what I was trying earlier, so it might do. What I was really hoping for was that I'd missed an inbuilt function (like how I've been working out distance between 2 points up to recently without realising there's an in-built dist() function).

Anyway, here it is:

Code:

for (int i=0; i < 8; i++) {
if (layerOne[i] == true) {
if (layerTwo[i*2] == true) {
if (layerOne[sequenceLooper(i)] == false) {
checkOne = true;
ellipse(x,y,5,5);
}
}
}
}

int sequenceLooper(int i) {
i = i-4;
if (i<0) {
i = i+8;
}
return i;
}
Re: Looping a sequence of numbers
Reply #4 - Jan 3rd, 2010, 4:14pm
 
Something like:
Code:
for (int i = 0; i < 8; i++) {
 if (layerOne[i] && layerTwo[i*2] && !layerOne[(i + 4) % 8]) {
   checkOne = true;
   ellipse(x,y,5,5);
 }
}
Questioning
(untested)
Re: Looping a sequence of numbers
Reply #5 - Jan 4th, 2010, 1:32pm
 
Aha! I'm unfamiliar with this remainder/modulus thing you just put in. This could be exactly what I'm after! Thanks!
Re: Looping a sequence of numbers
Reply #6 - Jan 4th, 2010, 1:39pm
 
That seems to work perfectly at the moment, thanks again PhiLho. It's been so long since I thought about the concept of remainders...  remind me if this is correct: 120 divided by 100 is 1 times, remainder 20. Whereas 20 divided by 100 is 0 times, remainder 20. Correct?
Re: Looping a sequence of numbers
Reply #7 - Jan 5th, 2010, 2:51am
 
Yes.

Modulo, ie. %, is a very convenient, terse way to cycle through a series of numbers.
Re: Looping a sequence of numbers
Reply #8 - Jan 5th, 2010, 4:56am
 
Great, I'll keep it in mind. Thanks again.
Page Index Toggle Pages: 1