水题。判断两条直线位置关系。
考虑平行的情况,那么 四边形的面积会相等,重合的话,四边形的面积相等且为0.
除去这两种就一定有交点。
1 #include <cstdio> 2 #include <cmath> 3 #define db double 4 using namespace std; 5 const db eps=1e-6; 6 const db pi = acos(-1); 7 int sign(db k){ 8 if (k>eps) return 1; else if (k<-eps) return -1; return 0; 9 } 10 int cmp(db k1,db k2){return sign(k1-k2);} 11 struct point{ 12 db x,y; 13 point operator + (const point &k1) const{return (point){k1.x+x,k1.y+y};} 14 point operator - (const point &k1) const{return (point){x-k1.x,y-k1.y};} 15 point operator * (db k1) const{return (point){x*k1,y*k1};} 16 point operator / (db k1) const{return (point){x/k1,y/k1};} 17 }; 18 db dot(point k1,point k2){ 19 return k1.x*k2.x+k1.y*k2.y; 20 } 21 db cross(point k1,point k2){ 22 return k1.x*k2.y-k1.y*k2.x; 23 } 24 int inmid(db k1,db k2,db k3){return sign(k1-k3)*sign(k2-k3)<=0;} 25 int inmid(point k1,point k2,point k3){//k3在[k1,k2] 26 return inmid(k1.x,k2.x,k3.x)&&inmid(k1.y,k2.y,k3.y); 27 } 28 point getLL (point k1,point k2,point k3,point k4){//两直线交点 29 db w1=cross(k1-k3,k4-k3),w2=cross(k4-k3,k2-k3); 30 return (k1*w2+k2*w1)/(w1+w2); 31 } 32 bool onS(point k1,point k2,point q){//q在[k1,k2] 33 return inmid(k1,k2,q)&&sign(cross(k1-q,k2-k1))==0; 34 } 35 int checkLL(point k1,point k2,point k3,point k4){//求两条直线是否 (平行||重合) 36 return cmp(cross(k3-k1,k4-k1),cross(k3-k2,k4-k2))==0; 37 } 38 struct Line{ 39 point p[2]; 40 }; 41 42 db xl,yl,x2,y2; 43 int n; 44 point p[5]; 45 int main(){ 46 scanf("%d",&n); 47 printf("INTERSECTING LINES OUTPUT\n"); 48 while (n--){ 49 for(int i=1;i<=4;i++){ 50 scanf("%lf%lf",&p[i].x,&p[i].y); 51 } 52 if(checkLL(p[1],p[2],p[3],p[4])){ 53 if(sign(cross(p[1]-p[3],p[2]-p[3]))==0){ 54 printf("LINE\n"); 55 } else{ 56 printf("NONE\n"); 57 } 58 } else{ 59 point tmp = getLL(p[1],p[2],p[3],p[4]); 60 printf("POINT %.2f %.2f\n",tmp.x,tmp.y); 61 } 62 } 63 printf("END OF OUTPUT\n"); 64 }View Code