POJ1269(计算几何基础-直线与直线之间的关系与它们的交点)

来源:互联网 发布:网络对青少年的利与弊 编辑:程序博客网 时间:2024/05/18 02:21

Intersecting Lines
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16216 Accepted: 7001
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

题意:每次给你两条直线,问你这两条直线的关系是重合还是平行还是相交,如果是相交,则输出它们的交点。

解题思路:用向量的叉积来做就行,套一套模板。注意用g++交可能过不了,用c++交。

#include<stdio.h>#include<iostream>#include<cmath>#include<algorithm>#include<cstring>using namespace std;const double eps = 1e-9;struct Point{    double x, y;    Point(double _x = 0, double _y = 0){        x = _x;        y = _y;    }};typedef Point Vector;Vector operator +(Vector A, Vector B) {return Vector(A.x + B.x, A.y + B.y);}//向量加Vector operator -(Vector A, Vector B) {return Vector(A.x - B.x, A.y - B.y);}//向量减Vector operator *(Vector A, double p) {return Vector(p * A.x, p * A.y);}//向量的数乘Vector operator /(Vector A, double p) {return Vector(A.x / p, A.y / p);}//向量的数除Vector operator -(Vector A) {return Vector(-A.x, -A.y);}//向量的取反int dcmp(double x){    if(fabs(x) < eps) return 0;    else return x < 0 ? -1 : 1;}bool operator == (const Point& a, const Point& b) {    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;}double Cross(Vector A, Vector B)//求解向量叉积{    return A.x * B.y - A.y * B.x;}double Dot(Vector A, Vector B)//求解向量数量积{    return A.x * B.x + A.y * B.y;}double xmulti(Point p, Point a, Point b)//求向量pa叉乘pb{    return (a.x - p.x) * (b.y - p.y) - (a.y - p.y) * (b.x - p.x);}struct Line{    Point p1;    Point p2;    Line(Point _p1, Point _p2){        p1 = _p1;        p2 = _p2;    }    Line(){        p1 = Point(0, 0);        p2 = Point(0, 0);    }};typedef Line seg;int line_loc(Line l1, Line l2)//判断直线的位置关系,1为平行,0为重合,-1为相交{    Point p1 = l1.p1;    Point p2 = l1.p2;    Point p3 = l2.p1;    Point p4 = l2.p2;    Vector v1 = p2 - p1;    Vector v2 = p4 - p3;    Vector v3 = p3 - p1;    double d1 = Cross(v1, v2);    double d2 = Cross(v1, v3);    if(dcmp(d1) == 0 && dcmp(d2) == 0) return 0;//重合    else if(dcmp(d1) == 0) return 1;    else return -1;}Point seg_intersecting(Line l1, Line l2)//求两相交的直线(线段也一样)的交点{    Point A = l1.p1;    Point B = l1.p2;    Point C = l2.p1;    Point D = l2.p2;    double _x, _y;    _x = (xmulti(A, B, D) * C.x - xmulti(A, B, C) * D.x) / (xmulti(A, B, D) - xmulti(A, B, C));    _y = (xmulti(A, B, D) * C.y - xmulti(A, B, C) * D.y) / (xmulti(A, B, D) - xmulti(A, B, C));    return Point(_x, _y);}int main(){    int T;    scanf("%d", &T);    Line L1, L2;    cout<<"INTERSECTING LINES OUTPUT"<<endl;    while(T--)    {        scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &L1.p1.x, &L1.p1.y, &L1.p2.x, &L1.p2.y, &L2.p1.x, &L2.p1.y, &L2.p2.x, &L2.p2.y);        int ans = line_loc(L1, L2);        if(ans == 0) cout<<"LINE"<<endl;        else if(ans == 1) cout<<"NONE"<<endl;        else        {            Point res = seg_intersecting(L1, L2);            cout<<"POINT";            printf(" %.2lf %.2lf\n", res.x, res.y);        }    }    cout<<"END OF OUTPUT"<<endl;    return 0;}