I have a value range, say the iPhone screen size 480×320. I have a position that may be outside the range, let’s say the position’s x coordinate is 600 for example.
In order to adjust the x coordinate to its on-screen position I can do: 600 – 480 = 120
But when the value is greater than two times 480 I’d have to run a loop, subtract 480 until the resulting number is below 480.
I know there’s an optimization to this problem revolving around division/modulo but I just can’t find a good answer (or question) related to this. Mainly because I can only guess possibly helpful search phrases.
I’m feeling sick today and for the live of me I can’t wrap my head around it. I’d welcome any pointers, even a “close as duplicate”.
PS: this is for Objective-C but any language will do.
You are looking for the modulo operator. The solution for the case of a width of
480is:Modulo will guarantee that the resulting value is between 0 and 479.