Walk around a coordinate in circular order to a distance of N For my development purpose, I need to work on all the 2D coordinates (in whole number) around [0,0] from 0 to N distance. For example, with N=1, I need

N0: [0, 0]
N1: [-1, -1] [0, -1] [1, -1] [1, 0] [1, 1] [0, 1] [-1, 1] [-1, 0]


The order is important. I want to walk these values in a circular way, starting from 0, incrementing the distance each round.

I first created two for-loops, from -N to N on each axes, but the resulting order is the one as you read a book: from top left to bottom right.

Here's the order I aim (the first coordinate to walk on each circle is not important):



Here's a first algorithm I tried, in pseudo code

int totalSize = (2*N+1, 2)*(2*N+1, 2)
Vector2[] coordinates = new Vector2[totalSize]
int index = 0
coordinates[index++] = new Vector2(0, 0)
for (int d = 1; d &lt;= N; d++) {
    for (int x = -d; x &lt;= d; x++) {
        coordinates[index++] = new Vector2(x, -d)
        coordinates[index++] = new Vector2(x, d)
    }
    for (int y = -d+1; y &lt;= d-1; y++) {
        coordinates[index++] = new Vector2(-d, y)
        coordinates[index++] = new Vector2(d, y)
    }
}
for (int i=0; i&lt;coordinates.Length; i++) {
    print(coordinates[i])
}


But:


It seems too cumbersome. I don't like the creation of a scructure that retains the coordinates. And 4 for-loops seems not optimized.
The order of the circles is ok, but the order inside each circle is not the one I described. 
I have the feeling trigonometry can help me, but I can't see how to implement it on discrete values


Any idea?
You can do this in a stateless manner.

Here is python code that generates the infinite walk:

def circular():
    yield (0,0)
    x,y = -1,0
    while True:
        yield (x,y)
        if x &lt; 0 and 0 &lt;= y &lt; -x:
            y += 1
        elif y &gt; 0 and x &lt; y:
            x += 1
        elif x &gt; 0 and y &gt; -x:
            y -= 1
        elif y &lt; 0 and x &gt; y:
            x -= 1
        elif x &lt; 0 and y &lt; -1:
            y += 1
        elif x &lt; 0 and y == -1:
            x -= 1
            y += 1