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;
}