首页 > 代码库 > POJ 1269 Intersecting Lines(线段相交,水题)

POJ 1269 Intersecting Lines(线段相交,水题)

Intersecting Lines


大意:给你两条直线的坐标,判断两条直线是否共线、平行、相交,若相交,求出交点。

思路:线段相交判断、求交点的水题,没什么好说的。

struct Point{
    double x, y;
} ;
struct Line{
    Point a, b;
} A, B;

double xmult(Point p1, Point p2, Point p)
{
    return (p1.x-p.x)*(p2.y-p.y)-(p1.y-p.y)*(p2.x-p.x);
}

bool parallel(Line u, Line v)
{
    return zero((u.a.x-u.b.x)*(v.a.y-v.b.y)-(v.a.x-v.b.x)*(u.a.y-u.b.y));
}

Point intersection(Line u, Line v)
{
    Point ret = u.a;
    double t = ((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))/((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));
    ret.x += (u.b.x-u.a.x)*t, ret.y += (u.b.y-u.a.y)*t;
    return ret;
}

int T;

void Solve()
{
    scanf("%d", &T);
    printf("INTERSECTING LINES OUTPUT\n");
    while(T--)
    {
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &A.a.x, &A.a.y, &A.b.x, &A.b.y, &B.a.x, &B.a.y, &B.b.x, &B.b.y);
        if(parallel(A, B) && zero(xmult(A.a, B.a, B.b)))
        {
            printf("LINE\n");
        }
        else if(parallel(A, B))
        {
            printf("NONE\n");
        }
        else
        {
            Point t = intersection(A, B);
            printf("POINT %.2f %.2f\n", t.x, t.y);
        }
    }
    printf("END OF OUTPUT\n");
}