Reputation: 1
How do I convert an -1 through 1 input to 0 through 1. I have tried but have been unsuccessful.
Upvotes: 0
Views: 157
Reputation: 26744
In general, if you want to map some interval x1..x2
to y1..y2
, you can apply the following logic:
value-x1
(y2-y1)/(x2-x1)
gives you the ratio by which to expand/contract, so you simply multiple by it: (value-x1)*(y2-y1)/(x2-x1)
y1
: (value-x1)*(y2-y1)/(x2-x1)+y1
.Substituting all the values from your example (x1..x2 is -1..1 and y1..y2 is 0..1), we get: (value-(-1))*(1-0)/(1-(-1))+0
, which is the same as (value+1)/2
.
Upvotes: 2