/////////////////////////////////////////////////// // // Useful value rescaling and clipping functions // #include "mapFunctions.h" // map value from [low1, high2] -> [low2, high2] // double map(double value, double low1, double high1, double low2, double high2) { double denom, a, b; denom = high1 - low1; a = (high2 - low2)/denom; b = (high1 * low2 - high2 * low1)/denom; return a * value + b; } // returns the parameters a and b necessary to do a linear mapping: a * value + b // that would be achieved by the map function above. This allows for these // to be precomputed. void mapParmeters(double low1, double high1, double low2, double high2, double &a, double &b) { double denom; denom = high1 - low1; a = (high2 - low2)/denom; b = (high1 * low2 - high2 * low1)/denom; return; } // normalizes value // that is maps a number between low and high to between 0 and 1 // returns map(value, low, high, 0, 1) // double norm(double value, double low, double high) { return (value-low)/(high-low); } // inverse norm called lerp(low, high, value) in Processing // better name is unNorm()? (tags: lerp lrep lper lepr) // returns map(value, 0, 1, low, high) // double unNorm(double value, double low, double high) { return value * (high-low) + low; } // pin value between low and high // double clip(double value, double low, double high) { if (value>high) return high; if (valuehigh) return high; if (value= 0 for all value with modulus>0. This is unfortunately not true for // the C mod function double realMod(double value, double modulus) { return value-floor(value/modulus)*modulus; } // this mod function goes up and down like sin rather than sawtooth. // that is there are no discontinuities. Not the same as mod at all. // WARNING: cycleMod(m, m) = m ! double cycleMod(double value, double modulus) { return modulus - fabs(realMod(value, 2*modulus) - modulus); } // pin value between low and high using a continuous cyclical mode // double cycleClip(double value, double low, double high) { return cycleMod(value - low, high-low) + low; }