# Help me solve a simple math problem

**URL:** <https://boards.straightdope.com/t/help-me-solve-a-simple-math-problem/686757>\
**Category:** Factual Questions\
**Created:** [April 23, 2014, 11:31am UTC](https://boards.straightdope.com/t/help-me-solve-a-simple-math-problem/686757 "2014-04-23T11:31:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![EllisDee](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ellisdee/32/4531_2.png) [@EllisDee](https://boards.straightdope.com/u/EllisDee)\
**Post date:** [April 23, 2014, 11:31am UTC](https://boards.straightdope.com/t/help-me-solve-a-simple-math-problem/686757/1 "2014-04-23T11:31:07Z")

</div>

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:

```auto

  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.

---

<div class="post-metadata">

**Author:** ![EllisDee](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ellisdee/32/4531_2.png) [@EllisDee](https://boards.straightdope.com/u/EllisDee)\
**Post date:** [April 23, 2014, 11:49am UTC](https://boards.straightdope.com/t/help-me-solve-a-simple-math-problem/686757/2 "2014-04-23T11:49:54Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Sofis](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@Sofis](https://boards.straightdope.com/u/Sofis)\
**Post date:** [April 23, 2014, 12:13pm UTC](https://boards.straightdope.com/t/help-me-solve-a-simple-math-problem/686757/3 "2014-04-23T12:13:34Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![EllisDee](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ellisdee/32/4531_2.png) [@EllisDee](https://boards.straightdope.com/u/EllisDee)\
**Post date:** [April 23, 2014, 12:22pm UTC](https://boards.straightdope.com/t/help-me-solve-a-simple-math-problem/686757/4 "2014-04-23T12:22:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sofis](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@Sofis](https://boards.straightdope.com/u/Sofis)\
**Post date:** [April 23, 2014, 12:30pm UTC](https://boards.straightdope.com/t/help-me-solve-a-simple-math-problem/686757/5 "2014-04-23T12:30:56Z")

</div>

No problem. 🙂 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. 🙂

---

<div class="post-metadata">

**Author:** ![Quercus](https://avatars.discourse-cdn.com/v4/letter/q/7ab992/32.png) [@Quercus](https://boards.straightdope.com/u/Quercus)\
**Post date:** [April 23, 2014, 12:42pm UTC](https://boards.straightdope.com/t/help-me-solve-a-simple-math-problem/686757/6 "2014-04-23T12:42:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sofis](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@Sofis](https://boards.straightdope.com/u/Sofis)\
**Post date:** [April 23, 2014, 12:51pm UTC](https://boards.straightdope.com/t/help-me-solve-a-simple-math-problem/686757/7 "2014-04-23T12:51:52Z")

</div>

Ah, right. I was thinking they had reversed the y-axis, but that was because I was misreading the bottom part of the OP. 🙂 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.

---

<div class="post-metadata">

**Author:** ![Quartz](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/quartz/32/267_2.png) [@Quartz](https://boards.straightdope.com/u/Quartz)\
**Post date:** [April 23, 2014, 12:55pm UTC](https://boards.straightdope.com/t/help-me-solve-a-simple-math-problem/686757/8 "2014-04-23T12:55:36Z")

</div>

> [@EllisDee](#):
>
> 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.

Do you mean a torus?

---

<div class="post-metadata">

**Author:** ![EllisDee](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ellisdee/32/4531_2.png) [@EllisDee](https://boards.straightdope.com/u/EllisDee)\
**Post date:** [April 23, 2014, 1:04pm UTC](https://boards.straightdope.com/t/help-me-solve-a-simple-math-problem/686757/9 "2014-04-23T13:04:30Z")

</div>

> [@Quartz](#):
>
> Do you mean a torus?

I believe so, yes.

> [@Quercus](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![Shalmanese](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@Shalmanese](https://boards.straightdope.com/u/Shalmanese)\
**Post date:** [April 23, 2014, 1:11pm UTC](https://boards.straightdope.com/t/help-me-solve-a-simple-math-problem/686757/10 "2014-04-23T13:11:59Z")

</div>

FYI, In general, this problem is called solving a system of linear equations. Khan academy has a [lesson on them](https://www.khanacademy.org/math/algebra/systems-of-eq-and-ineq/fast-systems-of-equations/v/solving-linear-systems-by-substitution).

---

<div class="post-metadata">

**Author:** ![EllisDee](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ellisdee/32/4531_2.png) [@EllisDee](https://boards.straightdope.com/u/EllisDee)\
**Post date:** [April 23, 2014, 1:18pm UTC](https://boards.straightdope.com/t/help-me-solve-a-simple-math-problem/686757/11 "2014-04-23T13:18:21Z")

</div>

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](http://www.google.com/imgres?imgurl=http%3A%2F%2Fi43.tinypic.com%2F2lmmc6r.jpg&imgrefurl=http%3A%2F%2Fforum.travian.us%2Fshowthread.php%3Ft%3D58004%26page%3D6&h=409&w=405&tbnid=RbsdZMqSz75c8M%3A&zoom=1&docid=vPXWIaswXjSe0M&itg=1&ei=WrtXU96eCIGPyASxmYDYCA&tbm=isch&ved=0CFkQMygFMAU&iact=rc&uact=3&dur=1192&page=1&start=0&ndsp=12&biw=905&bih=580)

[Picture 2](http://www.google.com/imgres?imgurl=http%3A%2F%2Ffarm3.static.flickr.com%2F2769%2F4497560521_f275b9d58f.jpg&imgrefurl=http%3A%2F%2Fforum.travian.co.uk%2Fshowthread.php%3Ft%3D64976&h=351&w=500&tbnid=kaQiH6K4r--_7M%3A&zoom=1&docid=8a9vHZ4lhfSEYM&ei=WrtXU96eCIGPyASxmYDYCA&tbm=isch&ved=0CFQQMygAMAA&iact=rc&uact=3&dur=6880&page=1&start=0&ndsp=12&biw=905&bih=580)

[Picture 3](http://www.google.com/imgres?imgurl=http%3A%2F%2Fi55.tinypic.com%2F20f46iu.png&imgrefurl=http%3A%2F%2Fforum.travian.co.uk%2Fshowthread.php%3Ft%3D66226&h=289&w=297&tbnid=_mH5E7f5Mf-AdM%3A&zoom=1&docid=GdFjZl9R5qPCRM&ei=WrtXU96eCIGPyASxmYDYCA&tbm=isch&ved=0CFcQMygDMAM&iact=rc&uact=3&dur=463&page=1&start=0&ndsp=12&biw=905&bih=580)
