直线相交 POJ 1269

 1 // 直线相交 POJ 1269
 2 
 3 // #include <bits/stdc++.h>
 4 #include <iostream>
 5 #include <cstdio>
 6 #include <cstdlib>
 7 #include <algorithm>
 8 #include <math.h>
 9 using namespace std;
10 #define LL long long
11 typedef pair<int,int> pii;
12 const double inf = 123456789012345.0;
13 const LL MOD =100000000LL;
14 const int N =1e4+10;
15 #define clc(a,b) memset(a,b,sizeof(a))
16 const double eps = 1e-8;
17 void fre() {freopen("in.txt","r",stdin);}
18 void freout() {freopen("out.txt","w",stdout);}
19 inline int read() {int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch=getchar();}while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}return x*f;}
20 
21 int sgn(double x){
22     if(fabs(x) < eps)return 0;
23     if(x < 0)return -1;
24     else return 1;
25 }
26 struct Point{
27     double x,y;
28     Point(){}
29     Point(double _x,double _y){
30         x = _x;y = _y;
31     }
32     Point operator -(const Point &b)const{
33         return Point(x - b.x,y - b.y);
34     }
35     double operator ^(const Point &b)const{
36         return x*b.y - y*b.x;
37     }
38     double operator *(const Point &b)const{
39         return x*b.x + y*b.y;
40     }
41 };
42 
43 struct Line{
44     Point s,e;
45     Line(){}
46     Line(Point _s,Point _e){
47         s=_s;e=_e;
48     }
49     pair<int,Point> operator & (const Line &b) const{
50         Point res=s;
51         if(sgn((s-e)^(b.s-b.e))==0){
52             if(sgn((s-b.e)^(b.s-b.e))==0) 
53                 return make_pair(0,res);
54             else  return make_pair(1,res);
55         }
56         double t=((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
57         res.x+=(e.x-s.x)*t;
58         res.y+=(e.y-s.y)*t;
59         return make_pair(2,res);
60     }
61 };
62 
63 int main(){
64     int T;
65     scanf("%d",&T);
66     double x1,x2,x3,x4,y1,y2,y3,y4;
67     printf("INTERSECTING LINES OUTPUT
");
68     while(T--){
69         scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
70         Line line1 = Line(Point(x1,y1),Point(x2,y2));
71         Line line2 = Line(Point(x3,y3),Point(x4,y4));
72         pair<int,Point> ans=line1 & line2;
73         if(ans.first==0) printf("LINE
");
74         else if(ans.first==1) printf("NONE
");
75         else printf("POINT %.2f %.2f
",ans.second.x,ans.second.y);
76     }
77     printf("END OF OUTPUT
");
78     return 0;
79 }
原文地址:https://www.cnblogs.com/ITUPC/p/5856218.html