Advertisement
Guest User

PointInPolygon

a guest
Nov 29th, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include <vector>
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <deque>
  6. #include <stack>
  7. #include <bitset>
  8. #include <algorithm>
  9. #include <functional>
  10. #include <numeric>
  11. #include <utility>
  12. #include <sstream>
  13. #include <iostream>
  14. #include <iomanip>
  15. #include <cstdio>
  16. #include <cmath>
  17. #include <cstdlib>
  18. #include <ctime>
  19.  
  20. using namespace std;
  21. #define eps 1e-9
  22.  
  23. class PointInPolygon
  24. {
  25. public:
  26.     string testPoint(vector <string>, int, int);
  27. };
  28.  
  29. struct point
  30. {
  31.     int x,y;
  32. };
  33.  
  34. bool isOnSeg(point a,point b,point c)
  35. {
  36.     int minx=min(a.x,b.x);
  37.     int maxx=max(a.x,b.x);
  38.     int miny=min(a.y,b.y);
  39.     int maxy=max(a.y,b.y);
  40.     return (c.x>=minx and c.x<=maxx and c.y>=miny and c.y<=maxy);
  41. }
  42.  
  43. int orientation(point a,point b,point c)
  44. {
  45.     /// By slope formula...
  46.     int delta=(c.x-b.x)*(b.y-a.y)-(b.x-a.x)*(c.y-b.y);
  47.     if(delta==0) return 0;
  48.     if(delta<0) return -1;
  49.     return +1;
  50. }
  51.  
  52. bool is_Segment_Segment_Intersection(point a,point b,point c,point d)
  53. {
  54.     int o1=orientation(a,b,c);
  55.     int o2=orientation(a,b,d);
  56.     int o3=orientation(c,d,a);
  57.     int o4=orientation(c,d,b);
  58.     if(o1!=o2 and o3!=o4) return true;
  59.     return false;
  60. }
  61.  
  62. string PointInPolygon::testPoint(vector <string> vrt, int X, int Y)
  63. {
  64.     vector<point>v;
  65.     int i,sz,cnt;
  66.     stringstream ss;
  67.     point pts,a,b,s,t;
  68.     sz=vrt.size();
  69.     for(i=0; i<sz; i++)
  70.     {
  71.         ss.str(vrt[i]);
  72.         ss>>pts.x;
  73.         ss>>pts.y;
  74.         v.push_back(pts);
  75.         ss.clear();
  76.     }
  77.     s.x=X;
  78.     s.y=Y;
  79.     t.x=700;
  80.     t.y=Y;
  81.     cnt=0;
  82.     for(i=0; i<sz; i++)
  83.     {
  84.         a.x=v[i].x;
  85.         a.y=v[i].y;
  86.         b.x=v[(i+1)%sz].x;
  87.         b.y=v[(i+1)%sz].y;
  88.         if(isOnSeg(a,b,s)) return "BOUNDARY";
  89.         if(is_Segment_Segment_Intersection(a,b,s,t)) ++cnt;
  90.     }
  91.     if(cnt&1) return "INTERIOR";
  92.     return "EXTERIOR";
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement