Fellow me on GitHub

POJ1269(KB13-D 计算几何)

Intersecting Lines

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16681   Accepted: 7192

Description

We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of one another (i.e. they are the same line), 3) intersect in a point. In this problem you will use your algebraic knowledge to create a program that determines how and where two lines intersect. 
Your program will repeatedly read in four points that define two lines in the x-y plane and determine how and where the lines intersect. All numbers required by this problem will be reasonable, say between -1000 and 1000. 

Input

The first line contains an integer N between 1 and 10 describing how many pairs of lines are represented. The next N lines will each contain eight integers. These integers represent the coordinates of four points on the plane in the order x1y1x2y2x3y3x4y4. Thus each of these input lines represents two lines on the plane: the line through (x1,y1) and (x2,y2) and the line through (x3,y3) and (x4,y4). The point (x1,y1) is always distinct from (x2,y2). Likewise with (x3,y3) and (x4,y4).

Output

There should be N+2 lines of output. The first line of output should read INTERSECTING LINES OUTPUT. There will then be one line of output for each pair of planar lines represented by a line of input, describing how the lines intersect: none, line, or point. If the intersection is a point then your program should output the x and y coordinates of the point, correct to two decimal places. The final line of output should read "END OF OUTPUT".

Sample Input

5
0 0 4 4 0 4 4 0
5 0 7 6 1 0 2 3
5 0 7 6 3 -6 4 -3
2 0 2 27 1 5 18 5
0 3 4 0 1 2 2 5

Sample Output

INTERSECTING LINES OUTPUT
POINT 2.00 2.00
NONE
LINE
POINT 2.00 5.00
POINT 1.07 2.20
END OF OUTPUT

Source

 
两直线求交点
 1 //2017-08-30
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <cmath>
 7 #include <iomanip>
 8 
 9 using namespace std;
10 
11 const int N = 110;
12 const double EPS = 1e-8;
13 
14 //三态函数
15 int sgn(double x){
16     if(fabs(x) < EPS)return 0;
17     if(x < 0)return -1;
18     else return 1;
19 }
20 
21 struct Point{
22     double x, y;
23     Point(){}
24     Point(double _x, double _y):x(_x), y(_y){}
25     Point(const Point &p):x(p.x), y(p.y){}
26     //a-b为向量ba
27     Point operator- (const Point &b) const {
28         return Point(x-b.x, y-b.y);
29     }
30     //向量叉积
31     double operator^ (const Point &b) const {
32         return x*b.y - y*b.x;
33     }
34     //向量点积
35     double operator* (const Point &b) const {
36         return x*b.x + y*b.y;
37     }
38 };
39 
40 struct Line{
41     Point a, b;
42     Line(){}
43     Line(Point _a, Point _b):a(_a), b(_b){}
44     //判断两直线关系
45     //input:两直线
46     //output:pair<int, Point> first == 0 表示直线重合
47     //                         first == 1 表示两直线平行
48     //                         first == 2 表示两直线相交,second 为交点
49     pair<int, Point> operator & (const Line &line) const{
50         Point res = a;
51         if(sgn((a-b)^(line.a-line.b)) == 0){
52             if(sgn((a-line.b)^(line.a-line.b)) == 0)
53                   return make_pair(0, res);
54             else return make_pair(1, res);
55         }
56         double t = ((a-line.a)^(line.a-line.b))/((a-b)^(line.a-line.b));
57         res.x += (b.x-a.x)*t;
58         res.y += (b.y-a.y)*t;
59         return make_pair(2, res);
60     }
61 }seg[N];
62 
63 
64 int n;
65 int main()
66 {
67     std::ios::sync_with_stdio(false);
68     //freopen("inputD.txt", "r", stdin);
69     int T;
70     cin>>T;
71     cout<<"INTERSECTING LINES OUTPUT"<<endl;
72     while(T--){
73         Line l1, l2;
74         cin>>l1.a.x>>l1.a.y>>l1.b.x>>l1.b.y;
75         cin>>l2.a.x>>l2.a.y>>l2.b.x>>l2.b.y;
76         pair<int, Point> res = l1 & l2;
77         cout.setf(ios::fixed);
78         if(res.first == 0)
79               cout<<"LINE"<<endl;
80         else if(res.first == 1)
81               cout<<"NONE"<<endl;
82         else
83               cout<<setprecision(2)<<"POINT "<<res.second.x<<" "<<res.second.y<<endl;
84     }
85     cout<<"END OF OUTPUT"<<endl;
86 
87     return 0;
88 }

 

posted @ 2017-08-30 17:16  Penn000  阅读(160)  评论(0编辑  收藏  举报