Get spiral index from location

Posted by ricick on Stack Overflow See other posts from Stack Overflow or by ricick
Published on 2012-04-02T02:10:17Z Indexed on 2012/04/02 5:30 UTC
Read the original article Hit count: 154

I'm using Alberto Santini's solution to this question to get a spiral grid reference based on an items index

Algorithm for iterating over an outward spiral on a discrete 2D grid from the origin

It's not the accepted solution, but it's the best for my needs as it avoids using a loop.

It's working well, but what I want now is to do the inverse. Based on a known x and y coordinate return the index of a location.

This is as a precursor to returning the items surrounding a given location.

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about language-agnostic