Skip to content

Instantly share code, notes, and snippets.

@warpling
Last active July 28, 2017 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warpling/88b24f19697d495c3780640c77fc8953 to your computer and use it in GitHub Desktop.
Save warpling/88b24f19697d495c3780640c77fc8953 to your computer and use it in GitHub Desktop.
MAPRANGE
// Maps one range onto another
// Example: [-50, 100] to [0, 1.0]
// Example: [0.2, 1.0] to [1.0, 0.0]
#define MAPRANGE(x, inputLow, inputHigh, outputLow, outputHigh) ({\
__typeof(x) __x = (x); \
__typeof(inputLow) __inputLow = (inputLow); \
__typeof(inputHigh) __inputHigh = (inputHigh); \
__typeof(outputLow) __outputLow = (outputLow); \
__typeof(outputHigh) __outputHigh = (outputHigh); \
(((__x - __inputLow) / (__inputHigh - __inputLow)) * (__outputHigh - __outputLow)) + __outputLow; \
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment