FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Topics & Contributions
   Tools
(Moderator: REAS)
   Constraining a value to a range
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Constraining a value to a range  (Read 754 times)
REAS


WWW
Constraining a value to a range
« on: Oct 31st, 2002, 12:51am »

// Constrains a value to a specific range  
int mx;  
void loop() {  
  mx = constrain(mouseX, 20, 80);  
  line(mx, 20, mx, 80);  
}  
int constrain(int val, int minv, int maxv) {  
  return min(max(val, minv), maxv);  
}  
 
 
// The same example as above,  
// but executes faster with a trinary operator  
int mx;  
void loop() {  
  mx = constrain(mouseX, 20, 80);  
  line(mx, 20, mx, 80);  
}  
int constrain(int val, int minv, int maxv) {  
  return (val > maxv) ? maxv : ((val < minv) ? minv : val);  
}
 
Pages: 1 

« Previous topic | Next topic »