SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. defmodule RangeMap do
  2.   def to_function(in_min, in_max, out_min, out_max) do
  3.     difference_in = in_max - in_min
  4.     difference_out = out_max - out_min
  5.     factor = difference_out / difference_in
  6.  
  7.     midpoint_in = (in_min + in_max) / 2
  8.     midpoint_out = (out_min + out_max) / 2
  9.     constant = midpoint_out - midpoint_in
  10.  
  11.     fn input ->
  12.       input * factor + constant
  13.     end
  14.   end
  15.  
  16.   def map(input, input_min, input_max, output_min, output_max) do
  17.     factor = (output_max - output_min) / (input_max - input_min)
  18.     constant = (output_min + output_max) / 2 - (input_min + input_max) / 2
  19.  
  20.     input * factor + constant
  21.   end
  22. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top