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.

enter image description 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.

  • $\begingroup$ 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. $\endgroup$ – NoChance Oct 20 '11 at 10:54
  • $\begingroup$ @ Emmad Kareem Of course it is consistent ! Adding 590 to 4 will get me 594 ! $\endgroup$ – Suhail Gupta Oct 20 '11 at 10:59
  • $\begingroup$ 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? $\endgroup$ – Beni Bogosel Oct 20 '11 at 11:32
  • $\begingroup$ @ 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 $\endgroup$ – Suhail Gupta Oct 20 '11 at 11:39
  • $\begingroup$ 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. $\endgroup$ – 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.

  • $\begingroup$ @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? $\endgroup$ – NoChance Oct 23 '11 at 5:38
  • $\begingroup$ @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$. $\endgroup$ – joriki Oct 23 '11 at 5:57
  • $\begingroup$ @jorki thanks for your explanation. +1 for good solution indeed. $\endgroup$ – NoChance Oct 23 '11 at 6:00
  • $\begingroup$ @Emmad: You're lucky the software only looks at the first three letters -- you keep misspelling my name :-) $\endgroup$ – joriki Oct 23 '11 at 6:02
  • $\begingroup$ OOPs, I am really sorry, I am typing on a horrible keyboard! $\endgroup$ – NoChance Oct 23 '11 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.