point_in_poly.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "point_in_poly.h"
  9. bool IGL_INLINE igl::point_in_poly( const std::vector<std::vector<unsigned int > >&poly,
  10. const unsigned int xt,
  11. const unsigned int yt)
  12. {
  13. int npoints= poly.size();
  14. unsigned int xnew,ynew;
  15. unsigned int xold,yold;
  16. unsigned int x1,y1;
  17. unsigned int x2,y2;
  18. int i;
  19. int inside=0;
  20. if (npoints < 3) {
  21. return(0);
  22. }
  23. xold=poly[npoints-1][0];
  24. yold=poly[npoints-1][1];
  25. for (i=0 ; i < npoints ; i++) {
  26. xnew=poly[i][0];
  27. ynew=poly[i][1];
  28. if (xnew > xold) {
  29. x1=xold;
  30. x2=xnew;
  31. y1=yold;
  32. y2=ynew;
  33. }
  34. else {
  35. x1=xnew;
  36. x2=xold;
  37. y1=ynew;
  38. y2=yold;
  39. }
  40. if ((xnew < xt) == (xt <= xold) /* edge "open" at one end */
  41. && ((long)yt-(long)y1)*(long)(x2-x1)
  42. < ((long)y2-(long)y1)*(long)(xt-x1)) {
  43. inside=!inside;
  44. }
  45. xold=xnew;
  46. yold=ynew;
  47. }
  48. return (inside != 0);
  49. }