Advertisement
Guest User

linesegment

a guest
Jun 22nd, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.12 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3.  
  4. using namespace std;
  5.  
  6. class Point{
  7. public:
  8.     int x,y;
  9.     Point(){}
  10.     Point(int a, int b){
  11.         x=a;
  12.         y=b;
  13.     }
  14. };
  15.  
  16. int okret(Point a, Point b, Point c){
  17.     if((b.y-a.y)*(c.x-b.x)-(c.y-b.y)*(b.x-a.x)<0) return -1;
  18.     if((b.y-a.y)*(c.x-b.x)-(c.y-b.y)*(b.x-a.x)>0) return 1;
  19.     return 0;
  20. }
  21.  
  22. bool naSegmentu(Point a,Point b, Point toCheck){
  23.     if(toCheck.x<min(a.x,b.x) || toCheck.x>max(a.x,b.x) || toCheck.y<min(a.y,b.y) || toCheck.y>max(a.y,b.y)) return false;
  24.     return true;
  25. }
  26.  
  27. bool dodirujuSe(Point a, Point b, Point c, Point d){
  28.     if(okret(a,b,c)!=okret(a,b,d) && okret(c,d,a)!=okret(c,d,b)) return true;
  29.     if(okret(a,b,c)==0 && naSegmentu(a,b,c)) return true;
  30.     if(okret(a,b,d)==0 && naSegmentu(a,b,d)) return true;
  31.     if(okret(c,d,a)==0 && naSegmentu(c,d,a)) return true;
  32.     if(okret(c,d,b)==0 && naSegmentu(c,d,b)) return true;
  33.     return false;
  34. }
  35.  
  36. int main(){
  37.  
  38.     Point a, b, c, d;
  39.     cin >> a.x >> a.y;
  40.     cin >> b.x >> b.y;
  41.     cin >> c.x >> c.y;
  42.     cin >> d.x >> d.y;
  43.     cout<<dodirujuSe(a,b,c,d)<<endl;
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement