Last active
February 11, 2024 08:26
-
-
Save kylepls/bcc8889d6acb43edb00975a28f84c3d7 to your computer and use it in GitHub Desktop.
2-Dimensional Grid Square Spiral Coordinates
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One of the problems I faced while working on Plotz was how do you partion segments of a world such that the following criteria are met:
These problems are solved by clamping world coordinates to a square spiral. The problem is that the conversion between x/y world coordinates and the spiral segment index is extremely difficult if not performed iteratively.
The code above defines the formulas necessary to convert x/y to index and index back to x/y.
See here for a great explaination on the math: https://math.stackexchange.com/questions/163080/on-a-two-dimensional-grid-is-there-a-formula-i-can-use-to-spiral-coordinates-in