Exemple #1
0
bool PointInPolygon( const vec2d & R, const std::vector< vec2d > & pnts )
{
    // Implementation of Algorithm 6 from "The Point in Polygon Problem
    // for Arbitrary Polygons" by Hormann and Agathos.
    //
    // R: the point in question
    // pnts: vector of points defining polygon, first and last point should be the same
    //
    // Note: This algorithm does not test to see if the test point lies on the
    //       an edge of the polygon

    int w = 0; // winding number
    bool modify_w = false;

    for ( int i = 0; i < ( int )pnts.size() - 1; i++ )
    {
        if ( ( pnts[i].y() < R.y() ) != ( pnts[i + 1].y() < R.y() ) ) // if crossing
        {
            if ( pnts[i].x() >= R.x() )
            {
                if ( pnts[i + 1].x() > R.x() )
                {
                    modify_w = true;
                }
                else if ( ( det( pnts[i], pnts[i + 1], R ) > 0 ) == ( pnts[i + 1].y() > pnts[i].y() ) ) // right crossing
                {
                    modify_w = true;
                }
            }
            else if ( pnts[i + 1].x() > R.x() )
            {
                if ( ( det( pnts[i], pnts[i + 1], R ) > 0 ) == ( pnts[i + 1].y() > pnts[i].y() ) ) // right crossing
                {
                    modify_w = true;
                }
            }
        }

        if ( modify_w )
        {
            w = w + 2 * ( pnts[i + 1].y() > pnts[i].y() ) - 1;    // modify w
        }

        modify_w = false;
    }

    return !!(abs( w % 2 ));

}
Exemple #2
0
vec3d MapFromPlane( vec2d & uw, vec3d & B, vec3d & e0, vec3d & e1 )
{
    vec3d result = B + e0 * uw.x() + e1 * uw.y();
    return result;
}