POJ1269 简单的计算几何判断直线相交

Intersecting Lines
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8898 Accepted: 4003

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






题目大意:每组数据给四个点,每两个点构成一个直线,判断直线关系,共线,平行还是有交点,分别输出LINE,NONE和交点坐标。


思路:非常基础的计算几何了,主要是用来熟悉模板的,利用cross判断是否共线需要分别判断p1,p2,p3和p1,p2,p4的值是否都为零(为0就是3点共线,这样都为零即四点共线)。平行的情况可以利用类似斜率的方法,用乘法判断相等比较方便。求交点的函数是isSS(),输出即可。模板是WJMZBMR大神的……不过好像开始的isSS()会出现小数点后负数精度问题我就改了下(应该是我没理解大神的)



贴上AC代码

#include <cstdio>
#include<iomanip>
#include<vector>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <climits>
#include <numeric>
#include<cmath>
#define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
#define REP(i,n) for(int i=0;i<n;++i)
using namespace std;

const double EPS = 1e-8;
inline int sign(double a)
{
    return a < -EPS ? -1 : a > EPS;
}

struct Point
{
    double x, y;
    Point()
    {
    }
    Point(double _x, double _y) :
        x(_x), y(_y)
    {
    }
    Point operator+(const Point&p) const
    {
        return Point(x + p.x, y + p.y);
    }
    Point operator-(const Point&p) const
    {
        return Point(x - p.x, y - p.y);
    }
    Point operator*(double d) const
    {
        return Point(x * d, y * d);
    }
    Point operator/(double d) const
    {
        return Point(x / d, y / d);
    }
    bool operator<(const Point&p) const
    {
        int c = sign(x - p.x);
        if (c)
            return c == -1;
        return sign(y - p.y) == -1;
    }
    double dot(const Point&p) const
    {
        return x * p.x + y * p.y;
    }
    double det(const Point&p) const
    {
        return x * p.y - y * p.x;
    }
    double alpha() const
    {
        return atan2(y, x);
    }
    double distTo(const Point&p) const
    {
        double dx = x - p.x, dy = y - p.y;
        return hypot(dx, dy);
    }
    double alphaTo(const Point&p) const
    {
        double dx = x - p.x, dy = y - p.y;
        return atan2(dy, dx);
    }
    void read()
    {
        scanf("%lf%lf", &x, &y);
    }
    double abs()
    {
        return hypot(x, y);
    }
    double abs2()
    {
        return x * x + y * y;
    }
    void write()
    {
        cout << "(" << x << "," << y << ")" << endl;
    }
};

#define cross(p1,p2,p3) ((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y))

#define crossOp(p1,p2,p3) sign(cross(p1,p2,p3))

Point isSS(Point p1, Point p2, Point q1, Point q2)       //可求p1,p2 直线与q1,q2的焦点。。不过不确定
{
    double a1 = cross(q1,q2,p1), a2 = -cross(q1,q2,p2);
    Point temp;
    temp.x=sign((p1.x*a2+p2.x*a1)/(a1+a2))==0?0:(p1.x*a2+p2.x*a1)/(a1+a2);
     temp.y=sign((p1.y*a2+p2.y*a1)/(a1+a2))==0?0:(p1.y*a2+p2.y*a1)/(a1+a2);
    return temp;
}

int main()
{
    Point p1,p2,p3,p4;
    int n,i;
    scanf("%d",&n);
    printf("INTERSECTING LINES OUTPUT\n");
    for(i=1;i<=n;i++)
    {
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y,&p3.x,&p3.y,&p4.x,&p4.y);
        if(!sign(cross(p1,p2,p3))&&!sign(cross(p1,p2,p4)))
        {
            cout<<"LINE"<<endl;
        }
        else
        {
            if((p2.x-p1.x)*(p4.y-p3.y)==(p4.x-p3.x)*(p2.y-p1.y))
            {
              cout<<"NONE"<<endl;
            }
            else
            {
                Point temp=isSS(p1,p2,p3,p4);
                cout<<fixed<<setprecision(2)<<"POINT"<<" "<<temp.x*1.0<<" "<<temp.y*1.0<<endl;
            }
        }
    }
    printf("END OF OUTPUT\n");
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值