www.pudn.com > roll.rar > INTERSEC.CPP
#include "stdafx.h" #include "intersec.h" #include "comnfun.h" #includeint AsinBcosC(double a,double b,double c,double *x) { int FLAG=0; double t; t=sqrt(a*a+b*b); if(fabs(t-fabs(c)) 0.0 && t1 < 1.0 && t2 > 0.0 && t2 < 1.0 ) return 1; if((t1 == 0.0 || t1 == 1.0) && (t2 == 0.0 || t2 == 1.0)) return 4; if( t1 == 0.0 || t1 == 1.0 ) return 2; if( t2 == 0.0 || t2 == 1.0 ) return 3; return 0; } // the function Arc_Arc_Int(...) calculate the intersection points // between two arcs describled below: // the one arc equation is x = x1+r1*cos(a1) and y = y1+r1*sin(a1); // another arc equation is x = x2+r2*cos(a2) and y = y2+r2*sin(a2); // in addition { s1 <= a1 <= e1 , s2 <= a2 <= e2 }. // the output is the a1[2] and the a2[2]. // return 0 implied there is not intersection point, // return 1 implied there is one intersection point[1], // return 2 implied there is one intersection point[2], // return 3 implied there is two intersection point[1] and point[2]. int Arc_Arc_Int (double x1,double y1,double s1,double e1,double r1,double x2,double y2,double s2,double e2,double r2,double x[2],double y[2]) { int ret = 2; int num = 0; int index = 0; double alf[2],bit[2]; if(Circle_Circle_InterSection(x1,y1,r1,x2,y2,r2,alf,bit)) return 0; x[0]=x1+r1*cos(alf[0]); x[1]=x1+r1*cos(alf[1]); y[0]=y1+r1*sin(alf[0]); y[1]=y1+r1*sin(alf[1]); if(fabs(x[1]-x[0]) = t ) bWithin = 1; if( bWithin ) { t = bit[i]; if( s2 <= t && e2 >= t ) { num++; index = i; } } } return num+index; } // the function Line_Arc_Int(...) calculate the intersection points of // the line and the arc describled below: // the line equation is x = x0+(xx1-x0)*t and y = y0+(yy1-y0)*t; // the arc equation is x = x1+r*cos(a) and y = y1+r*sin(a) {s<=a<=e}; // the output is the x[2] and y[2]; // return 0 implied there is not interpoint. // return 1 implied there is one intersection point[1], // return 2 implied there is one intersection point[2], // return 3 implied there is two intersection point[1] and point[2]. int Line_Arc_Int (double x0,double y0,double xx1,double yy1,double x1,double y1,double s,double e,double r,double x[2],double y[2]) { double t[2]; int index=0; int num = 0; double Dx = xx1-x0; double Dy = yy1-y0; if( !Line_Circle_Int(x0,y0,Dx,Dy,x1,y1,r,t)) return 0; for(int i=0;i<2;i++) { if( t[i]<0.0 || t[i]>1.0 ) continue; x[i] = x0+Dx*t[i]; y[i] = y0+Dy*t[i]; double tt; tt = atan2(y[i]-y1,x[i]-x1); if( stt ) { num++; index = i; } } return num+index; } // the function Line_Circle_Int(...) calculate the intersection points of // the line and the circle describled below: // the line equation is x = x0+Dx*t and y = y0+Dy*t; // the circle equation is (x-x1)**2+(y-y1)**2 = r**2; // the output is the t[2]; // return 0 implied there is not interpoint. int Line_Circle_Int (double x0,double y0,double Dx,double Dy,double x1,double y1,double r,double t[2]) { double DDXY = Dx*Dx+Dy*Dy; double DXDY = Dx*(x0-x1)+Dy*(y0-y1); double delta = DXDY*DXDY-DDXY*((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1)-r*r); if( delta < 0.0 ) return 0; delta = sqrt( delta ); DDXY = 1.0/DDXY; t[0] = (-DXDY+delta)*DDXY; t[1] = (-DXDY-delta)*DDXY; return 1; } // the function Circle_Circle_Int(...) calculate the intersection points of // the circle and the circle describled below: // the one circle equation is (x-x1)**2+(y-y1)**2 = r1**2; // the another circle equation is (x-x2)**2+(y-y2)**2 = r2**2; // the output is the x[2] and y[2]; // return 0 implied there is not interpoint, // return 1 implied there are two interpoint, // return 2 implied two circle are tangent. int Circle_Circle_Int (double x1,double y1,double r1,double x2,double y2,double r2,double x[2],double y[2]) { int ret=1; double delt; double t1,t2,t3; double a=x2-x1; double b=y2-y1; double c=r1*r1-r2*r2+x2*x2-x1*x1+y2*y2-y1*y1; if(fabs(a)>fabs(b)) { t1=1.0+(b*b)/(a*a); t2=(c/a-x1)*b/a-y1; t3=(c/a-x1)*(c/a-x1)+y1*y1-r1*r1; delt=t2*t2-t1*t3; if(fabs(delt)