B - 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 

题解:判断三角形的点在另一个三角形的位置即可,其他看代码即可。

注意:题目两个完全相同的三角形是contain!!!

#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
struct node
{
    double x,y;
} p[10],q[10];
double Area(double a,double b,double c)//三角形的面积海伦凯勒公式
{
    double p=(a+b+c)/2;
    return sqrt(p*(p-a)*(p-b)*(p-c));
}
double Length(node a,node b)//两点之间的长度
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool ok(node x,node a,node b,node c)//x点是否在a,b,c组成的三角形内,画个图容易理解
{
    double L1=Length(a,b);
    double L2=Length(a,c);
    double L3=Length(b,c);
    double L4=Length(x,a);
    double L5=Length(x,b);
    double L6=Length(x,c);
    double area=Area(L1,L2,L3);
    double area1=Area(L1,L4,L5);
    double area2=Area(L2,L4,L6);
    double area3=Area(L3,L5,L6);
    if(fabs(area-area1-area2-area3)<0.000001)//注意加上绝对值
        return 1;
    return 0;
}
int main()
{
    std::ios::sync_with_stdio(0);
    int t;
    cin>>t;
    while(t--)
    {
        for(int i=1; i<=3; i++)
            cin>>p[i].x>>p[i].y;
        for(int i=1; i<=3; i++)
            cin>>q[i].x>>q[i].y;
        int flag1=ok(q[1],p[1],p[2],p[3]);//依次判断每个点在另一个三角形的位置
        int flag2=ok(q[2],p[1],p[2],p[3]);
        int flag3=ok(q[3],p[1],p[2],p[3]);
        int flag4=ok(p[1],q[1],q[2],q[3]);
        int flag5=ok(p[2],q[1],q[2],q[3]);
        int flag6=ok(p[3],q[1],q[2],q[3]);
        if(flag1==0&&flag2==0&&flag3==0&&flag4==0&&flag5==0&&flag6==0)//每个点都在另一个三角形的外部
            cout<<"disjoint"<<endl;
        else                
        {
            if((flag1==1&&flag2==1&&flag3==1)||(flag4==1&&flag5==1&&flag6==1))//包含,即一个三角形的三个点全部在另一个三角形中
                cout<<"contain"<<endl;
            else
                cout<<"intersect"<<endl;
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值