________________________________________
| | |
| | |
| 16 | 15 | 14 |
13 | 12 |
| | |
| | |
---------------------------------------------------------------------
| | |
| | |
| 17 | 4 | 3 |
2 | 11 |
| | |
| | |
---------------------------------------------------------------------
| | |
| | |
| 18 | 5 | 0 |
1 | 10 |
| | |
| | |
---------------------------------------------------------------------
| | |
| | |
| 19 | 6 | 7 |
8 | 9 |
| | |
| | |
---------------------------------------------------------------------
Of course there is such an algorithm. If it is ever to be caught in a single formula, this
formula necessarily involves Floor and/or Ceiling functions.
Hint:
Departing from 0 in the upward left (NW) direction one has the even integral squares.
Departing from 1 in the SO direction one has the odd integral squares.
Departing from 0 in the SW direction one has 0 = 0*1, 6 = 2*3, 20 = 4*5, 42 = 6*7 etc.
Departing from 0 in the NE direction one has 0 = -1*0, 2 = 1*2, 12 = 3*4, 30 = 5*6 etc.
Good luck: Johan E. Mebius
Thanks for that information. But the question was:
If I have the number on the spiral which are the
cartesian coordinates?.
Otherwise, having the coordinates which is the number?.
Departing from zero in East direcction and counterclockwise.
(Origin of coordinates in center of cell number zero.)
Examples:
n X Y
11 2 1
14 0 2
21 -1 -2
24 2 -2
Ludovicus
>> Of course there is such an algorithm. If it is ever to be caught in a
>> single formula, this formula necessarily involves Floor and/or Ceiling
>> functions.
...
>> Departing from 0 in the upward left (NW) direction one has the even
>> integral squares. Departing from 1 in the SO direction one has the odd
>> integral squares. Departing from 0 in the SW direction one has 0 = 0*1,
>> 6 = 2*3, 20 = 4*5, 42 = 6*7 etc. Departing from 0 in the NE direction
>> one has 0 = -1*0, 2 = 1*2, 12 = 3*4, 30 = 5*6 etc.
> Thanks for that information. But the question was: If I have the
> number on the spiral which are the cartesian coordinates?.
> Otherwise, having the coordinates which is the number?. [...]
[Eg
n X Y
11 2 1
19 -2 -1
24 2 -2 ]
The rules that Johan mentioned allow calculating such coordinates,
and are clear enough (except for the 'SO' typo that should be 'SE').
From the rules: Given n, compute s = floor(sqrt(n)).
Case 0, s even: let b = s/2.
Case 0.a, n < s*(s+1): x,y = -b, b-n+s^2.
Case 0.b, n >= s*(s+1): x,y = -b+n-s*(s+1), -b.
Case 1, s odd: let b = (s+1)/2.
Case 1.a, n < s*(s+1): (etc)
Two example calcs:
If n=19, s=4, b=2, and 19 < 4*5. By case 0.a,
x = -b = -2, y = b-n+s^2 = 2-19+16 = -1; x,y = -2,-1.
If n=24, s=4, b=2, and 24 > 4*5. By case 0.b,
x = -b+n-s*(s+1) = -2+24-20 = 2, y = -b = -2; x,y = 2,-2.
For the other question, coordinates-to-number, you could
also have 4 cases, N-S-E-W, like (|x| > |y|: x>0; x<0)
and (|y| >= |x|: y>0; y<0).
--
jiw
Index to position
-----------------
n -> (x,y)
0 -> (0,0)
for n > 0,
sqrt(n)+1
m = floor( --------- )
2
k = n - 4m(m-1)
1 <= k <= 2m -> (x,y) = (m,k-m)
2m <= k <= 4m -> (x,y) = (3m-k,m)
4m <= k <= 6m -> (x,y) = (-m,5m-k)
6m <= k <= 8m -> (x,y) = (k-7m,-m)
Position to index
-----------------
(x,y) -> n
m = max(|x|,|y|)
x = m -> n = 4m(m-1) + m + y except if y = -m
y = m -> n = 4m(m-1) + 3m - x
x = -m -> n = 4m(m-1) + 5m - y
y = -m -> n = 4m(m-1) + 7m + x
Rob Johnson <r...@trash.whim.org>
take out the trash before replying
to view any ASCII art, display article in a monospaced font