Advertisement
Guest User

gp hw

a guest
Nov 17th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. using namespace std;
  5.  
  6. double triangleSquare(double x1,double y1,double x2,double y2){
  7.     return abs(x1*y2 - x2*y1)/2.0;
  8. }
  9.  
  10. struct Point{
  11.     double x = .0,y = .0;
  12.     double distanceTo(Point point){
  13.         return sqrt(sqrt((point.x - x)*(point.x - x) + (point.y - y)*(point.y - y)));
  14.     }
  15.     bool operator == (const Point &point) const {
  16.         return this->x == point.x && this->y == point.y;
  17.     }
  18. };
  19.  
  20. struct Polygone{
  21.     Point * points;
  22.     int numOfVert;
  23.    
  24.     bool isIncludesIn(Polygone p){
  25.         bool isIncludes = 1;
  26.         int n = numOfVert, m = p.numOfVert;
  27.         for(int i = 0;i < n;i++){
  28.             Point P = points[i];
  29.             double S = .0;
  30.             for(int j = 0;j < m-1;++j){
  31.                 S += triangleSquare(p.points[j].x - P.x,p.points[j].y - P.y,
  32.                                     p.points[j+1].x - P.x,p.points[j+1].y - P.y);
  33.             }
  34.             S += triangleSquare(p.points[m-1].x - P.x,p.points[m-1].y - P.y,
  35.                                     p.points[0].x - P.x,p.points[0].y - P.y);
  36.             isIncludes *= (abs(S - p.square()) < 1e-9);
  37.         }
  38.         return isIncludes;
  39.     }
  40.    
  41.     double square(){
  42.         double S = .0;
  43.         for(int i = 1;i < numOfVert-1;++i){
  44.             S+= triangleSquare(points[i].x-points[0].x,points[i].y-points[0].y,
  45.                                 points[i+1].x-points[0].x,points[i+1].y-points[0].y);
  46.         }
  47.         return S;
  48.     }
  49. };
  50.  
  51. int main(){
  52.     int n;
  53.     cout << "Введите кол-во вершин первого многоугольника\n";
  54.     cin >> n;
  55.     cout << "Вводите вершины в виде точек с координатами (x,y) в порядке обхода многоугольника по/против часовой стрелки\n"
  56.     Polygne p1,p2;
  57.     p1.points = new Point[n];
  58.     p1.numOfVert = n;
  59.     for(int i = 0;i < n;++i){
  60.         cin >> p1.points[i].x >> p1.points[i].y;
  61.     }
  62.     int m;
  63.     cout << "Введите кол-во вершин второго многоугольника\n";
  64.     cin >> m;
  65.     cout << "Вводите вершины в виде точек с координатами (x,y) в порядке обхода многоугольника по/против часовой стрелки\n"
  66.     p2.points = new Point[m];
  67.     p2.numOfVert = m;
  68.     for(int i = 0;i < m;++i){
  69.         cin >> p2.points[i].x >> p2.points[i].y;
  70.     }
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement