Advertisement
nasarouf

line-circle intersection test

Apr 20th, 2017
697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. //circle-line intersection
  2. //nasarouf@cs.ubc.ca
  3.  
  4. // from tju1607.cpp http://acm.tju.edu.cn/toj/showp1607.html
  5. // actually does a ray intersection test. but should work for lines too.
  6. // finds the int pt closest to pos
  7. // for ray intersection, if t<0, there is no intersection
  8.  
  9. #define X first
  10. #define Y second
  11. double hypot(double x, double y){   return sqrt(x*x + y*y); }
  12. #define area2(x1,y1,x2,y2,x3,y3) ((x1)*(y2)+(x2)*(y3)+(x3)*(y1))-((y1)*(x2)+(y2)*(x3)+(y3)*(x1))
  13. typedef pair<double,double> pdd;
  14. pdd unit(pdd d){   double h=hypot(d.X,d.Y);   return make_pair(d.X/h,d.Y/h); }
  15. pdd operator+(pdd a, pdd b){   return make_pair(a.X+b.X,a.Y+b.Y); }
  16. pdd operator*(pdd a, double t){   return make_pair(a.X*t,a.Y*t); }
  17. double dot(pdd a, pdd b){   return a.X*b.X + a.Y*b.Y; }
  18.  
  19. //dir has to be a unit vector
  20. //returns: pair<yes=1/no=0, t(param, how far is the intersection pt)>
  21. pair<int,double> line_intersects_circle(double x, double y, double r, pdd pos, pdd dir){
  22.   pos.X-=x;   pos.Y-=y;
  23.   x=y=0;
  24.   if (dir.X<0){     dir.X=-dir.X;     pos.X=-pos.X;   }
  25.   if (dir.Y<0){     dir.Y=-dir.Y;     pos.Y=-pos.Y;   }
  26.   pdd pos1=pos+dir;
  27.   double h=area2(pos.X,pos.Y,pos1.X,pos1.Y,x,y); //since base=|dir|=1
  28.   if (r<=abs(h)) return make_pair<int,double>(0,0.0);
  29.   pdd dir1=make_pair(dir.Y,-dir.X);
  30.   pos1=dir1*h;//+dir*(-sqrt(r*r-h*h));
  31.   pos1=pos1+pos*(-1);
  32.   double d,d1=sqrt(r*r-h*h);
  33.   if (abs(dir.X)>abs(dir.Y)) d=pos1.X/dir.X;
  34.   else d=pos1.Y/dir.Y;
  35.   if (d>0) return make_pair<int,double>(1,d-d1);
  36.   else return make_pair<int,double>(1,d+d1);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement