Triangles

This is a simple problem. Given two triangles A and B, you should determine they are intersect, contain or disjoint. (Public edge or point are treated as intersect.)

Input

First line contains an integer T (1 ≤ T ≤ 10), represents there are T test cases.

For each test case: X1 Y1 X2 Y2 X3 Y3 X4 Y4 X5 Y5 X6 Y6. All the coordinate are integer. (X1,Y1) , (X2,Y2), (X3,Y3) forms triangles A ; (X4,Y4) , (X5,Y5), (X6,Y6) forms triangles B.

-10000<=All the coordinate <=10000

Output

For each test case, output “intersect”, “contain” or “disjoint”.

Sample Input
2
0 0 0 1 1 0 10 10 9 9 9 10
0 0 1 1 1 0 0 0 1 1 0 1
Sample Output
disjoint 
intersect 

思路:先判断是否相交,这里是通过  判断任意两条线段的是否存在交点来确定是否相交的;以为两个三角形相交的话,只要判断存在两条线段相交即可;

包含的话是通过面积有判断的,  因为如果是包含关系的话,就可以由   被包含的三角形的三个顶点   来计算另一个三角形(s2)的面积,三个顶点全部计算一边,如果都和另一个三角形(s2)相等的话,就是包含关系;

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    double x,y;
} f[6];
double s0;
node jiaodian(node x1,node x2,node x3,node x4)//求任意两线段的交点(当然这两条线段必须是属于不同的三角形)
{
    double k1,k2;
    if(x1.x==x2.x)
        k1=0;
    else
        k1=(x1.y-x2.y)/(x1.x-x2.x);
    if(x3.x==x4.x)
        k2=0;
    else
        k2=(x3.y-x4.y)/(x3.x-x4.x);
    node dian;
    if(k1==k2)
    {
        dian.x=99999;
        dian.y=99999;
        return dian;
    }
    double a=x1.y-k1*x1.x;
    double b=x3.y-k2*x3.x;
    dian.x=(b-a)/(k1-k2);
    dian.y=k1*dian.x+a;
    return dian;
}
int A(node x1,node x2,node x3,node x4)//由三点求三角形面积
{
    double s1,s2,s3;
    s1=fabs((x1.x*x2.y+x2.x*x4.y+x4.x*x1.y-x1.y*x2.x-x2.y*x4.x-x4.y*x1.x)/2.0);
    s2=fabs((x1.x*x3.y+x3.x*x4.y+x4.x*x1.y-x1.y*x3.x-x3.y*x4.x-x4.y*x1.x)/2.0);
    s3=fabs((x3.x*x2.y+x2.x*x4.y+x4.x*x3.y-x3.y*x2.x-x2.y*x4.x-x4.y*x3.x)/2.0);
    if(fabs(s1+s2+s3-s0)<=0.000001)
        return 1;
    else
        return 0;
}

int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        node q;
        int xj=0,xl=0,bh=0;
        for(int i=0; i<6; i++)
            scanf("%lf %lf",&f[i].x,&f[i].y);
        for(int i=0; i<2; i++)
        {
            for(int l=i+1; l<3; l++)
            {
                for(int j=3; j<5; j++)
                {
                    for(int h=j+1; h<6; h++)
                    {
                        q=jiaodian(f[i],f[l],f[j],f[h]);
                        if(q.x>=max(min(f[i].x,f[l].x),min(f[j].x,f[h].x))&&q.x<=min(max(f[i].x,f[l].x),max(f[j].x,f[h].x))&&
                                (q.y>=max(min(f[i].y,f[l].y),min(f[j].y,f[h].y))&&q.y<=min(max(f[i].y,f[l].y),max(f[j].y,f[h].y))))
                        {
                            xj=1;
                            break;
                        }
                    }
                    if(xj)
                        break;
                }
                if(xj)
                    break;
            }
            if(xj)
                break;
        }
        if(xj)
        {
            printf("intersect\n");
            continue;
        }
        int ans=0;
        s0=fabs((f[0].x*f[1].y+f[1].x*f[2].y+f[2].x*f[0].y-f[0].y*f[1].x-f[1].y*f[2].x-f[2].y*f[0].x)/2.0);
        for(int i=3; i<6; i++)
        {
            if(A(f[0],f[1],f[2],f[i]))
            {
                ans++;
            }
        }
        if(ans==3)
            bh=1;
        if(bh)
            printf("contain\n");
        else
            printf("disjoint\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值