List<float> searchCrossingEdges( Vertices polygon, int y )
{
// sick-o-note:
// Used to search the x coordinates of edges in the polygon for a specific y coordinate.
// (Usualy comming from the texture data, that's why it's an int and not a float.)
List<float> edges = new List<float>();
// current edge
Vector2 slope;
Vector2 vertex1; // i
Vector2 vertex2; // i - 1
// next edge
Vector2 nextSlope;
Vector2 nextVertex; // i + 1
bool addFind;
if( polygon.Count > 2 )
{
// There is a gap between the last and the first vertex in the vertex list.
// We will bridge that by setting the last vertex (vertex2) to the last
// vertex in the list.
vertex2 = polygon[polygon.Count - 1];
// We are moving along the polygon edges.
for( int i = 0; i < polygon.Count; i++ )
{
vertex1 = polygon[i];
// Approx. check if the edge crosses our y coord.
if( ( vertex1.Y >= y && vertex2.Y <= y ) ||
( vertex1.Y <= y && vertex2.Y >= y ) )
{
// Ignore edges that are parallel to y.
if( vertex1.Y != vertex2.Y )
{
addFind = true;
slope = vertex2 - vertex1;
// Special threatment for edges that end at the y coord.
if( vertex1.Y == y )
{
// Create preview of the next edge.
nextVertex = polygon[( i + 1 ) % polygon.Count];
nextSlope = vertex1 - nextVertex;
// Ignore peaks.
// If thwo edges are aligned like this: /\ and the y coordinate lies on the top,
// then we get the same x coord twice and we don't need that.
if( slope.Y > 0 )
addFind = ( nextSlope.Y <= 0 );
else
addFind = ( nextSlope.Y >= 0 );
}
if( addFind )
edges.Add( ( y - vertex1.Y ) / slope.Y * slope.X + vertex1.X ); // Calculate and add the x coord.
}
}
// vertex1 becomes vertex2 :).
vertex2 = vertex1;
}
}
edges.Sort();
return edges;
}