Inverse number in the range

I have a servo that rotates against the number that I get from the program. The numbers that I get from the program are 37 ... 113. I need to translate 37 to the opposite side. Thus, 37 becomes 113, 38 becomes 112, etc. 75 remains at 75 because the midpoint.

Do any of you know a way to calculate this? It sounds like simple math, but I can't figure it out. I do not want to use a lookup table because the range may change.

+6
source share
2 answers
public int reverseNumber(int num, int min, int max) { return (max + min) - num; } reverseNumber(37, 37, 113); // returns 113 
+13
source
 public int calculate(int min, int max, int input) { return max-(min-input); } 
+1
source

Source: https://habr.com/ru/post/943735/


All Articles