【计算几何】POJ 1269

题目大意:
给定n对直线,求判断直线间的关系:平行、重合、相交,如果相交则输出两条直线的交点

计算几何模板题,只要注意精度,判断平行和重合是不要用==0,要写一个dcmp,当绝对值小于等于1e-6时就可以判断是0了。

附上AC代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const double eps = 1e-6;

class Point
{
    public:
        double x, y;
        Point(double x, double y):x(x), y(y) { }
};

typedef Point Vector;

Vector operator + (const Vector &a, const Vector &b)
{
    return Vector(a.x+b.x, a.y+b.y);
}

Vector operator - (const Point &a, const Point &b)
{
    return Vector(a.x-b.x, a.y-b.y);
}

Vector operator * (const Vector &a, const double &b)
{
    return Vector(a.x*b, a.y*b);
}

Vector operator / (const Vector &a, const double &b)
{
    return Vector(a.x/b, a.y/b);
}

double Cross(const Vector &a, const Vector &b)
{
    return a.x*b.y - a.y*b.x;
}

double Dot(const Vector &a, const Vector &b)
{
    return a.x*b.x + a.y*b.y;
}

double Length(const Vector &a)
{
    return sqrt(Dot(a, a));
}

int dcmp(double x)
{
    if (fabs(x) <= eps)
        return 0;
    return x > 0 ? 1 : -1;
}

Point LineIntersection(Point p, Vector v, Point q, Vector w)
{
    Vector u = p-q;
    double t = Cross(w, u) / Cross(v, w);
    return p+v*t;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("inter.in", "r", stdin);
#endif
    int t;
    double a, b, c, d, e, f, g, h;
    scanf("%d", &t);
    printf("INTERSECTING LINES OUTPUT\n");
    while (t--) {
        scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &a, &b, &c, &d, &e, &f, &g, &h);
        Point p1(a, b), p2(c, d), p3(e, f), p4(g, h);
        Vector v1 = p2-p1, v2 = p4-p3;
        if (dcmp(Cross(v1, p3-p1)) == 0 && dcmp(Cross(p3-p1, p4-p1)) == 0) {
            printf("LINE\n");
            continue;
        }
        double tmp = Cross(v1, v2);
        if (dcmp(tmp) == 0) {
            printf("NONE\n");
            continue;
        }
        Point ins = LineIntersection(p1, v1, p3, v2);
        printf("POINT %.2f %.2f\n", ins.x, ins.y);
    }
    printf("END OF OUTPUT");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值