Functionality for iterating through every 2D coordinate?

I need a way of iterating through ordered pairs in a spiral fashion like this:
(0, 0)
(1, 0)
(1, 1)
(0, 1)
(-1, 1)
(-1, 0)
(-1, -1)
(0, -1)
(1, -1)

This would be one revolution of the spiral, from there it would move on in the same manner. I would like it to be able to look at the previous pair and determine the next one. Any ideas??

Took a little longer than planned but:

Vector2 NextDirection(int x, int y){
	if(x == 0 && y == 0) return new Vector2(1, 0);
	float a = Mathf.Atan2 (y, x) / Mathf.PI;
	if(a >= -0.75f && a <= -0.25f) return new Vector2(x+1, y);
	if(a >= 0.25f && a < 0.75f) return new Vector2(x-1, y);
	if(a > -0.25 && a < 0.25f) return new Vector2(x, y+1);
	return new Vector2(x, y-1);

It’s easier to understand what happens when using Atan2, however it’s even possible without any trigonomety:

	Vector2 NextSpiralStep(int x, int y){
		if(x == 0 && y == 0) return new Vector2(1, 0);
		if(Mathf.Abs(x) > Mathf.Abs(y)+0.5f*Mathf.Sign(x) && Mathf.Abs(x) > (-y+0.5f))
			y += (int)Mathf.Sign(x);
			x -= (int)Mathf.Sign(y);
		return new Vector2(x,y);

	Vector2 NextSpiralCorner(int x, int y){
		if ((x<=0 && y<=0) || (x>0 && y >0))
			x = -x;
			y = x;
		return new Vector2(x,y);

NextSpiralStep returns the next point on the spiral and moves on 1 each step.

NextSpiralCorner calculates the whole edges of the spiral. So 4 steps is always one turn. I changed the parameters back to integers. You just have to replace your IntVec2. I also have such a vector (Vector3i) however it’s better to keep the solutions as generic as possible :wink:

I’ve tested SirCrazyNugget’s solution and it works without any problems. Mine does the same but without atan.