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;
}