Square Spiral Co-ordinate

天涯浪子 提交于 2020-01-14 07:12:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!