Help me solve a simple math problem

I’m working with a simple X,Y coordinate system. Either can be positive or negative, and the range is 0 to 400 in all four directions:

-400,400 = top left
400,400 = top right
0,0 = center
-400,-400 = bottom left
400,-400 bottom right

There is a predefined conversion I’m trying to figure out that combines a coordinate pair into a single number. They call this single number Z, despite it not being an actual Z coordinate.

It would be helpful to me if I could convert the X,Y pairs into Z, and vice versa, but this very basic arithmetic is beyond my grasp. Any help would be appreciated.

Here’s some sample values:


  X      Y       Z
----   ----   ------
  58     41   288018
  59     41   288019
  58     40   288819
  59     40   288820
-106    -34   347929
-140   -131   425592
-139   -131   425593
-138   -131   425594
-140   -132   426393
-139   -132   426394
-138   -132   426395
-140   -133   427194
-139   -133   427195
-138   -133   427196

I can provide more samples as needed, though I can’t arbitrarily find out the Z for any given X.Y.

Not sure if this is relevant, but the grid is actually a sphere of some kind. (-400,100) directly borders (400,100), for example. This is true in all directions. That means there’s a single 2x2 area with coordinates:

400,-400 (top left of the 2x2)
-400,-400 (top right of the 2x2)
400,400 (bottom left of the 2x2)
-400,400 (bottom right of the 2x2)

EDIT: And no, this is not homework. It’s for a game I play.

To clarify, 0 is a row and a column. Meaning (1,1) doesn’t border (-1,1). (0,1) is in between them. Similarly, (1,0) is in between (1,1) and (1,-1).

(401 - y)*801 + x - 400, I believe.

Edit: Which would be equivalent to (400 - y)*801 + x + 401, which looks a bit nicer.

Sweet, thanks much!

Plugging in sample values for random squares on the map in the various quadrants and it does appear to work.

I knew it was going to end up being an embarrassingly simple arithmetic problem to not be able to solve, but I just couldn’t figure it out. Much appreciated.

No problem. :slight_smile: Multiplying one number by the range of the other, and adding the other to the result, is the standard way of doing stuff like that. In this case they had to do some adjustments if they only wanted positive numbers. Not sure why they decided to use 400 - y instead of 400 + y, though, that seemed a bit unnecessarily convoluted. :slight_smile:

Or, equivalently, Z= X-801*Y+320801.

Another way to describe it is to start in the upper left corner with 1, and number consecutively, going left to right for each row, then moving to the next row down, as if you were reading English text.

Which explains why the Y is subtracted in the formula for Z: they wanted standard Cartesian coordinates for Y (larger is up), but wanted Z to be higher going down.

Ah, right. I was thinking they had reversed the y-axis, but that was because I was misreading the bottom part of the OP. :slight_smile: Yeah, there’s a definite utility to numbering things from the top. For example, if you’re rendering tiles in order of this number, and have some isometric perspective where things further down may cover things further up.

Do you mean a torus?

I believe so, yes.

Thanks much for this. I was about to ask how to convert a Z back into its X,Y values until I saw this, and then it fell into place. The "" sign refers to integer division, where the remainder is discarded:

Y = 400 - Z \ 801
X = Z - (400 - Y) * 801 - 401

Yay!

This works for me, though if it can be tightened up a bit I wouldn’t complain.

FYI, In general, this problem is called solving a system of linear equations. Khan academy has a lesson on them.

The grid obeys the basic a[sup]2[/sup] + b[sup]2[/sup] = c[sup]2[/sup] distance formula. So to calculate this in all cases, all I have to do is check which direction “a” is shorter and which direction “b” is shorter and then use the shorter versions, yes?

As in, (400,400) is either 801 squares or 1 square away from (-400,-400) for both a and b. So I would use the square root of 2 instead of the square root of 2 * (801[sup]2[/sup]).

Here’s a few pictures of the torus other users have posted. These are links from google image search, so I don’t know how reliable or persistent they are:

Picture 1

Picture 2

Picture 3