A bunch of guys and myself from work were trying to wrap our heads around this problem, but couldn't find a clean and efficient mathematical way to solve this problem. Here it is:

Given a standard Cartesian plane and a sequence of images (all square and of the same dimensions, we'll just say 1 unit for example) how do we organize them in such a fashion that they spiral around the origin (0,0) of the plane by their top left corner. But also more specifically, if given the number 25 for example (the 25th square in the sequence) what would it's X,Y coordinates be?

Hopefully this crude image helps explain the sequence. The very first square to be placed on the grid would be the red, then blue, yellow, purple, green, black, brown, and then so forth around as the dots illustrate.

We were hoping if there was a "relatively" simple mathematical formula for this, but maybe that's wishful thinking.

# Best How To :

(On edit: I added a second function which enables the Cartesian coordinate version to be obtained directly.)

I got this far before my head exploded. It is closed form in the sense that it gives the coordinates of say the one millionth square without needing to place them one by one in a loop. Writing it down wouldn't give a nice formula but you could write as a piecewise defined formula with 8 pieces. The answer is given in 1-based grid-coordinates. The first coordinate tells you how many squares you are to the right or left of the y-axis and the second coordinate how far up or down. From these numbers it is relatively easy to e.g. get Cartesian coordinates of their top left corner. I implemented it in Python:

```
from math import sqrt, ceil
def coordinates(m):
n = ceil(sqrt(m)/2) #shell number
i = m - 4*(n-1)**2 #index in shell
if i <= n:
return (n,-i)
elif i <= 2*n-1:
return (2*n - i, -n)
elif i <= 3*n - 1:
return (2*n - 1 - i, -n)
elif i <= 4*n - 2:
return (-n, -4*n + 1 + i)
elif i <= 5*n - 2:
return (-n, -4*n + 2 + i)
elif i <= 6*n - 3:
return (-6*n + 2 + i, n)
elif i <= 7*n - 3:
return (-6*n + 3 + i,n)
else:
return (n, 8*n -3 - i)
```

To get the (x,y) Cartesian coordinates of the top left corners from the (i,j) grid coordinates you can use the following function, which has an optional width parameter to allow for non-unit squares:

```
def cartesianFromGrid(i,j,w = 1):
x = w * (i if i < 0 else i - 1)
y = w * (j if j > 0 else j + 1)
return (x,y)
```

It is possible to directly get the Cartesian coordinates of the top left corner directly without first going through grid coordinates. The resulting formula involves fewer cases (since I don't need to skip from 1 directly to -1 or vice versa) though I am keeping both formulas in the answer since in many ways the grid perspective is more natural:

```
def cartCoordinates(m):
n = ceil(sqrt(m)/2) #shell number
i = m - 4*(n-1)**2 #index in shell
if i <= n:
return (n-1,-i+1)
elif i <= 3*n - 1:
return (2*n - 1 - i, -n + 1)
elif i <= (5*n - 2):
return (-n, -4*n + 2 + i)
elif i <= 7*n - 3:
return (-6*n + 2 + i, n)
else:
return (n-1, 8 * n - 3 - i)
```

Output for 1-16:

```
>>> for n in range(1,17):
print(n, ': grid coords =', coordinates(n),
'Cartesian =',cartesianFromGrid(*coordinates(n)))
1 : grid coords = (1, -1) Cartesian = (0, 0)
2 : grid coords = (-1, -1) Cartesian = (-1, 0)
3 : grid coords = (-1, 1) Cartesian = (-1, 1)
4 : grid coords = (1, 1) Cartesian = (0, 1)
5 : grid coords = (2, -1) Cartesian = (1, 0)
6 : grid coords = (2, -2) Cartesian = (1, -1)
7 : grid coords = (1, -2) Cartesian = (0, -1)
8 : grid coords = (-1, -2) Cartesian = (-1, -1)
9 : grid coords = (-2, -2) Cartesian = (-2, -1)
10 : grid coords = (-2, -1) Cartesian = (-2, 0)
11 : grid coords = (-2, 1) Cartesian = (-2, 1)
12 : grid coords = (-2, 2) Cartesian = (-2, 2)
13 : grid coords = (-1, 2) Cartesian = (-1, 2)
14 : grid coords = (1, 2) Cartesian = (0, 2)
15 : grid coords = (2, 2) Cartesian = (1, 2)
16 : grid coords = (2, 1) Cartesian = (1, 1)
```

In case you wondered:

```
>>> coordinates(1000000)
(500, 1)
```

This last answer makes sense since the one millionth square is the capstone of a 1000x1000 grid of squares.

I used the above to place colored squares on a tkinter canvas widget: