poj 1269 Intersecting Lines

来源:互联网 发布:淘宝 流量被限制 编辑:程序博客网 时间:2024/04/30 13:24


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

50 0 4 4 0 4 4 05 0 7 6 1 0 2 35 0 7 6 3 -6 4 -32 0 2 27 1 5 18 50 3 4 0 1 2 2 5

Sample Output

INTERSECTING LINES OUTPUTPOINT 2.00 2.00NONELINEPOINT 2.00 5.00POINT 1.07 2.20END OF OUTPUT



题意:就是求两条直线的交点,如果平行就输出:NONE,重合就输出:LINE否则就输出交点;

注意题目是求直线,当时在线段上WA了几次。

#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <vector>#include <queue>#include <cstdio>#include <cmath>#include <string>#include <stack>using namespace std;const  double  eps=1e-10;struct  point {    double  x, y;};double F(double x,double y) {    if(x-y>=0)        return x-y;    return y-x;}double Ka(point a,point b) {    return (a.y-b.y)/(a.x-b.x);}point fun(point a,point b,point c,point d) {    double k1=Ka(a,b);    double k2=Ka(c,d);    point ans;    ans.x=(k1*a.x-k2*c.x-a.y+c.y)/(k1-k2);    ans.y=k1*(ans.x-a.x)+a.y;    return ans;}int JudgeSide(point a,point b,double x,double y) {    double sum1,sum2;    sum1=(y-a.y)*(x-b.x);    sum2=(y-b.y)*(x-a.x);    return((sum1<sum2)?-1:((sum1>sum2)?1:0));}int main() {    int t;    int x1,x2,x3,x4,y1,y2,y3,y4;    point a,b,c,d;    scanf("%d",&t);    cout<<"INTERSECTING LINES OUTPUT"<<endl;    while(t--) {        cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y>>d.x>>d.y;        if(JudgeSide(a,b,c.x,c.y)==0&&JudgeSide(a,b,d.x,d.y)==0) {            cout<<"LINE"<<endl;            continue;        }        if(JudgeSide(c,d,a.x,a.y)==0&&JudgeSide(c,d,b.x,b.y)==0) {            cout<<"LINE"<<endl;            continue;        }        double k1=Ka(a,b);        double k2=Ka(c,d);        if(a.x==b.x&&c.x==d.x) {            cout<<"NONE"<<endl;        } else if(F(k1,k2)<=eps)            cout<<"NONE"<<endl;        else {            point ans;            if(a.x==b.x) {                ans.x=a.x;                ans.y=k2*(ans.x-c.x)+c.y;            } else if(c.x==d.x) {                ans.x=c.x;                ans.y=k1*(ans.x-a.x)+a.y;            } else                ans=fun(a,b,c,d);            printf("POINT %.2f %.2f\n",ans.x,ans.y);        }    }    cout<<"END OF OUTPUT"<<endl;    return 0;}