POJ1269 Intersecting Lines(直线位置关系&求直线交点)

题目链接:

  http://poj.org/problem?id=1269

题目描述:

Intersecting Lines
 

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

题目大意:

  给两条直线,判断是否重合/相交于一点/平行,若相交于一点,求坐标

思路:

  直线参数方程式,方向向量叉乘为0则重合或平行

  否则用点坐标之间的关系求解出交点

  设直线为 A1+t*v 和 B1+q*u

  交点为:

    A1+t0 * v

    其中 t0 = 

      ( (A1.y - B1.y)*(B2.x - B1.x) - (A1.x - B1.x)*(B2.y - B1.y) )  /
      ( (A2.x - A1.x)*(B2.y - B1.y) - (A2.y - A1.y)*(B2.x - B1.x) )

 

 

代码:

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 using namespace std;
 7 
 8 const double EPS = 1e-10;    //精度系数
 9 
10 struct Point {
11     double x, y;
12     Point(double x = 0, double y = 0) :x(x), y(y) {}
13 };    //点的定义
14 
15 typedef Point Vector;    //向量的定义
16 
17 Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }    //向量加法
18 Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }    //向量减法
19 Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }        //向量数乘
20 
21 int dcmp(double x) {
22     if (fabs(x) < EPS)return 0; else return x < 0 ? -1 : 1;
23 }    //与0的关系
24 
25 bool operator == (const Point& a, const Point& b) {
26     return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
27 }    //点相等
28 
29 double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }    //向量叉乘
30 
31 int main() {
32     int n;
33     Point A1, A2, B1, B2;
34     cin >> n;
35     printf("INTERSECTING LINES OUTPUT\n");
36     while (n--) {
37         scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &A1.x, &A1.y, &A2.x, &A2.y, &B1.x, &B1.y, &B2.x, &B2.y);
38         Vector v = A2 - A1, u = B2 - B1;
39         if (dcmp(Cross(v, u)) == 0) {
40             Vector tmp = B1 - A1;
41             if (A1 == B1 || dcmp(Cross(tmp, u)) == 0)printf("LINE\n");    //重合
42             else printf("NONE\n");    //平行
43         } else {
44             printf("POINT ");
45             double t = ((A1.y - B1.y)*(B2.x - B1.x) - (A1.x - B1.x)*(B2.y - B1.y)) /
46                 ((A2.x - A1.x)*(B2.y - B1.y) - (A2.y - A1.y)*(B2.x - B1.x));    //交点坐标
47             Point ans = v*t + A1;
48             printf("%.2lf %.2lf\n", ans.x, ans.y);
49         }
50     }
51     printf("END OF OUTPUT\n");
52 }

 

posted @ 2017-06-12 20:14  hyp1231  阅读(212)  评论(0编辑  收藏  举报