# Movement in a grid

I am in a middle of a grid of 8X8 . Each box of grid is a square with side of 80 units. Suppose my current coordinates are 594,422 . How many boxes will i have to move to enter the square that contains the coordinates 594,24 ?

The grid starts from the coordinates 350,20 as shown here.

The answer of the above problem is : I will have to move 5 boxes to capture the coordinate 594,24. But how can i generalize this result ? Any expression that can evaluate this is what i want ? Please also show the logic i.e where does the expression come from .

If my current coordinate is x,y and i have to capture x,q how many boxes will i have to move to do that ? Getting more general , if my current coordinate is x,y and i have to capture p,q how many boxes will i have to move to do that ?

In this grid x and y increase as shown.

-
The fact that the side is 80 units is looks strange to me. It is not consistent with drawing. Here is why: for the x-axis, you start at x=350, moving 3 squares to the right would get you to 3x80+350=590 not to 594 as in your drawing. Same thing for y. Starting at y= 20, moving 5 squares down would get you to 5*80+20 and not 422 as shown. – NoChance Oct 20 '11 at 10:54
@ Emmad Kareem Of course it is consistent ! Adding 590 to 4 will get me 594 ! – Suhail Gupta Oct 20 '11 at 10:59
Tell us why the sides of the small squares are of length 80? Do you want to create a chess game? You say that you want to capture the coordinate $(p,q)$. What does that mean? You say that you move 5 boxes. That means that you count your moves just like in chess. Why do you need coordinates then? – Beni Bogosel Oct 20 '11 at 11:32
@ Beni Bogosel The length is 80 because i have kept it 80.Yes i am working on a chess game.It means that i want reach the box that has the coordinate(p,q). Actually i want to highlight the box where my mouse pointer is currently in and i have the w.r.t with that point also ! . So,i need this method to get me what i want – Suhail Gupta Oct 20 '11 at 11:39
OK, if you are using addition as if the length of the square is 1 unit not 80, then how can you justify that moving 5 squares from (594,24) gets you to (594, 422)? The change from 24 to 422 is not achieved by adding 5 and is not achieved by adding 5x80 either. So, it something is not quite right here. – NoChance Oct 20 '11 at 17:10

You can do this in two steps: First figure out how to get from point coordinates to box coordinates; then just subtract the box coordinates corresponding to the source and destination. To find the box coordinates corresponding to given point coordinates, first subtract the coordinates $350,20$ of the origin. Then the box coordinate is just the result of dividing that by the box size, $80$, with integer division. In a programming language where / applied to integers results in integer division, you could write this as

bx = (x - 350) / 80;
by = (y -  20) / 80;


In mathematical notation, this would be

$$x_b = \left\lfloor\frac{x-350}{80}\right\rfloor\;,$$ $$y_b = \left\lfloor\frac{\,y\,\;-\;\,20\,}{80}\right\rfloor\;,$$

where the floor function $\lfloor\cdot\rfloor$ yields the greatest integer not greater than its argument.

-
@jorki could you please explain how can we justify that in the diagram, we have a the 2 points (350,20) and (594,24) representing the same row? Is this because the cell side is 80? – NoChance Oct 23 '11 at 5:38
@Emmad: The problem both in this comment and your comments under the question seems to be that you're thinking of these points as "representing" the boxes. That's not how I understand the question. We're at some point and given some other point, and we want to know the difference in boxes between the box containing the current point and the box containing the other point. There's no implication that these points "represent" the boxes, or are at any particular position within the boxes. The $y$ coordinate of a point in the upper row can be anything between $20$ and $99$. – joriki Oct 23 '11 at 5:57
@jorki thanks for your explanation. +1 for good solution indeed. – NoChance Oct 23 '11 at 6:00
@Emmad: You're lucky the software only looks at the first three letters -- you keep misspelling my name :-) – joriki Oct 23 '11 at 6:02
OOPs, I am really sorry, I am typing on a horrible keyboard! – NoChance Oct 23 '11 at 6:42