Created
November 22, 2016 10:07
-
-
Save jnsprnw/d09361009f8b9d2364ae8768e77f3af1 to your computer and use it in GitHub Desktop.
map a value from a range to a different domain
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
class DomainMap | |
def setDomain(v) | |
@minV = v[0].to_f | |
@maxV = v[1].to_f | |
self | |
end | |
def setRange(v) | |
@minN = v[0].to_f | |
@maxN = v[1].to_f | |
self | |
end | |
def domainMap(v) | |
v = v.to_f | |
if (v <= @minV) | |
@minN | |
elsif (v >= @maxV) | |
@maxN | |
else | |
(((@maxN - @minN) * ((v - @minV) / (@maxV - @minV))) + @minN).round(2) | |
end | |
end | |
end | |
skale = DomainMap.new.setDomain([1, 50]).setRange([1, 223]) | |
puts skale.domainMap(20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment