Advertisement
Guest User

mn

a guest
Nov 15th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. using namespace std;
  5.  
  6. double vectorProd(double x1,double y1,double x2,double y2){
  7.     return x1*y2 - x2*y1;
  8. }
  9.  
  10. bool signsIsDifferent(double a,double b){
  11.     return a <= 0 && b >= 0 || a >= 0 && b <= 0;
  12. }
  13.  
  14.  
  15. struct Point{
  16.     double x = .0,y = .0;
  17.     double distanceTo(Point point){
  18.         return sqrt(sqrt((point.x - x)*(point.x - x) + (point.y - y)*(point.y - y)));
  19.     }
  20.     bool operator == (const Point &point) const {
  21.         return this->x == point.x && this->y == point.y;
  22.     }
  23. };
  24.  
  25. struct Line{
  26.   Point l,r;
  27.   bool isParallelTo(Line line){
  28.       double x1 = r.x - l.x;
  29.       double y1 = r.y - l.y;
  30.       double x2 = line.r.x - line.l.x;
  31.       double y2 = line.r.y - line.l.y;
  32.       return (abs(vectorProd(x1,y1,x2,y2)) < 1e-8);
  33.   }
  34.   bool isCrossesTo(Line line){
  35.       Point l1 = line.l;
  36.       Point r1 = line.r;
  37.       double v1 = vectorProd(l1.x - l.x,l1.y - l.y,r.x - l.x,r.y - l.y);
  38.       double v2 = vectorProd(r1.x - l.x,r1.y - l.y,r.x - l.x,r.y - l.y);
  39.       double v3 = vectorProd(r.x - r1.x,r.y - r1.y,l1.x - r1.x, l1.y - r1.y);
  40.       double v4 = vectorProd(l.x - r1.x,l.y - r1.y,l1.x - r1.x, l1.y - r1.y);
  41.       int c = (v1 == 0) + (v2 == 0) + (v3 == 0) + (v4 == 0);
  42.       if(l1 == l || r1 == l || l1 == r || r1 == r){
  43.           return 1;
  44.       }
  45.       if(c == 4){
  46.           if(l.distanceTo(r1) + r1.distanceTo(r) == l.distanceTo(r) || l1.distanceTo(r) + r.distanceTo(r1) == l1.distanceTo(r1))return 1;
  47.       }
  48.       return signsIsDifferent(v1,v2) && signsIsDifferent(v3,v4);
  49.   }
  50. };
  51.  
  52.  
  53. int howAreTheLines(Line l1,Line l2){
  54.     if(l1.isParallelTo(l2) && l1.isCrossesTo(l2)) return 2;
  55.     if(l1.isParallelTo(l2)) return 1;
  56.     if(l1.isCrossesTo(l2)) return -1;
  57.     return 0;
  58. }
  59.  
  60. int main(){
  61.     int n;
  62.     cin >> n;
  63.     Line * arr = new Line[n];
  64.     for(int i = 0;i < n;++i){
  65.         cout << "Введите координаты начала и конца отрезка\n";
  66.         Point p1,p2;
  67.         cin >> p1.x >> p1.y >> p2.x >> p2.y;
  68.         Line line;
  69.         line.l = p1;
  70.         line.r = p2;
  71.         arr[i] = line;
  72.     }
  73.     for(int i = 0;i < n;++i){
  74.         for(int j = i+1;j < n;++j){
  75.             int ans = howAreTheLines(arr[i],arr[j]);
  76.             if(ans == 2) cout << "Отрезки " << i+1 << " и " << j+1 << " пересекаются и паралельны\n";
  77.             else if(ans == -1)cout << "Отрезки " << i+1 << " и " << j+1 << " пересекаются\n";
  78.             else if(ans == 1) cout << "Отрезки " << i+1 << " и " << j+1 << " параллельны\n";
  79.             else cout << "Отрезки " << i+1 << " и " << j+1 << " ни пересекаются, ни параллельны\n";
  80.         }
  81.     }
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement