private static Vertices CreateSimplePolygon(PolygonCreationAssistance pca, Vector2 entrance, Vector2 last)
{
bool entranceFound = false;
bool endOfHull = false;
Vertices polygon = new Vertices(32);
Vertices hullArea = new Vertices(32);
Vertices endOfHullArea = new Vertices(32);
Vector2 current = Vector2.Zero;
#region Entrance check
// Get the entrance point. //todo: alle möglichkeiten testen
if (entrance == Vector2.Zero || !pca.InBounds(ref entrance))
{
entranceFound = GetHullEntrance(pca, out entrance);
if (entranceFound)
{
current = new Vector2(entrance.X - 1f, entrance.Y);
}
}
else
{
if (pca.IsSolid(ref entrance))
{
if (IsNearPixel(pca, ref entrance, ref last))
{
current = last;
entranceFound = true;
}
else
{
Vector2 temp;
if (SearchNearPixels(pca, false, ref entrance, out temp))
{
current = temp;
entranceFound = true;
}
else
{
entranceFound = false;
}
}
}
}
#endregion
if (entranceFound)
{
polygon.Add(entrance);
hullArea.Add(entrance);
Vector2 next = entrance;
do
{
// Search in the pre vision list for an outstanding point.
Vector2 outstanding;
if (SearchForOutstandingVertex(hullArea, pca.HullTolerance, out outstanding))
{
if (endOfHull)
{
// We have found the next pixel, but is it on the last bit of the hull?
if (endOfHullArea.Contains(outstanding))
{
// Indeed.
polygon.Add(outstanding);
}
// That's enough, quit.
break;
}
// Add it and remove all vertices that don't matter anymore
// (all the vertices before the outstanding).
polygon.Add(outstanding);
hullArea.RemoveRange(0, hullArea.IndexOf(outstanding));
}
// Last point gets current and current gets next. Our little spider is moving forward on the hull ;).
last = current;
current = next;
// Get the next point on hull.
if (GetNextHullPoint(pca, ref last, ref current, out next))
{
// Add the vertex to a hull pre vision list.
hullArea.Add(next);
}
else
{
// Quit
break;
}
if (next == entrance && !endOfHull)
{
// It's the last bit of the hull, search on and exit at next found vertex.
endOfHull = true;
endOfHullArea.AddRange(hullArea);
}
} while (true);
}
return polygon;
}