Intersecting Lines POJ - 1269(共线、平行、相交)

#include
#include
#include
#define EPS 1e-9
using namespace std;
struct Point
{
    double x,y;
    Point(double _x=0,double _y=0):x(_x),y(_y){}
};
struct Line
{
    Point p1,p2;
    Line(double x1=0,double y1=0,double x2=0,double y2=0):p1(x1,y1),p2(x2,y2){}
};
Point getpoint(Line l1,Line l2)
{
    double A1=l1.p1.y-l1.p2.y,B1=l1.p2.x-l1.p1.x,C1=l1.p1.x*l1.p2.y-l1.p2.x*l1.p1.y;
    double A2=l2.p1.y-l2.p2.y,B2=l2.p2.x-l2.p1.x,C2=l2.p1.x*l2.p2.y-l2.p2.x*l2.p1.y;
    double D=A1*B2-A2*B1;
    return Point((B1*C2-B2*C1)/D,(A2*C1-A1*C2)/D);
}
double getval(Point p,Line l)
{
    return fabs((l.p1.x-p.x)*(l.p2.y-p.y)-(l.p2.x-p.x)*(l.p1.y-p.y));
}
int main()
{
    int n;
    scanf("%d",&n);
    printf("INTERSECTING LINES OUTPUT\n");
    Line l1,l2;
    while(n--)
    {
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&l1.p1.x,&l1.p1.y,&l1.p2.x,&l1.p2.y,&l2.p1.x,&l2.p1.y,&l2.p2.x,&l2.p2.y);
        if(getval(l2.p1,l1)<EPS&&getval(l2.p2,l1)<EPS)
        {//叉积是否共线
            printf("LINE\n");
            continue;
        }
        else if(fabs((l1.p2.y-l1.p1.y)*(l2.p2.x-l2.p1.x)-(l2.p2.y-l2.p1.y)*(l1.p2.x-l1.p1.x))<EPS)
        {//斜率是否平行
            printf("NONE\n");
            continue;
        }
        Point p=getpoint(l1,l2);//那么,一定有交点
        printf("POINT %.2lf %.2lf\n",p.x,p.y);
    }
    printf("END OF OUTPUT\n");
    return 0;
}

你可能感兴趣的:(几何,算法,jihe)