POJ 1269 Intersecting Lines 简单计算几何

来源:互联网 发布:利达编程软件 编辑:程序博客网 时间:2024/05/16 17:39

题目的链接如下:http://poj.org/problem?id=1269
Intersecting Lines
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 14498 Accepted: 6399

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
Mid-Atlantic 1996

题目的大意如下:给你四个点,然后前两个点组成一条直线,然后求两个直线的关系,相交平行还是重合。
具体代码如下:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#define eps 1e-8//一般式方程判断两条直线是否重合的充要条件A2B1-A1B2==0 && A1C2-C1A2==0 && B1C2-C1B2==0//判断是否平行的条件A2B1-A1B2==0struct TPoint{    double x,y;     TPoint operator-(TPoint&a){         TPoint p1;        p1.x=x-a.x;        p1.y=y-a.y;        return p1;    }};struct TLine{    double a,b,c;    TLine(double a = 0, double b = 0, double c = 0) :a(a), b(b), c(c){ }};//求p1关于p2的对称点TPoint symmetricalPoint(TPoint p1,TPoint p2){    TPoint p3;    p3.x=2*p2.x-p1.x;    p3.y=2*p2.y-p1.y;    return p3;} //p点关于直线L的对称点TPoint symmetricalPointofLine(TPoint p,TLine L){    TPoint p2;    double d;    d=L.a*L.a+L.b*L.b;    p2.x=(L.b*L.b*p.x-L.a*L.a*p.x-2*L.a*L.b*p.y-2*L.a*L.c)/d;    p2.y=(L.a*L.a*p.y-L.b*L.b*p.y-2*L.a*L.b*p.x-2*L.b*L.c)/d;        return p2;}//求线段所在直线,返回直线方程的三个系数//两点式化为一般式TLine lineFromSegment(TPoint p1,TPoint p2){    TLine tmp;    tmp.a=p2.y-p1.y;    tmp.b=p1.x-p2.x;    tmp.c=p2.x*p1.y-p1.x*p2.y;    return tmp;}TPoint LineInter(TLine l1,TLine l2){    using namespace std;    //求两直线得交点坐标    TPoint tmp;    double a1=l1.a;    double b1=l1.b;    double c1=l1.c;    double a2=l2.a;    double b2=l2.b;    double c2=l2.c;    //注意这里b1=0    if(fabs(b1)<eps){        tmp.x=-c1/a1;        tmp.y=(-c2-a2*tmp.x)/b2;    }    else{        tmp.x=(c1*b2-b1*c2)/(b1*a2-b2*a1);        tmp.y=(-c1-a1*tmp.x)/b1;    }    return tmp;}using namespace std;int main(){    int t;    double k1, k2;    double ansx, ansy;    double x1, x2, x3, y1, y2, y3;    TPoint a, b, c, d;    TPoint ans ;    TLine l1, l2;    scanf("%d", &t);    cout << "INTERSECTING LINES OUTPUT" << endl;    while(t--){        scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&d.x,&d.y);//输入四个点;        l1 = lineFromSegment(a,b);//前两个点确定的直线        l2 = lineFromSegment(c,d);//后两个点确定的直线        if(fabs(l2.a*l1.b - l1.a*l2.b) <= eps){//先判断是否平行,在判断是否重合            if(fabs(l1.a*l2.c - l2.a*l1.c) <= eps && fabs(l1.b*l2.c - l2.b*l1.c) <=eps) cout << "LINE" << endl;            else cout << "NONE" << endl;        }        else {            ans = LineInter(l1,l2);//求两线相交的点            printf("POINT %.2f %.2f\n", ans.x, ans.y);        }    }    cout << "END OF OUTPUT" << endl;    return 0;}
0 0