andrewm@0: /* andrewm@0: * Utilities.cpp andrewm@0: * andrewm@0: * Created on: Oct 27, 2014 andrewm@0: * Author: parallels andrewm@0: */ andrewm@0: andrewm@0: #include "../include/Utilities.h" andrewm@0: andrewm@0: // map() andrewm@0: // andrewm@0: // Scale an input value from one range to another. Works like its Wiring language equivalent. andrewm@0: // x is the value to scale; in_min and in_max are the input range; out_min and out_max andrewm@0: // are the output range. andrewm@0: andrewm@0: float map(float x, float in_min, float in_max, float out_min, float out_max) andrewm@0: { andrewm@0: return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; andrewm@0: } andrewm@0: andrewm@0: // constrain() andrewm@0: // andrewm@0: // Clips an input value to be between two end points andrewm@0: // x is the value to constrain; min_val and max_val are the range andrewm@0: andrewm@0: float constrain(float x, float min_val, float max_val) andrewm@0: { andrewm@0: if(x < min_val) return min_val; andrewm@0: if(x > max_val) return max_val; andrewm@0: return x; andrewm@0: }