Triangles FZU - 2273 (叉积判断点是否在三角形内)

Triangles

FZU - 2273

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 


叉积:判断点在三角形内


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值