zoukankan      html  css  js  c++  java
  • poj 1269 直线间的关系

    Intersecting Lines
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 9360   Accepted: 4210

    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



    #include <iostream>
    #include <cmath>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    
    struct Point{
        double x,y;
        Point(){}
        Point(double x,double y):x(x),y(y){}
    };
    struct Line{
        Point a,b;
    };
    
    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(A.x*p,A.y*p);}
    Vector operator /(Vector A,double p){return Vector(A.x/p,A.y/p);}
    bool operator < (const Point &a,const Point &b)
    {
        return a.x<b.x||(a.x==b.x&&a.y<b.y);
    }
    const double eps=1e-10;
    
    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 Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;}//点积
    double Length(Vector A){return sqrt(Dot(A,A));}//向量长度
    //两向量的夹角
    double Angle(Vector A,Vector B){return acos(Dot(A,B)/Length(A)/Length(B));}
    
    double Cross(Vector A,Vector B){ return A.x*B.y-A.y*B.x;}//叉积
    
    Point GetLineIntersection(Point p,Vector v,Point q,Vector w)
    {
        Vector u=p-q;
        double t=Cross(w,u)/Cross(v,w);
        return p+v*t;
    }
    double DistanceToLine(Point P,Point A,Point B)
    {
        Vector v1=B-A,v2=P-A;
        return fabs(Cross(v1,v2)) / Length(v1);
    }
    void judge(Line a,Line b)
    {
        Point p;
        if(dcmp(Cross(a.a-a.b,b.a-b.b)) == 0)
        {
            if(dcmp(DistanceToLine(b.a,a.a,a.b)) == 0)
            {
                printf("LINE
    ");return ;
            }
            else
            {
                printf("NONE
    ");return ;
            }
        }
        else
        {
            p=GetLineIntersection(a.a,a.a-a.b,b.a,b.a-b.b);
            printf("POINT %.2lf %.2lf
    ",p.x,p.y);
            return ;
        }
    }
    int main()
    {
        int n,i;
        Line L1,L2;
        while(~scanf("%d",&n))
        {
            printf("INTERSECTING LINES OUTPUT
    ");
            for(i=0;i < n;i++)
            {
                scanf("%lf %lf %lf %lf",&L1.a.x,&L1.a.y,&L1.b.x,&L1.b.y);
                scanf("%lf %lf %lf %lf",&L2.a.x,&L2.a.y,&L2.b.x,&L2.b.y);
                judge(L1,L2);
            }
            printf("END OF OUTPUT
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    OpenSSL使用1(用OpenSSL生成自签名证书在IIS上搭建Https站点)(用于iOS的https访问)
    dotnet调用node.js写的socket服务(websocket/socket/socket.io)
    Jenkins中deploy插件的deploy war/ear to a container与deploy artifacts to maven reepository区别
    Mac下启动MySQL出现错误“the /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql' user”解决
    Mac下配置node.js环境(Mac 10.12)
    brew udpate出现错误“/usr/local is not writable.”的问题解决
    Linux快速查看某条命令的版本和存放的位置(ls -l `which mvn`)
    Mac下node.js卸载方法收集
    Mac下安装包管理平台Homebrew(Mac 10.12)
    Java出现“Error configuring application listener of class...”类似的错误解决
  • 原文地址:https://www.cnblogs.com/xiong-/p/3421999.html
Copyright © 2011-2022 走看看