src/Utilities/pointInPolygon.cc
//------------------------------------------------------------------------------
// Work with a closed polygon defined by it's vertices.
// We assume the caller has ordered the vertices (clockwise or 
// counter-clockwise), and should have already performed the box exclusion
// test for efficiency.
//------------------------------------------------------------------------------
Work with a closed polygon defined by it's vertices.
We assume the caller has ordered the vertices (clockwise or
counter-clockwise), and should have already performed the box exclusion
test for efficiency.
Work with a closed polygon defined by it ' s vertices . We assume the caller has ordered the vertices ( clockwise or counter - clockwise ) and should have already performed the box exclusion test for efficiency .
bool pointInPolygon(const Dim<2>::Vector& p,
                    const std::vector<Dim<2>::Vector>& vertices) {
  unsigned i, j;
  bool result = false;
  const unsigned nvertices = vertices.size();
  const double px = p.x();
  const double py = p.y();
  for (i = 0, j = nvertices - 1; i < nvertices; j = i++) {
    if ( ((vertices[i].y() > py) != (vertices[j].y() > py)) &&
	 (px < (vertices[j].x() - vertices[i].x()) * (py - vertices[i].y()) / (vertices[j].y() - vertices[i].y()) + vertices[i].x()) )
      result = not result;
  }
  return result;
}
bool pointInPolygon ( const Dim < 2 > :: Vector & p , const std :: vector < Dim < 2 > :: Vector > & vertices ) { unsigned i , j ; bool result = false ; const unsigned nvertices = vertices . size ( ) ; const double px = p . x ( ) ; const double py = p . y ( ) ; for ( i = 0 , j = nvertices - 1 ; i < nvertices ; j = i ++ ) { if ( ( ( vertices [ i ] . y ( ) > py ) != ( vertices [ j ] . y ( ) > py ) ) && ( px < ( vertices [ j ] . x ( ) - vertices [ i ] . x ( ) ) * ( py - vertices [ i ] . y ( ) ) / ( vertices [ j ] . y ( ) - vertices [ i ] . y ( ) ) + vertices [ i ] . x ( ) ) ) result = not result ; } return result ; }
Work with a closed polygon defined by it's vertices.
Work with a closed polygon defined by it ' s vertices .
// Now we do the test of casting a semi-infinite ray in the x direction from  // the point and counting intersections with the polygon. // Note this bit of code will reject points lying exactly on the boundary // of a polygon. // That's it.