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
https://cn.vjudge.net/problem/FZU-2273
给定两个三角形的坐标,求两者的关系(包含,相交,相离);
#include
#include
#include
double area(double a, double b, double c)//根据三边求三角形面积
{
double p = (a+b+c)/2;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
double len(double x1,double y1,double x2, double y2)//两点之间的距离
{
return sqrt((x1-x2)*(x1-x2) +(y1-y2)*(y1-y2));
}
int is_inside(double x, double y,double x1, double y1,double x2, double y2,double x3, double y3)//求一个点在不在三角形内部
{
double s1,s2,s3,s,l1,l2,l3,l4,l5,l6;
l1 = len(x1,y1,x2,y2);
l2 = len(x1,y1,x3,y3);
l3 = len(x3,y3,x2,y2);
l4 = len(x,y,x1,y1);
l5 = len(x,y,x2,y2);
l6 = len(x,y,x3,y3);
s = area(l1,l2,l3);
s1 = area(l1,l4,l5);
s2 = area(l2,l4,l6);
s3 = area(l3,l5,l6);
if(fabs(s-s1-s2-s3) < 0.000001)//三角形面积 和 此点与三角构成的三个三角形面积之和相等,则此点在三角形内部
return 1;
else
return 0;
}
int main()
{
double x1,x2,x3,y1,y2,y3,x4,x5,x6,y4,y5,y6;
int t,f1,f2,f3,f4,f5,f6;
scanf("%d",&t);
while(t --)
{
scanf("%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4,&x5,&y5,&x6,&y6);
f1 = is_inside(x1,y1,x4,y4,x5,y5,x6,y6);
f2 = is_inside(x2,y2,x4,y4,x5,y5,x6,y6);
f3 = is_inside(x3,y3,x4,y4,x5,y5,x6,y6);
f4 = is_inside(x4,y4,x1,y1,x2,y2,x3,y3);
f5 = is_inside(x5,y5,x1,y1,x2,y2,x3,y3);
f6 = is_inside(x6,y6,x1,y1,x2,y2,x3,y3);
if(!(f1||f2||f3||f4||f5||f6))//六个点都不在另一个三角形内部
printf("disjoint\n");
else if((f1&&f2&&f3)||(f4&&f5&&f6))//一个点在另一个三角形内部
printf("contain\n");
else
printf("intersect\n");
}
return 0;
}