zoukankan      html  css  js  c++  java
  • POJ1269 Intersecting Lines[线段相交 交点]

    Intersecting Lines
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 15145   Accepted: 6640

    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


    呵呵 这种裸题...
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    using namespace std;
    typedef long long ll;
    const double eps=1e-8;
    inline int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
        return x*f;
    }
    inline int sgn(double x){
        if(abs(x)<eps) return 0;
        else return x<0?-1:1;
    }
    struct Vector{
        double x,y;
        Vector(double a=0,double b=0):x(a),y(b){}
        bool operator <(const Vector &a)const{
            return x<a.x||(x==a.x&&y<a.y);
        }
        void print(){
            printf("%lf %lf
    ",x,y);
        }
    };
    typedef Vector Point;
    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 b){return Vector(a.x*b,a.y*b);}
    Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);}
    bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==0&&sgn(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 DisPP(Point a,Point b){
        Point t=a-b;
        return sqrt(t.x*t.x+t.y*t.y);
    }
    struct Line{
        Point s,t;
        Line(){}
        Line(Point p,Point v):s(p),t(v){}
    };
    bool isLSI(Line l1,Line l2){
        Vector v=l1.t-l1.s,u=l2.s-l1.s,w=l2.t-l1.s;
        return sgn(Cross(v,u))!=sgn(Cross(v,w));
    }
    bool isSSI(Line l1,Line l2){
        return isLSI(l1,l2)&&isLSI(l2,l1);
    }
    Point LI(Line a,Line b){
        Vector v=a.s-b.s,v1=a.t-a.s,v2=b.t-b.s;
        double t=Cross(v2,v)/Cross(v1,v2);
        return a.s+v1*t;
    }
    double x,y,x2,y2;
    Line l1,l2;
    void solve(){
        if(sgn(Cross(l1.t-l1.s,l2.t-l2.s))==0){
            Vector v=l1.t-l1.s,u=l2.s-l1.s,w=l2.t-l1.s;
            if(sgn(Cross(v,u))==0&&sgn(Cross(v,w))==0) puts("LINE");
            else puts("NONE");
        }else{
            Point p=LI(l1,l2);
            printf("POINT %.2f %.2f
    ",p.x,p.y);
        }
    }
    int main(int argc, const char * argv[]) {
        int T=read();
        puts("INTERSECTING LINES OUTPUT");
        while(T--){
            scanf("%lf%lf%lf%lf",&x,&y,&x2,&y2);
            l1=Line(Point(x,y),Point(x2,y2));
            scanf("%lf%lf%lf%lf",&x,&y,&x2,&y2);
            l2=Line(Point(x,y),Point(x2,y2));
            solve();
        }
        puts("END OF OUTPUT");
        return 0;
    }
     
  • 相关阅读:
    mysql 组合聚集函数
    mysql distinct()函数 去重
    mysql sum()函数 , 计算总和
    mysql max()函数,min()函数,获取最大值以及最小值
    mysql count() 函数,对结果统计计数
    三个实例演示 Java Thread Dump 日志分析
    借助LVS+Keepalived实现负载均衡
    配置Tomcat成为系统服务
    快速理解Docker
    tomcat 启动时设置 java 参数,mark
  • 原文地址:https://www.cnblogs.com/candy99/p/6354159.html
Copyright © 2011-2022 走看看