Advertisement
metalni

OOP Labs 1 Presek na otsecka

May 30th, 2020
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct Point {
  5.    int x, y;
  6. };
  7.  
  8. struct line {
  9.    Point p1, p2;
  10. };
  11.  
  12. bool onLine(line l1, Point p) {  
  13.    if(p.x <= max(l1.p1.x, l1.p2.x)&&p.x <= min(l1.p1.x, l1.p2.x)&&(p.y <= max(l1.p1.y, l1.p2.y)&&p.y <= min(l1.p1.y, l1.p2.y)))
  14.       return true;
  15.    
  16.    return false;
  17. }
  18.  
  19. int direction(Point a, Point b, Point c) {
  20.    int val = (b.y-a.y)*(c.x-b.x)-(b.x-a.x)*(c.y-b.y);
  21.    if (val == 0)
  22.       return 0;    
  23.    else if(val < 0)
  24.       return 2;    
  25.       return 1;    
  26. }
  27.  
  28. bool isIntersect(line l1, line l2) {
  29.    int dir1 = direction(l1.p1, l1.p2, l2.p1);
  30.    int dir2 = direction(l1.p1, l1.p2, l2.p2);
  31.    int dir3 = direction(l2.p1, l2.p2, l1.p1);
  32.    int dir4 = direction(l2.p1, l2.p2, l1.p2);
  33.    
  34.    if(dir1 != dir2&&dir3 != dir4)
  35.       return true;
  36.  
  37.    if(dir1==0&&onLine(l1, l2.p1))
  38.       return true;
  39.  
  40.    if(dir2==0 && onLine(l1, l2.p2))
  41.       return true;
  42.  
  43.    if(dir3==0 && onLine(l2, l1.p1))
  44.       return true;
  45.  
  46.    if(dir4==0 && onLine(l2, l1.p2))
  47.       return true;
  48.          
  49.    return false;
  50. }
  51.  
  52. int main() {
  53.  //  line l1 = {{-2,-2}, {2, 0}};
  54.  //  line l2 = {{0, 1}, {2, 2}};
  55.      line l1, l2;
  56.      cin>>l1.p1.x>>l1.p1.y>>l1.p2.x>>l1.p2.y;
  57.      cin>>l2.p1.x>>l2.p1.y>>l2.p2.x>>l2.p2.y;
  58.  
  59.    if(isIntersect(l1, l2))
  60.       cout << "1";
  61.    else
  62.       cout << "0";
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement