2017acm福建省赛FZU 2273 Triangles

本文介绍了一种用于判断两个三角形是否相交、内含或分离的算法,并提供了详细的实现代码。通过计算有向面积和三角形面积,结合线段相交检测,实现了准确的三角形位置关系判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem 2273 Triangles

Accept: 34    Submit: 82
Time Limit: 1000 mSec    Memory Limit : 262144 KB

Problem Description

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

Source

第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)
判断两个三角形是 内含 还是相交 还是分离

什么都不说 直接上模板吧

#include<stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
#define ABS_FLOAT_0 0.0001
double xx[88],yy[88];
struct node
{
    double x, y;
} st1, ed1, st2, ed2;

struct point_float
{
    float x;
    float y;
};

double get_area(node a0, node a1, node a2)   //求有向面积
{
    double s = a0.x*a1.y + a2.x*a0.y +a1.x*a2.y - a2.x*a1.y - a0.x*a2.y - a1.x*a0.y;
    return s;
}

float GetTriangleSquar(const point_float pt0, const point_float pt1, const point_float pt2)
{
    point_float AB,   BC;
    AB.x = pt1.x - pt0.x;
    AB.y = pt1.y - pt0.y;
    BC.x = pt2.x - pt1.x;
    BC.y = pt2.y - pt1.y;
    return fabs((AB.x * BC.y - AB.y * BC.x)) / 2.0f;
}

bool IsInTriangle(const point_float A, const point_float B, const point_float C, const point_float D)
{
    float SABC, SADB, SBDC, SADC;
    SABC = GetTriangleSquar(A, B, C);
    SADB = GetTriangleSquar(A, D, B);
    SBDC = GetTriangleSquar(B, D, C);
    SADC = GetTriangleSquar(A, D, C);

    float SumSuqar = SADB + SBDC + SADC;

    if ((-ABS_FLOAT_0 < (SABC - SumSuqar)) && ((SABC - SumSuqar) < ABS_FLOAT_0))
    {
        return true;
    }
    else
    {
        return false;
    }
}


int pd()
{
    double s1 = get_area(st1, ed1, st2);
    double s2 = get_area(st1, ed1, ed2);
    double s3 = get_area(st2, ed2, st1);
    double s4 = get_area(st2, ed2, ed1);
    if(s1 * s2 <= 0 && s3 * s4 <= 0)
        return 1; //printf("Interseetion\n");
    else
        return 0;  //printf("Not Interseetion\n");
}


int main()
{
    int t,flag,i,j,k,h;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lf %lf %lf %lf %lf %lf",&xx[1],&yy[1],&xx[2],&yy[2],&xx[3],&yy[3]);
        scanf("%lf %lf %lf %lf %lf %lf",&xx[4],&yy[4],&xx[5],&yy[5],&xx[6],&yy[6]);
        flag=0;
        for(i=1; i<=3; i++)
        {
            st1.x=xx[i];
            st1.y=yy[i];
            for(k=1; k<=3; k++)
            {
                if(k!=i)
                {
                    ed1.x=xx[k];
                    ed1.y=yy[k];
                    for(j=4; j<=6; j++)
                    {
                        st2.x=xx[j];
                        st2.y=yy[j];
                        for(h=4; h<=6; h++)
                        {
                            if(h!=j)
                            {
                                ed2.x=xx[h];
                                ed2.y=yy[h];
                                if(pd())
                                {
                                    flag=1;
                                    break;
                                }
                            }
                        }
                    }

                }
            }
        }  //利用两线段是否相交 判断三角形是否相交
        if(flag)
        {
            printf("intersect\n");
            continue;
        }

        point_float A, B, C, P;
        A.x =xx[1];
        A.y =yy[1] ;
        B.x =xx[2] ;
        B.y =yy[2] ;
        C.x =xx[3] ;
        C.y =yy[3] ;
       //判断三个点是否在三角形内
        for(i=4; i<=6; i++)
        {
            P.x=xx[i];
            P.y=yy[i];
            if(IsInTriangle(A, B, C, P))
            {
                flag=1;
                break;
            }
        }
        //再判断A在B里面吗
        A.x =xx[4];
        A.y =yy[4] ;
        B.x =xx[5] ;
        B.y =yy[5] ;
        C.x =xx[6] ;
        C.y =yy[6] ;
       //判断三个点是否在三角形内
        for(i=1; i<=3; i++)
        {
            P.x=xx[i];
            P.y=yy[i];
            if(IsInTriangle(A, B, C, P))
            {
                flag=1;
                break;
            }
        }

        if(flag)
        {
            printf("contain\n");
            continue;
        }
        else printf("disjoint\n");
    }
    return 0;
}



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值