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 forloops, 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 <= N; d++) { for (int x = d; x <= d; x++) { coordinates[index++] = new Vector2(x, d) coordinates[index++] = new Vector2(x, d) } for (int y = d+1; y <= d1; y++) { coordinates[index++] = new Vector2(d, y) coordinates[index++] = new Vector2(d, y) } } for (int i=0; i<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 forloops 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?