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 › Using % (modulo) to constrain a number
Page Index Toggle Pages: 1
Using % (modulo) to constrain a number (Read 612 times)
Using % (modulo) to constrain a number
Oct 22nd, 2009, 2:19am
 
Hello,
I tried to use % in an attempt to constrain x and y inside the window. This did not turn out very well, mostly because mod in Processing includes the sign in the remainder. That is, when my x value goes negative, the modulo operation does not return a positive remainder, thus kept it inside my window. I ended up using the modified if-else version with the use of a couple of conditionals. I thought the use of % would work well for constraing, but now I find this not to be true. Or am I wrong on this?

Here is my code (not working):
Code:

x = x % width; // I also tried translating, and scaling, and though I believe there must be a way to do it using modolus, I could not figure it out this time ...


... but this one works as intended ...
Code:

x = (x<minX) ? maxX : (x>maxX) ? minX : x;


Are there other ways, better ways, to do this?

Here is the complete working program in case anyone was wondering:
Code:

float x, y;
int maxX, maxY, minX, minY;
float rot, rotNoise;

void setup(){
 size (300,300);
 minX = 50;
 maxX = 250;
 minY = 50;
 maxY = 250;
 x = 150;
 y = 150;
 rot = random(TWO_PI);
//  noiseSeed(10);
}

void draw(){
//translate(150, 150);
 //  background(120,90,90);
 rotNoise = noise(rot);
// println("rotNoise = "+rotNoise);
 

 x+=cos(rotNoise*TWO_PI);
 y+=sin(rotNoise*TWO_PI);
 
   y = (y<minY) ? maxY : (y>maxY) ? minY : y;
 x = (x<minX) ? maxX : (x>maxX) ? minX : x;

 line(x,y, x+2, y+2);

 rot += TWO_PI/1300;

}
Re: Using % (modulo) to constrain a number
Reply #1 - Oct 22nd, 2009, 4:01am
 
Quote:
... but this one works as intended ...
x = (x<minX) ? maxX : (x>maxX) ? minX : x;
Are there other ways, better ways, to do this?

A problem with this is that you will always appear on the left (right) boundary no matter how far you went out the right (left) boundary in the frame before.

If you are sure that x will never be smaller than -width, you can do the following:

Code:
x = (x + width) % width; 


Adding width makes sure the left hand side of % is always positive, but it doesn't change the result if x was positive already.

With your minX and maxX:

Code:
x = minX + ((x - minX) + (maxX - minX)) % (maxX - minX); 

Re: Using % (modulo) to constrain a number
Reply #2 - Oct 22nd, 2009, 9:15am
 
Thanks! Your solution Code:
x = (x + width) % width; 

is doing exactly what I was after in the first place.

I am not sure I understand your comment about my ?: solution, though, as it seems to work perfect for its purpose. Ah, but now I understand what you are saying! I didn't quite get the 'on' until now ... Anyways, I'm not sure if that is a problem at all ... where else should it be? I guess if we are talking about making patterns and such, the modularity would be of importance, and thus it should be taken care of properly, but in this case, where I am only concerned with keeping a moving 'thing' inside a boundary, it is actually better, imo. Thanks for your feedback! Smiley
Page Index Toggle Pages: 1