Skip to content

Instantly share code, notes, and snippets.

@warpling
Last active January 26, 2017 03:57
Show Gist options
  • Select an option

  • Save warpling/4841285b98e5524fa3f3 to your computer and use it in GitHub Desktop.

Select an option

Save warpling/4841285b98e5524fa3f3 to your computer and use it in GitHub Desktop.
MapRange Macro
// Maps one ascending range onto another
// Example: map a range like [-50, 100] to [0, 1.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