FZUOJ 2273 Triangles

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2273

题意:给两个三角形六个点的坐标,判断两个三角形的位置关系(相交、内含还是外离)。

思路:先判断是否包含,判断方法就是如果一个点在三角形的内部,那么这个点和三角形三个点的连线的面积和就等于这个三角形的面积,如果三个点都在三角形的内部那么两个三角形就是包含关系,不然就判断线段两两是否相交,如果相交就是相交关系不然就是外离关系。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define MOD 1e9+7
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define Key_Value ch[ch[root][1]][0]
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=500000+5;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-10;
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
int T,sum;
struct Point{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
};
struct Triangle{
    Point a,b,c;
}A,B;
int dcmp(double x){
    if (fabs(x)<eps) return 0;
    else return x<0?-1:1;
}
typedef Point Vector;
Vector operator +(Vector A,Vector B){return Vector(A.x+B.x,A.y+B.y);}
Vector operator -(Vector A,Vector B){return Vector(A.x-B.x,A.y-B.y);}
double Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;}
double Length(Vector A){return sqrt(Dot(A,A));}
double Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;}
double Area2(Point A,Point B,Point C){return Cross(B-A,C-A);}
bool OnSegment(Point p,Point a1,Point a2){
    return dcmp(Cross(a1-p,a2-p))==0 && dcmp(Dot(a1-p,a2-p))<0;
}
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2){
    double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),
           c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
    if (dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0) return true;
    if (dcmp(c1)==0 && OnSegment(b1,a1,a2)) return true;
    if (dcmp(c2)==0 && OnSegment(b2,a1,a2)) return true;
    if (dcmp(c3)==0 && OnSegment(a1,b1,b2)) return true;
    if (dcmp(c4)==0 && OnSegment(a2,b1,b2)) return true;
    return false;
}
bool contain(Triangle A,Triangle B){
    int cnt=0;
    double ss=fabs(Area2(A.a,A.b,A.c));

    double area1=fabs(Area2(B.a,A.a,A.b));
    double area2=fabs(Area2(B.a,A.b,A.c));
    double area3=fabs(Area2(B.a,A.c,A.a));
    if (area1+area2+area3==ss) cnt++,sum++;

    area1=fabs(Area2(B.b,A.a,A.b));
    area2=fabs(Area2(B.b,A.b,A.c));
    area3=fabs(Area2(B.b,A.c,A.a));
    if (area1+area2+area3==ss) cnt++,sum++;

    area1=fabs(Area2(B.c,A.a,A.b));
    area2=fabs(Area2(B.c,A.b,A.c));
    area3=fabs(Area2(B.c,A.c,A.a));
    if (area1+area2+area3==ss) cnt++,sum++;

    return cnt==3;
}
bool ok(){
    if (SegmentProperIntersection(A.a,A.b,B.a,B.b)) return true;
    if (SegmentProperIntersection(A.a,A.b,B.b,B.c)) return true;
    if (SegmentProperIntersection(A.a,A.b,B.c,B.a)) return true;

    if (SegmentProperIntersection(A.b,A.c,B.a,B.b)) return true;
    if (SegmentProperIntersection(A.b,A.c,B.b,B.c)) return true;
    if (SegmentProperIntersection(A.b,A.c,B.c,B.a)) return true;

    if (SegmentProperIntersection(A.c,A.a,B.a,B.b)) return true;
    if (SegmentProperIntersection(A.c,A.a,B.b,B.c)) return true;
    if (SegmentProperIntersection(A.c,A.a,B.c,B.a)) return true;

    return false;
}
void solve(Triangle A,Triangle B){
    sum=0;
    if (contain(A,B) || contain(B,A)){
        puts("contain");
    }
    else if (sum>0 || ok()){
        puts("intersect");
    }
    else puts("disjoint");
}
int main(){
    for (read(T);T;T--){
        scanf("%lf%lf%lf%lf%lf%lf",&A.a.x,&A.a.y,&A.b.x,&A.b.y,&A.c.x,&A.c.y);
        scanf("%lf%lf%lf%lf%lf%lf",&B.a.x,&B.a.y,&B.b.x,&B.b.y,&B.c.x,&B.c.y);
        solve(A,B);
    }
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注