Possible Duplicate:
Math - mapping numbers
I have value "x" that can be from 0 to 127 and a value "y" that can be from -1000 to 0. I need to make that if x = 0 than y = -1000 and if x = 127 than y = 0... How can i make it?
Possible Duplicate:
Math - mapping numbers
I have value "x" that can be from 0 to 127 and a value "y" that can be from -1000 to 0. I need to make that if x = 0 than y = -1000 and if x = 127 than y = 0... How can i make it?
It sounds like you just want a linear equation (y = mx + b). In your case, this would be
y = x*(1000/127) - 1000
linear interpolation...
slope = (0 - -1000) / (127 - 0) = (1000.0/127.0) y-intercept = 127
y = (1000.0/127.0) * x - 1000
of course this assumes x and y can take on "real" values and not just integers.
y = x * (1000.0/127.0) - 1000.0
Make sure you use float values in your calculation otherwise you will get inaccurate answers.
EDIT: And if you're really picky about accuracy, then this is better still:
y = (int) (0.5 + (x * (1000.0/127.0) - 1000.0))
(which will do correct rounding).