Alex_tz307

Intersectie Segmente + Punct pe Segment

Dec 17th, 2020 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define pi pair<int,int>
  3. #define x first
  4. #define y second
  5.  
  6. using namespace std;
  7.  
  8. ifstream fin("intersectiesegmente.in");
  9. ofstream fout("intersectiesegmente.out");
  10.  
  11. int det(const pi &A, const pi &B, const pi &C) {
  12.     return (B.x - A.x) * (C.y - A.y) - (C.x - A.x) * (B.y - A.y);
  13. }
  14.  
  15. bool check(const pi &A, const pi &B, const pi &C) {
  16.     vector<pi> a{A, B, C};
  17.     int X = a[2].x, Y = a[2].y;
  18.     sort(a.begin(), a.end());
  19.     if(!(X == a[1].x && Y == a[1].y))
  20.         return false;
  21.     if(det(a[0], a[1], a[2]) == 0)
  22.         return true;
  23.     return false;
  24. }
  25.  
  26. int main() {
  27.     vector<pi> p(4);
  28.     for(pi &x : p)
  29.         fin >> x.x >> x.y;
  30.     if(check(p[0], p[1], p[2])) {
  31.         fout << "DA";
  32.         return 0;
  33.     }
  34.     if(check(p[0], p[1], p[3])) {
  35.         fout << "DA";
  36.         return 0;
  37.     }
  38.     if(check(p[2], p[3], p[0])) {
  39.         fout << "DA";
  40.         return 0;
  41.     }
  42.     if(check(p[2], p[3], p[1])) {
  43.         fout << "DA";
  44.         return 0;
  45.     }
  46.     int d1 = det(p[0], p[1], p[2]),
  47.         d2 = det(p[0], p[1], p[3]),
  48.         d3 = det(p[2], p[3], p[0]),
  49.         d4 = det(p[2], p[3], p[1]);
  50.     if(d1 * d2 < 0 && d3 * d4 < 0)
  51.         fout << "DA";
  52.     else
  53.         fout << "NU";
  54. }
  55.  
Add Comment
Please, Sign In to add comment