diff --git a/snippets/c/mathematical-functions/linear-mapping.md b/snippets/c/mathematical-functions/linear-mapping.md new file mode 100644 index 00000000..0d4e360b --- /dev/null +++ b/snippets/c/mathematical-functions/linear-mapping.md @@ -0,0 +1,18 @@ +--- +title: Linear Mapping +description: remaps a value from one range to another +author: JasimAlrawie +tags: math,number-theory,algebra +--- + +```c +float linearMapping(float value, float minIn, float maxIn, float minOut, float maxOut) { + return (value - minIn) * (maxOut - minOut)/(maxIn - minIn) + minOut; +} + + +// Usage: +linearMapping(value, 0, 1, 0, 255); // remaps the value from (0,1) to (0,255) +linearMapping(value, 0, PI*2, 0, 360); // remaps the value from rad to deg +linearMapping(value, -1, 1, 1, 8); // remaps the value from (-1,1) to (1,8) +``` \ No newline at end of file diff --git a/snippets/javascript/mathematical-functions/linear-mapping.md b/snippets/javascript/mathematical-functions/linear-mapping.md new file mode 100644 index 00000000..5baada59 --- /dev/null +++ b/snippets/javascript/mathematical-functions/linear-mapping.md @@ -0,0 +1,17 @@ +--- +title: Linear Mapping +description: remaps a value from one range to another +author: JasimAlrawie +tags: math,number-theory,algebra +--- + +```js +function linearMapping(value, minIn, maxIn, minOut, maxOut) { + return (value - minIn) * (maxOut - minOut)/(maxIn - minIn) + minOut +} + +// Usage: +linearMapping(value, 0, 1, 0, 255) // remaps the value from (0,1) to (0,255) +linearMapping(value, 0, PI*2, 0, 360) // remaps the value from rad to deg +linearMapping(value, -1, 1, 1, 8) // remaps the value from (-1,1) to (1,8) +``` \ No newline at end of file diff --git a/snippets/python/math-and-numbers/linear-mapping.md b/snippets/python/math-and-numbers/linear-mapping.md new file mode 100644 index 00000000..3f7f5daf --- /dev/null +++ b/snippets/python/math-and-numbers/linear-mapping.md @@ -0,0 +1,16 @@ +--- +title: Linear Mapping +description: remaps a value from one range to another +author: JasimAlrawie +tags: math,number-theory,algebra +--- + +```py +def linear_mapping(value, min_in, max_in, min_out, max_out): + return (value - min_in) * (max_out - min_out) / (max_in - min_in) + min_out + +#Usage: +linear_mapping(value, 0, 1, 0, 255) # remaps the value from (0,1) to (0,255) +linear_mapping(value, 0, PI*2, 0, 360) # remaps the value from rad to deg +linear_mapping(value, -1, 1, 1, 8) # remaps the value from (-1,1) to (1,8) +``` \ No newline at end of file