# How to generate a grid from the center?

Without starting from one corner to another, but from the center and expands, because I’m learning ways to program my own chunk system.

I guess you want something like the Ulam spiral (the actual spiral, not just the prime numbers ^^).

We already developed a function over here to iterate through the points of such a square spiral. You just pass in “0, 0” for the first point and you get the next one out. If you continue passing in the last position you get out the next one. Of course since the return value is a Vector2 you have to cast it’s components to “int” in order to pass them in as integers.

It might be easier when you create a simple “Vector2i” struct that simply contains two integers:

``````public struct Vector2i
{
public int x;
public int y;
}
``````

And use that as parameter as well as return value. Or pack everything into a class like this:

``````public class Spiral
{
public int x = 0;
public int y = 0;
public void Next()
{
if(x == 0 && y == 0)
{
x = 1;
return;
}
if(Mathf.Abs(x) > Mathf.Abs(y)+0.5f*Mathf.Sign(x) && Mathf.Abs(x) > (-y+0.5f))
y += (int)Mathf.Sign(x);
else
x -= (int)Mathf.Sign(y);
}
public Vector2 NextPoint()
{
Next();
return new Vector2(x,y);
}
public void Reset()
{
x = 0;
y = 0;
}
}
``````

You can simply create an instance of that class and call “NextPoint” repeatedly. Each time this would give you a relative vector that spirals out around 0,0. If you want the point as integers you can simply call “Next” and read the public x and y variables afterwards. When dealing with chunk positions you most likely want to use integers.