POJ 1269 计算几何 求直线交点

题意:

此题给出N组直线,每组2条直线。

如果共线则输出LINE,相交则输入点坐标,否则输出NONE(平行)。

题解:

点积判断平行和相交,有向面积(分点公式)求交点(不知道写的对不对,至少能过这个题,貌似数据很弱的样子)。

View Code
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 #include <cmath>
 7 
 8 #define EPS 1e-7
 9 
10 using namespace std;
11 
12 struct PO
13 {
14     double x,y;
15 }li[3][2];
16 
17 inline double cross(const PO &o,const PO &a,const PO &b)
18 {
19     return (a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
20 }
21 
22 inline void read()
23 {
24     for(int i=1;i<=2;i++)
25         scanf("%lf%lf%lf%lf",&li[i][0].x,&li[i][0].y,&li[i][1].x,&li[i][1].y);
26 }
27 
28 inline bool judge_parallel(PO s[],PO t[])
29 {
30     return fabs((s[1].y-s[0].y)*(t[1].x-t[0].x)-(s[1].x-s[0].x)*(t[1].y-t[0].y))<EPS;
31 }
32 
33 inline bool judge_same(PO s[],PO t[])
34 {
35     return fabs((s[1].y-s[0].y)*(t[1].x-s[0].x)-(s[1].x-s[0].x)*(t[1].y-s[0].y))<EPS;
36 }
37 
38 inline PO get_intersection(PO s[],PO t[])
39 {
40     PO ans;
41     double k2=1.0;
42     double k1=cross(s[0],t[1],t[0])/cross(s[1],t[0],t[1]);
43     ans.x=(k2*s[0].x+k1*s[1].x)/(k1+k2);
44     ans.y=(k2*s[0].y+k1*s[1].y)/(k1+k2);
45     return ans;
46 }
47 
48 inline void go()
49 {
50     if(judge_parallel(li[1],li[2]))
51     {
52         if(judge_same(li[1],li[2])) puts("LINE");
53         else puts("NONE");
54         return;
55     }
56     PO ans=get_intersection(li[1],li[2]);
57     printf("POINT %.2lf %.2lf\n",ans.x,ans.y);
58 }
59 
60 int main()
61 {
62     int cas; scanf("%d",&cas);
63     puts("INTERSECTING LINES OUTPUT");
64     while(cas--) read(),go();
65     puts("END OF OUTPUT");
66     return 0;
67 }
原文地址:https://www.cnblogs.com/proverbs/p/2855417.html