Ember

Untitled

Jun 17th, 2011
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. struct POINT { float x, y; };
  2. struct EDGE { POINT p[2]; };
  3.  
  4. bool intersect (float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, POINT *res=NULL)
  5. { float x1_x2=x1-x2, x3_x4=x3-x4, y1_y2=y1-y2, y3_y4=y3-y4;
  6. float div=x1_x2*y3_y4-y1_y2*x3_x4;
  7. if (div==0) return false;
  8. float x1_y1_x2_y2=x1*y2-y1*x2, x3_y3_x4_y4=x3*y4-y3*x4,
  9. x=(x1_y1_x2_y2*x3_x4-x1_x2*x3_y3_x4_y4)/div;
  10. if ((x1-x)*(x2-x)>0 || (x3-x)*(x4-x)>0) return false;
  11. if (res!=NULL) { float y=(x1_y1_x2_y2*y3_y4-y1_y2*x3_y3_x4_y4)/div;
  12. (*res).x=x;
  13. (*res).y=y; }
  14. return true; }
  15.  
  16. inline bool intersect(POINT p1,POINT p2,POINT p3,POINT p4,POINT *res=NULL){ return intersect(p1.x,p1.y,p2.x,p2.y, p3.x,p3.y,p4.x,p4.y, res); }
  17. inline bool intersect(EDGE e1, EDGE e2, POINT *res=NULL) { return intersect(e1.p[0],e1.p[1], e2.p[0],e2.p[1], res); }
Advertisement
Add Comment
Please, Sign In to add comment