Skip to content

Instantly share code, notes, and snippets.

@kylepls
Last active February 11, 2024 08:26
Show Gist options
  • Save kylepls/bcc8889d6acb43edb00975a28f84c3d7 to your computer and use it in GitHub Desktop.
Save kylepls/bcc8889d6acb43edb00975a28f84c3d7 to your computer and use it in GitHub Desktop.
2-Dimensional Grid Square Spiral Coordinates
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kylepls
Copy link
Author

kylepls commented Feb 11, 2024

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:

  1. Plots must be near other plots. Adding extreme separation or randomness makes the world feel empty.
  2. Plots arrangements should be continous. Empty plots make for an empty server.
  3. A new plot should be speedy to allocate. Millions of plots may be active at any time. Referencing the x/y coordinate makes it difficult to find the next location to allocate as a plot.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment