// Copyright (C) 2010, Guy Barrand. All rights reserved. // See the file tools.license for terms. #ifndef tools_geom2 #define tools_geom2 #include #include namespace tools { template inline double is_left(const VEC2& P0,const VEC2& P1,const VEC2& P2){ return ( (P1.v0() - P0.v0()) * (P2.v1() - P0.v1()) - (P2.v0() - P0.v0()) * (P1.v1() - P0.v1()) ); } template inline bool is_inside(const VEC2& a_P,const std::vector& a_V) { // V[] = vertex points of a polygon V[n+1] with V[n]=V[0] // From : // http://softsurfer.com/Archive/algorithm_0103/algorithm_0103.htm // Copyright 2001, softSurfer (www.softsurfer.com) // This code may be freely used and modified for any purpose // providing that this copyright notice is included with it. // SoftSurfer makes no warranty for this code, and cannot be held // liable for any real or imagined damage resulting from its use. // Users of this code must verify correctness for their application. size_t n = a_V.size()-1; int wn = 0; // the winding number counter // loop through all edges of the polygon for (size_t i=0; i a_P.v1()) // an upward crossing if (is_left( a_V[i], a_V[i+1], a_P) > 0) // P left of edge ++wn; // have a valid up intersect } else { // start y > P[1] (no test needed) if (a_V[i+1].v1() <= a_P.v1()) // a downward crossing if (is_left( a_V[i], a_V[i+1], a_P) < 0) // P right of edge --wn; // have a valid down intersect } } return ((wn!=0)?true:false); } // the same done with std::pair. template inline double is_left(const std::pair& P0,const std::pair& P1,const std::pair& P2){ return ( (P1.first - P0.first) * (P2.second - P0.second) - (P2.first - P0.first) * (P1.second - P0.second) ); } template inline bool inside(const std::pair& a_P,const std::vector< std::pair >& a_V) { // V[] = vertex points of a polygon V[n+1] with V[n]=V[0] // From : // http://softsurfer.com/Archive/algorithm_0103/algorithm_0103.htm // Copyright 2001, softSurfer (www.softsurfer.com) // This code may be freely used and modified for any purpose // providing that this copyright notice is included with it. // SoftSurfer makes no warranty for this code, and cannot be held // liable for any real or imagined damage resulting from its use. // Users of this code must verify correctness for their application. size_t n = a_V.size()-1; int wn = 0; // the winding number counter // loop through all edges of the polygon for (size_t i=0; i a_P.second) // an upward crossing if (is_left( a_V[i], a_V[i+1], a_P) > 0) // P left of edge ++wn; // have a valid up intersect } else { // start y > P[1] (no test needed) if (a_V[i+1].second <= a_P.second) // a downward crossing if (is_left( a_V[i], a_V[i+1], a_P) < 0) // P right of edge --wn; // have a valid down intersect } } return ((wn!=0)?true:false); } } #endif