【题目链接】FZU
【题意】给定两个三角形6个点的坐标,判断两个三角形是包含、相交还是相离。
【样例】
【分析】
【代码】
#include<stdio.h>
#include<iostream>
#include<cmath>
using namespace std;
int ans;
typedef struct{
double x,y;
}Point;
Point S[6];
double area(Point a,Point b,Point c){
double x1,y1,x2,y2,x3,y3;
x1=a.x;x2=b.x;x3=c.x;
y1=a.y;y2=b.y;y3=c.y;
double l1,l2,l3;
l1=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
l2=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
l3=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
if(!l1||!l2||!l3){
return -1;
}
if(l1+l2==l3||l2+l3==l1||l1+l3==l1){
return -1;
}
double cos=(l1*l1+l2*l2-l3*l3)/(2*l1*l2);
double sin=sqrt(1-cos*cos);
double ss=0.5*l1*l2*sin;
return ss;
}
double mult(Point a, Point b, Point c){
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
bool intersect(Point aa, Point bb, Point cc, Point dd){
if ( max(aa.x, bb.x)<min(cc.x, dd.x) )
return false;
if ( max(aa.y, bb.y)<min(cc.y, dd.y) )
return false;
if ( max(cc.x, dd.x)<min(aa.x, bb.x) )
return false;
if ( max(cc.y, dd.y)<min(aa.y, bb.y) )
return false;
if ( mult(cc, bb, aa)*mult(bb, dd, aa)<0 )
return false;
if ( mult(aa, dd, cc)*mult(dd, bb, cc)<0 )
return false;
ans++;
return true;
}
int main(){
int T;
bool ins;
cin>>T;
while(T--){
ans=0;
for(int i=0;i<6;i++){
cin>>S[i].x>>S[i].y;
}
intersect(S[0],S[1],S[3],S[4]);
intersect(S[0],S[1],S[3],S[5]);
intersect(S[0],S[1],S[4],S[5]);
intersect(S[0],S[2],S[3],S[4]);
intersect(S[0],S[2],S[3],S[5]);
intersect(S[0],S[2],S[4],S[5]);
intersect(S[1],S[2],S[3],S[4]);
intersect(S[1],S[2],S[3],S[5]);
intersect(S[1],S[2],S[4],S[5]);
//cout<<ans<<endl;
if(!ans){
ins=false;
double s=area(S[0],S[1],S[2]);
for(int i=3;i<6;i++){
double s1=area(S[0],S[1],S[i]);
double s2=area(S[1],S[2],S[i]);
double s3=area(S[0],S[2],S[i]);
if(s1==-1||s2==-1||s3==-1){
break;
}
if(abs(s1+s2+s3-s)<1e-9)
ins=true;
}
if(ins)
printf("contain\n");
else
printf("disjoint\n");
}
else
printf("intersect\n");
}
return 0;
}
【说明】