point_in_poly.cpp 933 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "point_in_poly.h"
  2. bool IGL_INLINE igl::point_in_poly( const std::vector<std::vector<unsigned int > >&poly,
  3. const unsigned int xt,
  4. const unsigned int yt)
  5. {
  6. int npoints= poly.size();
  7. unsigned int xnew,ynew;
  8. unsigned int xold,yold;
  9. unsigned int x1,y1;
  10. unsigned int x2,y2;
  11. int i;
  12. int inside=0;
  13. if (npoints < 3) {
  14. return(0);
  15. }
  16. xold=poly[npoints-1][0];
  17. yold=poly[npoints-1][1];
  18. for (i=0 ; i < npoints ; i++) {
  19. xnew=poly[i][0];
  20. ynew=poly[i][1];
  21. if (xnew > xold) {
  22. x1=xold;
  23. x2=xnew;
  24. y1=yold;
  25. y2=ynew;
  26. }
  27. else {
  28. x1=xnew;
  29. x2=xold;
  30. y1=ynew;
  31. y2=yold;
  32. }
  33. if ((xnew < xt) == (xt <= xold) /* edge "open" at one end */
  34. && ((long)yt-(long)y1)*(long)(x2-x1)
  35. < ((long)y2-(long)y1)*(long)(xt-x1)) {
  36. inside=!inside;
  37. }
  38. xold=xnew;
  39. yold=ynew;
  40. }
  41. return (inside != 0);
  42. }