Vertices createSimplePolygon( 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 || !inBounds( ref entrance ) )
{
entranceFound = searchHullEntrance( out entrance );
if( entranceFound )
{
current = new Vector2( entrance.X - 1f, entrance.Y );
}
}
else
{
if( isSolid( ref entrance ) )
{
if( isNearPixel( ref entrance, ref last ) )
{
current = last;
entranceFound = true;
}
else
{
Vector2 temp;
if( searchNearPixels( 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, 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( 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 );
// We don't want the last vertex to be the same as the first one, because it causes the triangulation code to crash.
if( endOfHullArea.Contains( entrance ) )
endOfHullArea.Remove( entrance );
}
} while( true );
}
return polygon;
}