Advertisement
pasholnahuy

Untitled

Oct 10th, 2023
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. //x1y2-x2y1
  6.  
  7. double line_and_point(double x1, double y1, double x2, double y2) {
  8.     return x1 * y2 - x2 * y1;
  9. }
  10.  
  11. bool check_segment_point(double x, double y, double x1, double y1, double x2, double y2) {
  12.     double a = y2 - y1;
  13.     double b = x1 - x2;
  14.     double c = -(a * x2 + b * y2);
  15.     return (a * x + b * y + c == 0 && (x1 - x) * (x2 - x) + (y1 - y) * (y2 - y) <= 0);
  16. }
  17.  
  18. int main() {
  19.     double x1, y1, x2, y2, x3, y3, x4, y4;
  20.     cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;
  21.     double p1 = line_and_point(x2 - x1, y2 - y1, x3 - x1, y3 - y1);
  22.     double p2 = line_and_point(x2 - x1, y2 - y1, x4 - x1, y4 - y1);
  23.     double p3 = line_and_point(x4 - x3, y4 - y3, x1 - x3, y1 - y3);
  24.     double p4 = line_and_point(x4 - x3, y4 - y3, x2 - x3, y2 - y3);
  25.     if (p1 * p2 < 0 && p3 * p4 < 0 || (check_segment_point(x1, y1, x3, y3, x4, y4) ||
  26.         check_segment_point(x2, y2, x3, y3, x4, y4) || check_segment_point(x3, y3, x1, y1, x2, y2) ||
  27.         check_segment_point(x4, y4, x1, y1, x2, y2))) {
  28.         cout << "YES";
  29.     } else {
  30.         cout << "NO";
  31.     }
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement