Problem 2273 Triangles

Accept: 109    Submit: 360
Time Limit: 1000 mSec    Memory Limit : 262144 KB

 Problem Description

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

20 0 0 1 1 0 10 10 9 9 9 100 0 1 1 1 0 0 0 1 1 0 1

 Sample Output

disjoint intersect

 Source

第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)

链接:http://acm.fzu.edu.cn/contest/problem.php?cid=156&sortid=2

题意:给出两个三角形A,B的三个点的坐标,判断这两个三角形的位置关系,分为包含(contain),相交(intersect),不相交(disjoint)三种关系,特别的,边或者点重合也属于相交;

思路:可以先判断A的某一条边是否和B的某一条边相交(包括点重合和边重合)来判断相交关系,然后可以根据三角形A(B)的一个定点是否在三角形B(A)内来判断包含关系;如不满足前两种关系,则三角形A,B便满足第三种关系;判断某一点是否在三角形内可以用面积法来判断,若点P在三角形内部(包括边上),则有S(ABC)=S(ABP)+S(APC)+S(BPC);否则有S(ABC)小于三个小三角形面积之和;三角形的面积可以利用向量积(叉积)来做,S=|ABxAC|/2;

ps:(如果下面的分析有误,欢迎指出,代码有参考模板)

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define eps 1e-8
#define zero(x) (((x)>0?(x):(-x))<eps) using namespace std; struct point{double x,y;};
struct triangle{point a,b,c;}; ///计算叉积
/**
设向量A=p1-p0,B=p2-p0;
| i j k |
AxB=| p1.x-p0.x p1.y-p0.y 0 |
| p2.x-p0.x p2.y-p0.y 0 |
*/ double xmult(point p1,point p2,point p0){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
} ///判断三点共线
/**
若三点共线 设向量A=p1-p0,B=p2-p0;
|AxB|=|A||B|sin<A,B>,又三点共线,所以<A,B>=0,所以|AxB|=0;
*/
int dots_inline(point p1,point p2,point p3){
return zero(xmult(p1,p2,p3));
}
///判断两点在线段同侧
/**
设向量 A=l2-l1,B=p2-l1,C=p1-l1;
由右手定则可以得出,若p1,p2两点同侧,则|AxB|与|AxC|同正负;
若p1或p2在线段上,则|AxC|或|AxB|等于0
*/
int same_side(point p1,point p2,point l1,point l2){
return xmult(l1,p1,l2) * xmult(l1,p2,l2) > eps;
}
///判断点是否在线上,包括端点
/**
1.先判三点共线;
2.再判点是否在线段上,如果在,则两个端点的横坐标与该点的横坐标(纵坐标)之差的积小于或者等于零;
*/
int dot_online_in(point p,point l1,point l2){
return zero(xmult(p,l1,l2))&&(l1.x-p.x)*(l2.x-p.x) < eps && (l1.y-p.y)*(l2.y-p.y) < eps;
} ///判断两线相交,包括部分重合和点重合
/**
1.先判是否共线,如果不共线,看其中一条边的两个端点是否在另一条线段的两侧;
2.判断点是否在线段上;
*/
int intersect_in(point u1,point u2,point v1,point v2){
if(!dots_inline(u1,u2,v1)||!dots_inline(u1,u2,v2))
return !same_side(u1,u2,v1,v2) && !same_side(v1,v2,u1,u2);
return dot_online_in(u1,v1,v2)||dot_online_in(u2,v1,v2)||dot_online_in(v1,u1,u2)||dot_online_in(v2,u1,u2);
} ///计算面积
/**
S=|AxB|/2;
*/
double area_triangle(point p1,point p2,point p3){
return fabs(xmult(p1,p2,p3))/;
}
///判断点是否包含在三角形内部,包括三条边上
/**
大三角形与三个小三角形的面积之差是否为零
*/
int dot_triangle_in(triangle p1,point p0){
return fabs(area_triangle(p1.a,p1.b,p1.c)-area_triangle(p1.a,p1.b,p0)-area_triangle(p1.a,p0,p1.c)-area_triangle(p0,p1.b,p1.c))<eps;
} void solve_question(){
point a[],b[];
triangle A,B;
for(int i=;i<;i++)
cin >> a[i].x >> a[i].y;
for(int i=;i<;i++)
cin >> b[i].x >> b[i].y;
A = (triangle){a[],a[],a[]};
B = (triangle){b[],b[],b[]};
for(int i=;i<;i++)
for(int j=;j<;j++)
if(intersect_in(a[i],a[(i+)%],b[j],b[(j+)%])) { printf("intersect\n");return;}
if(dot_triangle_in(A,b[]) || dot_triangle_in(B,a[]))
printf("contain\n");
else
printf("disjoint\n"); } int main(){
int T;
scanf("%d",&T);
while(T--){
solve_question();
}
}

