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. 前端-带header和footer的双栏布局

    目标是实现如上图带header和footer的双栏布局,其中右侧sidebar是固定宽度,左侧content是自适应: https://www.zybuluo.com/dengzhirong/note ...

  2. graphQL 启动报错No method or field found with any of the following signatures (with or without one of [interface graphql.schema.DataFetchingEnvironment] as the last argument), in priority order:

    -------------------root.graphqls---------------------------这个文件用来定义属性字段,必须和实体类相同 文件里面的字段写错会报这个错误 com ...

  3. 重载&重写

    重载:同一个类中,方法名相同,方法参数不同(参数个数.参数类型),返回类型无关,所以返回类型不能作为重载的区别依据. 重写:子父类中,子类的方法名.参数位置.参数个数.返回类型和父类一致,方法体不同 ...

  4. mac利用套件管理工具homebrew正确地同时安装python2.7和python3

    MAC OSX 正確地同時安裝 PYTHON 2.7 和 PYTHON3     Python3 出來了(其實已經出來很久了,暈)!但是還是有很多 library 還是使用 Python2.7,所以要 ...

  5. 第8课 Thinkphp 5 update判断修改成功与失败 Thinkphp5商城第四季

    没有修改数据时,判断修改成功与失败 如果提交时的数据库里之前的数据一样(即没有修改就提交表单),会返回0,此时 判断修改成功用$save !== false 这样才会提示修改成功. $save=db( ...

  6. Labyrinth POJ - 1383

    Labyrinth POJ - 1383 The northern part of the Pyramid contains a very large and complicated labyrint ...

  7. 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 ...

  8. loj2073 「JSOI2016」扭动的回文串

    ref 主要是要理解"撑到"最长这个概念 (为啥我的代码这么长QAQ #include <iostream> #include <cstdio> using ...

  9. AutoMapper教程

    http://www.cnblogs.com/gc2013/p/4487567.html http://www.qeefee.com/article/automapper

  10. Hive jdbc连接出现java.sql.SQLException: enabling autocommit is not supported

    1.代码如下 String url = "jdbc:hive2://master135:10000/default"; String user = "root" ...