问题
I want to find if a point (x,y)where x,y integers satisfy the spiral square. (0,0) (0,1) (1,1) (1,2) (0,2) (-1,2) (-2,2) (-2,1) (-2,0) ans so on.....
How do I do it? I want the logic for a java or c++ function.
回答1:
Here is some pseudocode logic:
Start with x=0, y=0, dist=0, direction=1
Loop
x += (++dist * direction)
WritePoint(x,y)
y += (++dist * direction)
WritePoint(x,y)
direction *= -1
LoopEnd
Take it from there.
回答2:
Do this operation:
(operator)(operation)(amount)
where operator alternates as x,y,x,y,...(use % operator for that), operation alternates as +,+,-,-,+,+,-,-,+,+...(again use % operator for that) and amount changes as 1,2,3,...
来源:https://stackoverflow.com/questions/25228464/square-spiral-co-ordinate