I create a "circle" on my 2d-grid using this code:
public List<Cell> GetSurroundingCellsCircle(Coord originCoord, int distance)
{
List<Cell> matches = new List<Cell>();
int EX = distance + coord.x;
int EY = distance + coord.z;
int SQ = distance * distance;
for (int x = coord.x - distance; x <= EX; x++)
{
for (int z = coord.z - distance; z <= EY; z++)
{
int c = x - coord.x;
int d = z - coord.z;
if ((c * c + d * d) < SQ)
{
Cell cell = GetCell(new Coord(x, z));
if (cell != null)
matches.Add(cell);
}
}
}
return matches;
}
This code works fine and returns a list of cells that together create a circle-shape. Now I want to get all the edge-cells of this circle, and not only that, but I also need to know which side of the edge-cell that has no neighbor, so I know where to draw the line. I want a result that looks like this red line:
I wrote this code that would work for a square (it just finds the max/min of X/Z and draws a sphere there, but as you can see in my image, its missing the "rounding" cells:
int maxX = 0, maxZ = 0, minX = 0, minZ = 0;
for (int i = 0; i < cells.Count; i++)
{
int cX = cells[i].coord.x;
int cZ = cells[i].coord.z;
if (i == 0)
{
minX = cX;
minZ = cZ;
}
else
{
if (cX < minX)
minX = cX;
if (cZ < minZ)
minZ = cZ;
}
if (cX > maxX)
maxX = cX;
if (cZ > maxZ)
maxZ = cZ;
}
for (int i = 0; i < cells.Count; i++)
{
int cX = cells[i].coord.x;
int cZ = cells[i].coord.z;
if (cX == maxX || cZ == maxZ || cX == minX || cZ == minZ)
{
Gizmos.DrawSphere(
new Vector3((cells[i].coord.x * GridBase.WORLD_CELL_SIZE) + GridBase.WORLD_CELL_OFFSET,
1,
(cells[i].coord.z * GridBase.WORLD_CELL_SIZE) + GridBase.WORLD_CELL_OFFSET),
1);
}
}