Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: C++  |  size: 1.11 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. int area2(tpoint a,tpoint b,tpoint c)
  2. {
  3.  
  4.     return  ((b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y));
  5.  
  6. }
  7.  
  8.  
  9. bool left(tpoint a,tpoint b,tpoint c)
  10. {
  11.       return area2(a,b,c)>0;
  12. }
  13.  
  14.  
  15. bool lefton(tpoint a,tpoint b,tpoint c)
  16. {
  17.       return area2(a,b,c) >= 0;
  18.  
  19. }
  20.  
  21.  
  22. bool collinear(tpoint a,tpoint b,tpoint c)
  23. {
  24.  
  25.    return area2(a,b,c)==0;
  26.  
  27. }
  28.  
  29.  
  30. bool intersectprop(tpoint a,tpoint b,tpoint c,tpoint d)
  31. {
  32.      if(collinear(a,b,c) || collinear(a,b,d) || collinear(c,d,a) || collinear(c,d,b)) return false;
  33.  
  34.          else return area2(a,b,c) * area2(a,b,d) <0 && area2(c,d,a) * area2(c,d,b)<0;
  35. }
  36.  
  37.  
  38. bool between(tpoint a,tpoint b,tpoint c)
  39. {
  40.      //tpoint ba,ca;
  41.  
  42.          if(! collinear(a,b,c)) return false;
  43.  
  44.          if(a.x!=b.x) return ((a.x <= c.x) && (c.x <= b.x)) || ((a.x >= c.x) && (c.x>=b.x));
  45.  
  46.          else
  47.           return ((a.y<=c.y) && (c.y <= b.y)) ||
  48.                           ((a.y>=c.y ) && c.y>= b.y);
  49. }
  50.  
  51.  
  52. bool intersect(tpoint a,tpoint b,tpoint c,tpoint d)
  53. {
  54.    if( intersectprop(a,b,c,d) ) return true;
  55.  
  56.    else if(between(a,b,c) || between(a,b,d)||between(c,d,a) || between(c,d,b)) return true;
  57.  
  58.    else return false;
  59.  
  60. }