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;
    }
    
  • 相关阅读:
    第三百节,python操作redis缓存-其他常用操作,用于操作redis里的数据name,不论什么数据类型
    第二百九十九节,python操作redis缓存-SortSet有序集合类型,可以理解为有序列表
    第二百九十八节,python操作redis缓存-Set集合类型,可以理解为不能有重复元素的列表
    第二百九十七节,python操作redis缓存-List类型,可以理解为列表
    第二百九十六节,python操作redis缓存-Hash哈希类型,可以理解为字典类型
    第二百九十五节,python操作redis缓存-字符串类型
    第二百九十四节,Redis缓存-Redis安装
    第二百九十三,Memcached缓存
    第二百九十二节,RabbitMQ多设备消息队列-Python开发
    数据库大小写与编程语言大小写的区别
  • 原文地址:https://www.cnblogs.com/xiong-/p/3421999.html
Copyright © 2011-2022 走看看