  | 
    
 
  
    
    
      
        
          
         Author | 
        
         Topic: Constraining a value to a range   (Read 754 times) | 
       
     
     | 
   
 
  
    
    
      
        
        
          
            
            REAS 
 
 
        
      
             | 
            
            
            
              
                
                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);    } 
            
             | 
           
            | 
            
            
            
             | 
           
         
         | 
       
     
     | 
   
 
 
 |