DuongNhi99

PointInPolygon (Điểm thuộc Đa giác)

Jan 7th, 2022 (edited)
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. int fEqual(double a, double b) {
  2.   return (fabs(a - b) < eps) ? 1 : 0;
  3. }
  4. bool CW(point a, point b, point c) { // Cùng chiều kim đồng hồ
  5.     return a.x*(c.y-b.y) + b.x*(a.y-c.y) + c.x*(b.y-a.y) > 0;
  6. }
  7.  
  8. bool CCW(point a, point b, point c) { // Ngược chiều kim đồng hồ
  9.     return a.x*(c.y-b.y) + b.x*(a.y-c.y) + c.x*(b.y-a.y) < 0;
  10. }
  11.  
  12. bool check(point &p) {
  13.     if (hull.size() < 3)
  14.         return false;
  15.     int l = 1, r = hull.size() - 1;
  16.  
  17.     if (CCW(hull[0], hull[l], hull[r]))
  18.         swap(l, r);
  19.  
  20.     if (CCW(hull[0], hull[l], p) || CW(hull[0], hull[r], p))
  21.         return false;
  22.  
  23.     while (abs(r - l) > 1) {
  24.         int m = (l + r) / 2;
  25.  
  26.         if (CW(hull[0], hull[m], p))
  27.             l = m;
  28.         else
  29.             r = m;
  30.     }
  31.  
  32.     if (CCW(hull[l], hull[r], p)) return false;
  33.     return true;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment