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 › oscillate revisited
Page Index Toggle Pages: 1
oscillate revisited (Read 580 times)
oscillate revisited
Oct 8th, 2009, 9:40am
 
BenHem gave me a basic algorithm like the one below for moving back and forth between boundaries. The slight problem I have is that the value actually goes beyond the boundaries. Of course one can state nominal boundaries that are slightly less, but that also means that one cannot start with the actual boundary value, since the value will never get back within the boundaries. Is there a simple solution to this?

float dir=1;
float maxValue = 99; // specify one less than needed
float minValue = 1; // specify one more than needed
float value = 1; // must start with nominal min
float incr = 1;

void draw() {
 if ((value > (maxValue)) || (value < (minValue))) {
   dir = -dir;
 }
 value += incr * dir;
 println(value);
}
Re: oscillate revisited
Reply #1 - Oct 8th, 2009, 9:47am
 
Can't you use <= and >= instead of < and > Questioning
Re: oscillate revisited
Reply #2 - Oct 8th, 2009, 10:50am
 
PhilLo's suggestion will work in this example because your increment value is 1...but if it were larger, and you still needed to absolutely protect the value from exceeding those bounds, you would need to reverse "dir" not:
"if the value is already beyond the boundaries"
but:
"if what the value is about to become is beyond the boundaries"

so (if value + dir * incr <= boundary) ....

--Ben
Page Index Toggle Pages: 1