Problem 2273 Triangles

Accept: 201    Submit: 661
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

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

Source

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

 
思路:先判断其中一个三角形a的顶点是否都在另一个三角形b的内部,或者有点在另一个三角形b的边上,抑或b的外部和内部都有a的顶点,这三种情况都好判断,直接输出判断结果。若a的三个顶点都在b的外部,继续判断b的三个顶点与a的关系,可能的关系:b的三个顶点都在a的外部,b的顶点在a内部外部都有,b的顶点都在a的内部。后两种情况好判断,输出结果。第一种情况则要继续判断,此时a,b的顶点都在对方三角形的外部,此时有可能相交,也有可能相离,只需要判断一下其中一个三角形的一条边是否和另一个三角形的某条边相交即可。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<bitset>
#include<set>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
#define N_MAX 10000+4
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EPS 1e-8
typedef long long ll;
struct point {
double x, y;
point(double x = , double y = ) :x(x), y(y) {}
point operator + (point p) { return point(x + p.x, y + p.y); }
point operator - (point p) { return point(x - p.x, y - p.y); }
point operator * (double a) { return point(a*x, a*y); }
double norm() { return x*x + y*y; }
bool operator < (const point &p) const{
return x != p.x ? x + EPS < p.x : y + EPS < p.y;
}
}; double dot(point a,point b) {
return a.x*b.x + a.y*b.y;
}
double det(point a,point b) {
return a.x*b.y - a.y*b.x;
}
typedef vector<point>polygon;
int contain( polygon g, point p) {//1代表在多边形内,0代表在多边形外,2在边上
int n = g.size(); bool x = ;
for (int i = ; i < n;i++) {
point a = g[i] - p, b = g[(i + ) % n] - p;
if (fabs(det(a, b)) < EPS&&dot(a, b) < EPS)return ;
if (a.y > b.y)swap(a, b);
if (a.y<EPS&&b.y>EPS&&det(a, b) > EPS)x = !x;
}
return x ? : ;
}
int ccw(point a,point b,point c) {//顺时针转还是逆时针转
point x = b - a, y = c - a;
if(det(x, y)>EPS)return ;
if (det(x, y) < -EPS)return -;
if (dot(a, b) < -EPS)return ;
if (a.norm() < b.norm())return -;
return ;
} int main() {
int t; scanf("%d",&t);
while (t--) {
polygon T1, T2;
T1.resize(); T2.resize();
for (int i = ; i < ; i++)
scanf("%lf%lf",&T1[i].x,&T1[i].y);
for (int i = ; i < ; i++)
scanf("%lf%lf", &T2[i].x, &T2[i].y);
bool is_wai = , is_nei = ,flag=;
for (int i = ; i < ;i++) {
int c = contain(T1, T2[i]);
if (c==) {
is_nei = ;
}
else if(c==)is_wai = ;
else { flag = ; break; }//在边上一定相交
}
if (flag||(is_wai&&is_nei)) { puts("intersect"); continue; }
if (is_nei && !is_wai) { puts("contain"); }
else {
is_nei = ,is_wai=;
for (int i = ; i < ;i++) {
int c = contain(T2,T1[i]);
if (c == )is_nei = ;
if (c == )is_wai = ;
}
if (is_nei&&!is_wai)puts("contain");
if (is_nei&&is_wai)puts("intersect");
else {//六个点都在另外三角形的外部,判断是否有边相交的情况
bool is_intersect = ;
point a = T1[], b = T1[];
for (int i = ; i < ; i++) {
point c = T2[i], d = T2[(i + ) % ];
if (ccw(a, b, c)*ccw(a, b, d) <= && ccw(c, d, a)*ccw(c, d, b) <= ){
is_intersect = ; break;
}
}
if (is_intersect)puts("intersect");
else puts("disjoint");
}
}
}
return ;
}

FOJ Problem 2273 Triangles的更多相关文章

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

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

  2. FOJ ——Problem 1759 Super A^B mod C

     Problem 1759 Super A^B mod C Accept: 1368    Submit: 4639Time Limit: 1000 mSec    Memory Limit : 32 ...

  3. FOJ Problem 1016 无归之室

     Problem 1016 无归之室 Accept: 926    Submit: 7502Time Limit: 1000 mSec    Memory Limit : 32768 KB  Prob ...

  4. FOJ Problem 1015 土地划分

    Problem 1015 土地划分 Accept: 823    Submit: 1956Time Limit: 1000 mSec    Memory Limit : 32768 KB  Probl ...

  5. foj Problem 2107 Hua Rong Dao

    Problem 2107 Hua Rong Dao Accept: 503    Submit: 1054Time Limit: 1000 mSec    Memory Limit : 32768 K ...

  6. foj Problem 2282 Wand

     Problem 2282 Wand Accept: 432    Submit: 1537Time Limit: 1000 mSec    Memory Limit : 262144 KB Prob ...

  7. foj Problem 2275 Game

    Problem D Game Accept: 145    Submit: 844Time Limit: 1000 mSec    Memory Limit : 262144 KB Problem D ...

  8. foj Problem 2283 Tic-Tac-Toe

                                                                                                    Prob ...

  9. FOJ Problem 2257 Saya的小熊饼干

                                                                                                        ...

随机推荐

  1. DOS当中的基本操作命令,如何切换磁盘,如何查看文件和文件夹,如何清屏,进入文件夹的命令,javac是什么意思,作用是什么?DOS如何建文件夹?退出文件夹?

    如何切换磁盘:使用盘符+:举例 d: 如何查看文件和文件夹 dir/w 如何清屏: cls (clear screen) 进入文件夹的命令cd ,举例cd JDK javac是什么意思,c 是comp ...

  2. 转载:jsonp详解

    json相信大家都用的多,jsonp我就一直没有机会用到,但也经常看到,只知道是“用来跨域的”,一直不知道具体是个什么东西.今天总算搞明白了.下面一步步来搞清楚jsonp是个什么玩意. 同源策略 首先 ...

  3. Centos 安装python 3.7 ,同时兼容python2.7

    下载Python源码 从http://www.python.org/download/根据需要的版本下载源文件. 例如上图就是我在官网直接找到3.5.6版本的下载页面,点击的tar源码包进行下载. 1 ...

  4. 100个linux系统常用指令

    1.ls [选项] [目录名 | 列出相关目录下的所有目录和文件 -a 列出包括.a开头的隐藏文件的所有文件-A 通-a,但不列出"."和".."-l 列出文件 ...

  5. A1095 Cars on Campus (30)(30 分)

    A1095 Cars on Campus (30)(30 分) Zhejiang University has 6 campuses and a lot of gates. From each gat ...

  6. North American Invitational Programming Contest (NAIPC) 2017

    (待补) A. Pieces of Parentheses 将括号处理完成后排序,方式参加下面的博客.然后做一遍背包即可. 2018 Multi-University Training Contest ...

  7. HDU:3336-Count the string(next数组理解)

    Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pr ...

  8. 笔记-python-变量作用域

    笔记-python-变量作用域 1.      python变量作用域和引用范围 1.1.    变量作用域 一般而言程序的变量并不是任何对象或在任何位置都可以访问的,访问权限决定于这个变量是在哪里赋 ...

  9. 笔记-python-tutorial-5.data structure

    笔记-python-tutorial-5.data structure 1.      data structure 1.1.    list operation list.append(x) #尾部 ...

  10. OWINS是什么(转载)

    OWIN的英文全称是Open Web Interface for .NET. 如果仅从名称上解析,可以得出这样的信息:OWIN是针对.NET平台的开放Web接口. 那Web接口是谁和谁之间的接口呢?是 ...