Last active
April 13, 2019 10:51
-
-
Save elliotthilaire/267cb0b907c36748f824670d9988bf5e to your computer and use it in GitHub Desktop.
Create a function that maps an input to a specific range
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule RangeMap do | |
def to_function(in_min, in_max, out_min, out_max) do | |
difference_in = in_max - in_min | |
difference_out = out_max - out_min | |
factor = difference_out / difference_in | |
midpoint_in = (in_min + in_max) / 2 | |
midpoint_out = (out_min + out_max) / 2 | |
constant = midpoint_out - midpoint_in | |
fn input -> | |
input * factor + constant | |
end | |
end | |
def map(input, input_min, input_max, output_min, output_max) do | |
factor = (output_max - output_min) / (input_max - input_min) | |
constant = (output_min + output_max) / 2 - (input_min + input_max) / 2 | |
input * factor + constant | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment