Reputation: 449
Hi I am wanting to map a particular range of numbers to a different range in objective C for an iPad application.
For example I may have an input value in the range 0-255 but I want to output to be in the range 0.5-1. For example, an input of 127.5 would result in an output of 0.75.
Cheers in advance.
Upvotes: 2
Views: 1512
Reputation: 21464
Here's the general solution (it should work for any combination of ranges and input values):
CGFloat const inMin = 0.0;
CGFloat const inMax = 255.0;
CGFloat const outMin = 0.5;
CGFloat const outMax = 1.0;
CGFloat in = 127.5;
CGFloat out = outMin + (outMax - outMin) * (in - inMin) / (inMax - inMin);
Upvotes: 9
Reputation: 9887
Calculate the ratio of the first and apply it to the range of the second:
CGFloat result = ((127.5 / 255) * 0.5) + 0.5;
Upvotes: 5