I have a situation where I have a rectangle that moves in 25 px increments. I need to stop the rectangle when it’s x position is equal to 740. 740 is not dividable by 25 so the x pos will never fall on 740. This increment of 25 is also dynamic and can be from 25-100. Question is language independent.
Asked
Active
Viewed 62 times
-3
-
If the increment of 25 is dynamic then surely you can change it to something that goes into 740 evenly? – Jon Reinhold May 06 '18 at 14:18
-
Operator is using a slider to select the increment from 25-100. It’s a bit embarrassing that it works only sometimes :). Well maybe I can allow only the increments that land on 740. – user9146930 May 06 '18 at 15:16
-
What about `X := Min(X + Increment, 740);` – Tom Brunberg May 06 '18 at 16:21
-
That depends on your move algorithm. By "I need to stop the rectangle" you mean stop algorithm that moves or stop moving rectangle? What you say is very abstract. Why simple `IF x < 740 THEN x := x + 25; END_IF;` does not work? – Sergey Romanov May 07 '18 at 05:47
-
I like Toms solution. Sergey, that will stop rectangle at x = 750. It will be 725 and then on the next pass will be 750, then it will stop. – user9146930 May 23 '18 at 19:53
2 Answers
0
Given that there are possibilities that the position 740 can not be reached, but only approximated by +/- increment, you must take it in account.
So, the test could be:
if (x >= 740-increment div 2) and (x <= 740+increment div 2) then... (got)
Of course, this works if you know the increment. If not, you have to use the maximum possible increment.
Another way, longer but perhaps more understandable is:
if x < 740 then if x+increment >= 740 then... (got)
if x > 740 then if x-increment <= 740 then... (got)
Try to compare the two methods... and have fun!

linuxfan says Reinstate Monica
- 4,281
- 2
- 19
- 35
0
Possibly implement a check before you increment the position variable.
IF currentXpos + increment < 740 THEN
currentXpos = currentXpos + increment;
END_IF
If you want the position to be AT LEAST 740, simply use
IF currentXpos < 740 THEN
currentXpos = currentXpos + increment;
END_IF

Alex Marks
- 66
- 7