简体   繁体   中英

Formula for returning X or Y on a grid spiral, but only the direction changes?

I am trying to find two formulae that will return either X or Y from an n input. These coordinates should be on a spiral grid pattern, similar to an Ulam spiral, but n signifies not the position on the spiral, but only the direction changes.

n = 1, return 0,1;
n = 2, return 1,1;
n = 3, return 1,-1;
n = 4, return -1,-1;
n = 5, return -1,2;
n = 6, return 2,2;
n = 7, return 2,-2;
n = 8, return -2,-2;

Should be two discrete formulae single, just supply n and get X and another to get Y. Effectively this just in single lines for X or Y with n as loop position input.

Look at the X values:

0 1 1 -1 -1 2 2 -2 -2 3 3 -3 -3 ...

Perform the obvious grouping and compare with n:

n: 1   2  3  4  5   6  7  8  9   10 ...
x: 0   1  1 -1 -1   2  2 -2 -2    3 ...

Add another row for k = (n+2)/4 (rounding down, I don't know how to indicate the floor function in ascii) and m = n mod 4:

k: 0   1  1  1  1   2  2  2  2    3 ...
m: 1   2  3  0  1   2  3  0  1    2

That should be enough to show how to render x in whichever language you like best.

And then y is easy: y(n) = x(n+1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM