Advertisement
Guest User

D

a guest
Feb 25th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. struct Point {
  8.     double x, y;
  9.  
  10.     Point(double x0 = 0, double y0 = 0)
  11.     {
  12.         x = x0;
  13.         y = y0;
  14.     }
  15. };
  16.  
  17. istream & operator >> (istream & in, Point & P) {
  18.     in >> P.x >> P.y;
  19.     return in;
  20. }
  21.  
  22.  
  23. struct Vector {
  24.     double x, y;
  25.  
  26.     Vector(double x0, double y0) {
  27.         x = x0;
  28.         y = y0;
  29.     }
  30.  
  31.     Vector(Point A, Point B) {
  32.         x = B.x - A.x;
  33.         y = B.y - A.y;
  34.     }
  35. };
  36.  
  37.  
  38. double vectMultipl(Vector a, Vector b) {
  39.     return a.x * b.y - b.x * a.y;
  40. }
  41.  
  42. double scalarMultipl(Vector a, Vector b) {
  43.     return a.x * b.x + b.y * a.y;
  44. }
  45.  
  46.  
  47. bool onSegment(Point C, Point A, Point B) {
  48.     Vector AB = Vector(A, B);
  49.     Vector AC = Vector(A, C);
  50.     Vector BA = Vector(B, A);
  51.     Vector BC = Vector(B, C);
  52.     if (vectMultipl(AB, AC) == 0 && AB.x * AC.x >= 0 && AB.y * AC.y >= 0 &&
  53.         vectMultipl(BA, BC) == 0 && BA.x * BC.x >= 0 && BA.y * BC.y >= 0)
  54.         return true;
  55.     else
  56.         return false;
  57.  }
  58.  
  59.  
  60. int main() {
  61.     int n;
  62.     Point O;
  63.     cin >> n >> O;
  64.     Point polygon[n];
  65.     for (int i = 0; i < n; i++)
  66.         cin >> polygon[i];
  67.     double angle = 0;
  68.     for (int i = 0; i < n; i++) {
  69.         if (onSegment(O, polygon[i], polygon[(i + 1) % n])) {
  70.             cout << "YES";
  71.             return 0;
  72.         }
  73.         Vector OPi = Vector(O, polygon[i]);
  74.         Vector OPi1 = Vector(O, polygon[(i + 1) % n]);
  75.         angle += atan2(vectMultipl(OPi, OPi1), scalarMultipl(OPi, OPi1));
  76.     }
  77.     if (abs(abs(angle) - 2 * M_PI) <= 0.1)
  78.         cout << "YES";
  79.     else
  80.         cout << "NO";
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement