POJ1269+直线相交

求相交点

 1 /*
 2 线段相交模板:判相交、求交点
 3 */
 4 #include<stdio.h>
 5 #include<string.h>
 6 #include<stdlib.h>
 7 #include<math.h>
 8 
 9 const double eps = 1e-8;
10 struct Point{
11     double x,y;
12 };
13 Point P_ans;
14 double cross( Point a,Point b,Point c ){
15     return ( b.x-a.x )*( c.y-a.y )-( b.y-a.y )*( c.x-a.x );
16 }
17 int solve( Point a,Point b,Point c,Point d ){
18     if( fabs(cross(a,b,c))<=eps&&fabs(cross(a,b,d))<=eps )
19         return -1;//两条线段在同一条直线上
20     if( fabs((b.x-a.x)*(d.y-c.y)-(b.y-a.y)*(d.x-c.x))<=eps )
21         return 0;//两条线断平行
22     /*
23     求交点用到叉积(必须保证有交点)
24     交点为p0(x,y)
25     (A-p0)*(B-p0)=0
26     (C-p0)*(D-p0)=0
27     */
28     double a1,a2,b1,b2,c1,c2;
29     a1 = a.y-b.y,b1 = b.x-a.x,c1 = a.x*b.y-b.x*a.y;
30     a2 = c.y-d.y,b2 = d.x-c.x,c2 = c.x*d.y-d.x*c.y;
31     P_ans.x = (b1*c2-b2*c1)/(a1*b2-a2*b1); 
32     P_ans.y = (a2*c1-a1*c2)/(a1*b2-a2*b1); 
33     return 1;
34 }
35 int main(){
36     int n;
37     Point p1,p2,p3,p4;
38     scanf("%d",&n);
39     printf("INTERSECTING LINES OUTPUT
");  
40     while( n-- ){
41         scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y,&p3.x,&p3.y,&p4.x,&p4.y);
42         int f = solve( p1,p2,p3,p4 );
43         if( f==-1 ) puts("LINE");  
44         else if( f==0 ) puts("NONE");  
45         else{
46             printf("POINT %.2lf %.2lf
",P_ans.x,P_ans.y);
47         }
48     }
49     printf("END OF OUTPUT
");  
50     return 0;
51 }
View Code
keep moving...
原文地址:https://www.cnblogs.com/xxx0624/p/3199618.html