[POJ1269]Intersecting Lines(计算几何)

版权声明:转载请注明出处:http://blog.csdn.net/clove_unique https://blog.csdn.net/Clove_unique/article/details/53967984

题目描述

传送门
题意:每次给出两条直线,判断是否平行、重合、相交,相交的话就交点。

题解

判断两条直线是否平行
两条直线各任选两个点组成两个向量平行(叉积为0)

判断两条直线是否重合
在平行的基础上,在两条直线上各选一个点组成一个向量在去与前两个判平行(叉积为0)

求交点的话用直线的分点(比值)+叉积面积法求解
注意比值不能加fabs

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;

const double eps=1e-9;
int dcmp(double x)
{
    if (x<=eps&&x>=-eps) return 0;
    return (x>0)?1:-1;
}
struct Vector
{
    double x,y;
    Vector(double X=0,double Y=0)
    {
        x=X,y=Y;
    }
};
typedef Vector Point;
struct Line
{
    Point p;
    Vector v;
    Line(Point P=Point(0,0),Vector V=Vector(0,0))
    {
        p=P,v=V;
    }
};
Vector operator + (Vector A,Vector B) {return Vector(A.x+B.x,A.y+B.y);}
Vector operator * (Vector A,double a) {return Vector(A.x*a,A.y*a);}

int T;
double a,b,c,d;

double Cross(Vector A,Vector B)
{
    return A.x*B.y-A.y*B.x;
}
Point GIL(Line A,Line B)
{
    Vector v=A.v,w=B.v,u=Vector(A.p.x-B.p.x,A.p.y-B.p.y);
    double t=Cross(w,u)/Cross(v,w);
    return A.p+A.v*t;
}
int main()
{
    puts("INTERSECTING LINES OUTPUT"); 
    scanf("%d",&T);
    while (T--)
    {
        scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
        Vector v1=Vector(c-a,d-b);
        Line A=Line(Point(a,b),v1);
        scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
        Vector v2=Vector(c-a,d-b);
        Line B=Line(Point(a,b),v2);
        Vector v3=Vector(A.p.x-B.p.x,A.p.y-B.p.y);
        double x=Cross(v1,v2);
        double y=Cross(v1,v3);
        if (!dcmp(x)&&!dcmp(y)) puts("LINE");
        else if (!dcmp(x)) puts("NONE");
        else
        {
            Point ans=GIL(A,B);
            printf("POINT %.2lf %.2lf\n",ans.x,ans.y);
        }
    }
    puts("END OF OUTPUT");
}
阅读更多
想对作者说点什么? 我来说一句
相关热词

没有更多推荐了,返回首页