I have a list of objects arranged on a grid. I want to remove any which don’t don’t have a path back to the top of the grid, either directly or through their neighbours.
I thought a good way to do this would be to first make everything on the grid deletable, then make anything on the top row not deletable. Then, I wanted to do a flood fill from any objects in the top row, to make the objects connected to them not deletable.
I can’t think of a way (that works) to optimize it. Is there a simpler way to go about what I’m trying to do?
I may have shot myself in the foot by using a list, rather than a 2d array. It’s introducing a lot of extra foreach loops.
internal void DetectHangingObjects(int X)
{
//Set all objects to deletable
for (int i = 0; i < planets.Count; i++)
planets[i].deletable = true;
//Set first row to not deletable
for (int i = 0; i < planets.Count; i++)
{
Debug.WriteLine(planets[i].X + " " + X);
if (planets[i].X == X) //X=0
{
planets[i].deletable = false;
try
{
DetectHangingNeighbours(planets[i]);
}
catch (Exception ee)
{ Debug.WriteLine(ee.Message); }
}
}
}
internal void DetectHangingNeighbours(Planet planet)
{
if (planet == null || planet.deletable==true)
return;
planet.deletable = false;
DetectHangingNeighbours(GetTopLeftNode2(planet));
DetectHangingNeighbours(GetTopRightNode2(planet));
DetectHangingNeighbours(GetLeftNode2(planet));
DetectHangingNeighbours(GetRightNode2(planet));
DetectHangingNeighbours(GetBottomLeftNode2(planet));
DetectHangingNeighbours(GetBottomRightNode2(planet));
}
//The following methods check the six adjacent objects and returns them to the caller if they match
internal Planet GetTopLeftNode2(Planet planet)
{
foreach (Planet gridPlanet in planets)
if (gridPlanet.X == planet.X - planetSize && gridPlanet.Y == planet.Y - yOffset)
return gridPlanet;
return null;
}
internal Planet GetTopRightNode2(Planet planet)
{
foreach (Planet gridPlanet in planets)
if (gridPlanet.X == planet.X - planetSize && gridPlanet.Y == planet.Y + yOffset)
return gridPlanet;
return null;
}
internal Planet GetLeftNode2(Planet planet)
{
foreach (Planet gridPlanet in planets)
if (gridPlanet.X == planet.X && gridPlanet.Y == planet.Y - planetSize)
return gridPlanet;
return null;
}
internal Planet GetRightNode2(Planet planet)
{
foreach (Planet gridPlanet in planets)
if (gridPlanet.X == planet.X && gridPlanet.Y == planet.Y + planetSize)
return gridPlanet;
return null;
}
internal Planet GetBottomLeftNode2(Planet planet)
{
foreach (Planet gridPlanet in planets)
if (gridPlanet.X == planet.X + planetSize && gridPlanet.Y == planet.Y - yOffset)
return gridPlanet;
return null;
}
internal Planet GetBottomRightNode2(Planet planet)
{
foreach (Planet gridPlanet in planets)
if (gridPlanet.X == planet.X + planetSize && gridPlanet.Y == planet.Y + yOffset)
return gridPlanet;
return null;
}
Avoid recursion (DetectHangingNeighbours calling itself). Use instead a stack approach:
Good luck.