Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective c map one number range to another

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.

like image 271
KTas Avatar asked Nov 20 '25 17:11

KTas


1 Answers

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);
like image 111
Nick Forge Avatar answered Nov 23 '25 07:11

Nick Forge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!