Advertisement
georgiy110802

Untitled

Oct 12th, 2021
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cmath>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. const double EPS = 1e-9;
  9.  
  10. struct Point {
  11.     double x;
  12.     double y;
  13.  
  14.     Point() = default;
  15.  
  16.     Point(double x_, double y_) : x(x_), y(y_) {}
  17.  
  18.     Point(const Point& p1, const Point& p2) {
  19.         x = p2.x - p1.x;
  20.         y = p2.y - p1.y;
  21.     }
  22.  
  23.     double length() const {
  24.         return hypot(x, y);
  25.     }
  26.  
  27.     double distanceTo(const Point& p2) const {
  28.         return hypot(x - p2.x, y - p2.y);
  29.     }
  30.  
  31.     Point operator+(const Point& p2) const {
  32.         return Point(x + p2.x, y + p2.y);
  33.     }
  34.  
  35.     Point operator-(const Point& p2) const {
  36.         return Point(x - p2.x, y - p2.y);
  37.     }
  38.  
  39.     Point operator*(const double& scalar) const {
  40.         return Point(x * scalar, y * scalar);
  41.     }
  42.  
  43.     bool operator==(const Point& p2) const {
  44.         return EPS > abs(x - p2.x) && EPS > abs(y - p2.y);
  45.     }
  46.  
  47.     bool operator<(const Point& p2) const {
  48.         if (EPS > abs(x - p2.x)) {
  49.             return y < p2.y;
  50.         } else {
  51.             return x < p2.x;
  52.         }
  53.     }
  54.  
  55.     double DotProduct(const Point& p2) const {
  56.         return x * p2.x + y * p2.y;
  57.     }
  58.  
  59.     double AngleBetween(const Point& p2) const {
  60.         return acos(DotProduct(p2) / length() / p2.length());
  61.     }
  62.  
  63.     bool isOrthogonal(const Point& p2) const {
  64.         return EPS > abs(DotProduct(p2));
  65.     }
  66.  
  67.     double CrossProduct(const Point& p2) const {
  68.         return x * p2.y - p2.x * y;
  69.     }
  70.  
  71.     bool isCollinear(const Point& p2) const {
  72.         return EPS > abs(CrossProduct(p2));
  73.     }
  74. };
  75.  
  76. istream& operator>>(istream& input, Point& point) {
  77.     input >> point.x >> point.y;
  78.     return input;
  79. }
  80.  
  81. ostream& operator<<(ostream& output, const Point& point) {
  82.     output << '(' << point.x << ", " << point.y << ")";
  83.     return output;
  84. }
  85.  
  86. struct Polygon {
  87.     vector<Point> points;
  88.  
  89.     void add(const Point& p) {
  90.         points.push_back(p);
  91.     }
  92.  
  93.     double perimeter () const {
  94.         double sum = 0;
  95.         for (int i = 0; i < points.size(); ++i) {
  96.             sum += points[i].distanceTo(points[(i + 1) % points.size()]);
  97.         }
  98.         return sum;
  99.     }
  100.  
  101.     double area() const {
  102.         double sum = 0;
  103.         for (int i = 0; i < points.size(); ++i) {
  104.             sum += points[i].CrossProduct(points[(i + 1) % points.size()]);
  105.         }
  106.         return abs(sum / 2);
  107.     }
  108.  
  109.     bool contains(const Point& p) {
  110.         bool plus = false, minus = false;
  111.         for (int i = 0; i < points.size(); ++i) {
  112.             Point &a = points[i], &b = points[(i + 1) % points.size()];
  113.             Point ab (a, b), ap (a, p);
  114.             double prod = ab.CrossProduct(ap);
  115.             if (prod < -EPS) {
  116.                 minus = true;
  117.             } else if (prod > EPS) {
  118.                 plus = true;
  119.             }
  120.         }
  121.         return !(minus & plus);
  122.     }
  123. };
  124.  
  125. int main() {
  126. //    int n;
  127. //    cin >> n;
  128.     Polygon _p{{{2, 2}, {2, 0}, {0, 0}, {0, 2}}};
  129.     Point other{2, 2};
  130.     cout << _p.contains(other);
  131. //    for (int i = 0; i < n; ++i) {
  132. //        Point p;
  133. //        cin >> p;
  134. //        _p.add(p);
  135. //    }
  136. //    cout << fixed << _p.area();
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement