Skip to content

Instantly share code, notes, and snippets.

@jnsprnw
Created November 22, 2016 10:07
Show Gist options
  • Save jnsprnw/d09361009f8b9d2364ae8768e77f3af1 to your computer and use it in GitHub Desktop.
Save jnsprnw/d09361009f8b9d2364ae8768e77f3af1 to your computer and use it in GitHub Desktop.
map a value from a range to a different domain
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