FZUOJ-2273 Triangles的更多相关文章

  1. FOJ Problem 2273 Triangles

    Problem 2273 Triangles Accept: 201    Submit: 661Time Limit: 1000 mSec    Memory Limit : 262144 KB P ...

  2. FZU 2273 Triangles 第八届福建省赛 (三角形面积交 有重边算相交)

    Problem Description This is a simple problem. Given two triangles A and B, you should determine they ...

  3. Count the number of possible triangles

    From: http://www.geeksforgeeks.org/find-number-of-triangles-possible/ Given an unsorted array of pos ...

  4. [ACM_搜索] Triangles(POJ1471,简单搜索,注意细节)

    Description It is always very nice to have little brothers or sisters. You can tease them, lock them ...

  5. acdream.Triangles(数学推导)

    Triangles Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit Stat ...

  6. UVA 12651 Triangles

    You will be given N points on a circle. You must write a program to determine how many distinctequil ...

  7. Codeforces Gym 100015F Fighting for Triangles 状压DP

    Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...

  8. Codeforces Round #309 (Div. 1) C. Love Triangles dfs

    C. Love Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/553/pro ...

  9. Codeforces Round #308 (Div. 2) D. Vanya and Triangles 水题

    D. Vanya and Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55 ...

随机推荐

  1. 基于vue上传base64图片,通过canvas压缩base64

    其实和vue关系不大,和我们之前做上传压缩性质是一样的 当然下面的代码是没有处理ios横屏拍照的bug的 有兴趣的可以多搜一下  网上都有相应的解答 .. var that = this if (e. ...

  2. Java语言基础1-关键字、标识符、常量和变量

    关键字-标识符-常量和变量-运算符-流程控制-方法-数组 1.关键字 keyword Java系统中已经赋予了特殊含义的单词 特点:全部是小写字母注意: Java中的保留字:现在没有使用,以后有可能会 ...

  3. MVC模式 和 MVVM模式

    MVC模式 模型 - 视图 - 控制器或MVC,MVC是普遍的叫法,是一种软件设计模式,用于开发Web应用程序.模型- 视图 - 控制器模式是由以下三部分组成: 模型/Model - 一个负责维护数据 ...

  4. shiro常见的异常以及处理方法

    1.shiro的常见异常 1.1  AuthenticationException 异常是Shiro在登录认证过程中,认证失败需要抛出的异常. AuthenticationException包含以下子 ...

  5. 0-4评价一个语言模型Evaluating Language Models:Perplexity

    有了一个语言模型,就要判断这个模型的好坏. 现在假设: 我们有一些测试数据,test data.测试数据中有m个句子;s1,s2,s3-,sm 我们可以查看在某个模型下面的概率: 我们也知道,如果计算 ...

  6. Java面试之基础篇(4)

    31.String s = new String("xyz");创建了几个StringObject?是否可以继承String类? 两个或一个都有可能,”xyz”对应一个对象,这个对 ...

  7. linux运维、架构之路-CentOS7

    一.CentOS7介绍 1.CentOS7使用起来最大的变化就是服务管理 2.systemd是linux下的一种init软件,开发目标是提供更优秀的框架以表示系统服务间的依赖关系,并依此实现系统初始化 ...

  8. CF G. Orientation of Edges BFS

    来两遍 $BFS,$ 都贪心一下即可. #include <bits/stdc++.h> #define maxn 300009 using namespace std; void set ...

  9. 题解 P2674 【《瞿葩的数字游戏》T2-多边形数】

    题目说了很清楚,此题找规律,那么就找规律. 我们观察数列. 令k表示数列的第k个数. 三角形数:1 3 6 10 15 两项相减:1 2 3 4 5 再次相减:1 1 1 1 1 四边形数:1 4 9 ...

  10. Python_016(面向对象之属性和类方法)

    一.特性(property,setter,deleter) 1.属性:将一个方法伪装成一个属性,在代码级别上没有本质的提升,但是看起来更合理; class Person: def __init__(s ...