Triangles
FZU - 2273This 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
For each test case, output “intersect”, “contain” or “disjoint”.
2 0 0 0 1 1 0 10 10 9 9 9 10 0 0 1 1 1 0 0 0 1 1 0 1Sample Output
disjoint intersect
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
struct point{
double x,y;
point(){};
point(double xx,double yy):x(xx),y(yy){};
};
struct tri{
point a,b,c;
double s;
tri(){};
tri(point aa,point bb,point cc){
a = aa;
b = bb;
c = cc;
s = abs((b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x)) * 0.5;
}
};
bool judge(point a,point b,point c,double S,point p){
double s1,s2,s3;
s1 = abs((a.x-p.x)*(b.y-p.y) - (a.y-p.y)*(b.x-p.x)) * 0.5;
s2 = abs((a.x-p.x)*(c.y-p.y) - (a.y-p.y)*(c.x-p.x)) * 0.5;
s3 = abs((c.x-p.x)*(b.y-p.y) - (c.y-p.y)*(b.x-p.x)) * 0.5;
if(s1+s2+s3 == S) return true;
else return false;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
double x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6;
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);
tri t1 = tri(point(x1,y1),point(x2,y2),point(x3,y3));
tri t2 = tri(point(x4,y4),point(x5,y5),point(x6,y6));
tri target,checkt;
if(t1.s > t2.s){
target = t1;
checkt = t2;
}
else{
target = t2;
checkt = t1;
}
int cnt = 0;
if(judge(target.a,target.b,target.c,target.s,checkt.a)) cnt++;
if(judge(target.a,target.b,target.c,target.s,checkt.b)) cnt++;
if(judge(target.a,target.b,target.c,target.s,checkt.c)) cnt++;
if(cnt == 3) printf("contain\n");
else if(cnt == 0) printf("disjoint\n");
else printf("intersect\n");
}
return 0;
}