Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse Number in a Range

Tags:

java

math

I've got a servo which turns opposite to the number I get from a program. The numbers I get from the program are between 37...113. I need to convert the 37 to its opposite side. So 37 becomes 113, 38 becomes 112, and so on. 75 stays at 75 because that's the mid point.

Do any of you know of a way to calculate this? This sounds like simple math, but I can't figure it out. I don't want to use a look-up table because the range may change.

like image 813
Adam Davies Avatar asked Apr 26 '13 17:04

Adam Davies


1 Answers

public int reverseNumber(int num, int min, int max) {
    return (max + min) - num;
}

reverseNumber(37, 37, 113); // returns 113
like image 200
Brigham Avatar answered Sep 20 '22 03:09

Brigham