欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Triangles FZU - 2273

程序员文章站 2022-04-01 16:11:17
...

题目大意:

给你两个三角形,判断他们的关系:包含,相交,相离

代码:

#include <iostream>
#include <cmath>
#include <cstdio>
#define ABS_FLOAT_0  0.0001
using namespace std;

struct Point
{
    double x,y;
};

struct Triangle
{
    Point A,B,C;
};

//ab和ac两向量叉积
double mult(Point a,Point b,Point c)
{
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}

//aa, bb为一条线段两端点 cc, dd为另一条线段的两端点 相交返回true, 不相交返回false
bool intersect(Point aa, Point bb, Point cc, Point dd)
{
    if ( max(aa.x, bb.x)<min(cc.x, dd.x) )
        return false;
    if ( max(aa.y, bb.y)<min(cc.y, dd.y) )
        return false;
    if ( max(cc.x, dd.x)<min(aa.x, bb.x) )
        return false;
    if ( max(cc.y, dd.y)<min(aa.y, bb.y) )
        return false;
    if ( mult(cc, bb, aa)*mult(bb, dd, aa)<0 )
        return false;
    if ( mult(aa, dd, cc)*mult(dd, bb, cc)<0 )
        return false;
    return true;
}

//求三角形面积
double Get_Triangle_Squar(Point A,Point B,Point C)
{
    return fabs(mult(A,B,C))/2.0;
}

//判断点D是否在三角形ABC内
bool IsInTriangle(const Point A, const Point B, const Point C, const Point D)
{
    double SABC, SADB, SBDC, SADC;
    SABC = Get_Triangle_Squar(A, B, C);
    SADB = Get_Triangle_Squar(A, D, B);
    SBDC = Get_Triangle_Squar(B, D, C);
    SADC = Get_Triangle_Squar(A, D, C);
    double SumSuqar = SADB + SBDC + SADC;
    if ((-ABS_FLOAT_0 < (SABC - SumSuqar)) && ((SABC - SumSuqar) < ABS_FLOAT_0))
        return true;
    else
        return false;
}


int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        Triangle a,b;
        scanf("%lf%lf%lf%lf%lf%lf",&a.A.x,&a.A.y,&a.B.x,&a.B.y,&a.C.x,&a.C.y);
        scanf("%lf%lf%lf%lf%lf%lf",&b.A.x,&b.A.y,&b.B.x,&b.B.y,&b.C.x,&b.C.y);
        if(IsInTriangle(a.A,a.B,a.C,b.A)&&IsInTriangle(a.A,a.B,a.C,b.B)&&IsInTriangle(a.A,a.B,a.C,b.C))
        {
            printf("contain\n");
        }
        else if(IsInTriangle(b.A,b.B,b.C,a.A)&&IsInTriangle(b.A,b.B,b.C,a.B)&&IsInTriangle(b.A,b.B,b.C,a.C))
        {
            printf("contain\n");
        }
        else if(intersect(a.A,a.B,b.A,b.B)||intersect(a.A,a.B,b.A,b.C)||intersect(a.A,a.B,b.B,b.C))
        {
            printf("intersect\n");
        }
        else if(intersect(a.A,a.C,b.A,b.B)||intersect(a.A,a.C,b.A,b.C)||intersect(a.A,a.C,b.B,b.C))
        {
            printf("intersect\n");
        }
        else if(intersect(a.B,a.C,b.A,b.B)||intersect(a.B,a.C,b.A,b.C)||intersect(a.B,a.C,b.B,b.C))
        {
            printf("intersect\n");
        }
        else
        {
            printf("disjoint\n");
        }
    }
    return 0;
}