warriorscats

poligon

May 29th, 2020
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cstdlib>
  5. #include <ctime>
  6.  
  7. #define OO 10000
  8. struct Point
  9. {
  10.     int x, y;
  11.  
  12.     friend std::ifstream& operator >> (std::ifstream &flux, Point &p)
  13.     {
  14.         flux >> p.x >> p.y;
  15.         return flux;
  16.     }
  17.  
  18.     friend std::ofstream& operator << (std::ofstream &flux, Point p)
  19.     {
  20.         flux << "(" << p.x << ", " << p.y << ")";
  21.         return flux;
  22.     }
  23. };
  24.  
  25. std::ifstream fin ("punctinpoligonsimplu.in");
  26. std::ofstream fout ("punctinpoligonsimplu.out");
  27.  
  28. Point p0;
  29. int n, m;
  30. std::vector <Point> polygon;
  31.  
  32. inline bool onSegment (Point p, Point q, Point r)
  33. {
  34.     return (std::min (p.x, r.x) <= q.x && q.x <= std::max (p.x, r.x) &&
  35.             std::min (p.y, r.y) <= q.y && q.y <= std::max (p.y, r.y));
  36. }
  37.  
  38. inline int orientation (Point p, Point q, Point r)
  39. {
  40.     int val = (q.y - p.y) * (r.x - q.x) -
  41.               (q.x - p.x) * (r.y - q.y);
  42.  
  43.     if (!val) return 0;
  44.     return (val > 0 ? 1 : -1);
  45. }
  46.  
  47. inline bool doIntersect (Point p1, Point q1, Point p2, Point q2)
  48. {
  49.     int o1 = orientation (p1, q1, p2);
  50.     int o2 = orientation (p1, q1, q2);
  51.     int o3 = orientation (p2, q2, p1);
  52.     int o4 = orientation (p2, q2, q1);
  53.  
  54.     if (o1 != o2 && o3 != o4) return true;
  55.  
  56.     if (!o1 && onSegment (p1, p2, q1)) return true;
  57.  
  58.     if (!o2 && onSegment (p1, q2, q1)) return true;
  59.  
  60.     if (!o3 && onSegment (p2, p1, q2)) return true;
  61.  
  62.     if (!o4 && onSegment (p2, q1, q2)) return true;
  63.  
  64.     return false;
  65. }
  66.  
  67. inline bool isInside (Point p)
  68. {
  69.     if (n < 3) return false;
  70.  
  71.     std::vector <bool> Seen (n, false);
  72.     Point extreme = {rand () % 1001 + OO, rand () % 1001 + OO};
  73.     int cnt = 0, i = 0;
  74.     do
  75.     {
  76.         int next = (i + 1) % n;
  77.  
  78.         if (doIntersect (polygon[i], polygon[next], p, extreme))
  79.         {
  80.             //if (!orientation (polygon[i], p, polygon[next]))
  81.                 //return onSegment (polygon[i], p, polygon[next]);
  82.             if (!orientation (polygon[i], p, polygon[next]) && onSegment(polygon[i], p, polygon[next]))
  83.                 return true;
  84.  
  85.             if ((!orientation (p, polygon[i], extreme) && !Seen[i]))
  86.             {
  87.                 ++ cnt, Seen[i] = true;
  88.                 /*fout << "[";
  89.                 fout << polygon[i];
  90.                 fout << ", ";
  91.                 fout << polygon[next];
  92.                 fout << "], ";*/
  93.                 goto label;
  94.             }
  95.  
  96.             if ((!orientation (p, polygon[next], extreme) && !Seen[next]))
  97.             {
  98.                 ++ cnt, Seen[next] = true;
  99.                 /*fout << "[";
  100.                 fout << polygon[i];
  101.                 fout << ", ";
  102.                 fout << polygon[next];
  103.                 fout << "], ";*/
  104.                 goto label;
  105.             }
  106.  
  107.             if (orientation (p, polygon[i], extreme) && orientation (p, polygon[next], extreme))
  108.             {
  109.                 ++ cnt;
  110.                 /*fout << "[";
  111.                 fout << polygon[i];
  112.                 fout << ", ";
  113.                 fout << polygon[next];
  114.                 fout << "], ";*/
  115.             }
  116.         }
  117.         label : i = next;
  118.     } while (i);
  119.  
  120.     //fout << cnt << " => ";
  121.     return (cnt & 1);
  122. }
  123.  
  124. int main ()
  125. {
  126.     std::srand (std::time (nullptr));
  127.  
  128.     fin >> n >> m;
  129.  
  130.     polygon = std::vector <Point> (n, {0, 0});
  131.  
  132.     for (int i = 0; i < n; ++ i)
  133.         fin >> polygon[i];
  134.  
  135.     for ( ; m; -- m)
  136.     {
  137.         Point p;
  138.         fin >> p;
  139.         //fout << p;
  140.         //fout << ": ";
  141.         fout << (isInside (p) ? "DA" : "NU") << "\n";
  142.     }
  143.  
  144.     return 0;
  145. }
Add Comment
Please, Sign In to add comment