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

最新文章

  1. LYDSY模拟赛day1 Tourist Attractions
  2. hostapd与wpa_supplicant
  3. 【BZOJ-4590】自动刷题机 二分 + 判定
  4. Java学习笔记(十)&mdash;&mdash;多态
  5. Android_ADB 常用 shell命令 和 sqlite3 简单增删改查
  6. linux搭建一个配置简单的nginx反向代理服务器 2个tomcat
  7. 微信发明人竟是他!也是WeChat/Line/WhatsApp的发明者
  8. 客服端调用自定义宿主的WCF报错&quot;没有终结点在侦听可以接受消息的http://localhost:8085/mex。这通常是由于不正确的地址或者 SOAP 操作导致的错误&quot;的解决方案。
  9. kali2 ssh
  10. EXPLAIN PLAN获取SQL语句执行计划
  11. RichTextBox控件-主要用于输入输出编辑文本信息
  12. 一个php类 Autoloader
  13. oracle中exp,imp的使用详解
  14. ffmpeg之移植到ARM
  15. delphi TColorDialog
  16. 带左右箭头切换的自动滚动图片JS特效
  17. C语言的本质(16)——函数接口的传入参数与传出参数
  18. UnixShell编程(第三版)
  19. ubuntu中切换到root账号方法
  20. 例10-4 uva10791(唯一分解)

热门文章

  1. CSS的垂直居中和水平居中总结
  2. 1043: [HAOI2008]下落的圆盘
  3. linux几种文件传输方式
  4. k8s的认证和service account简述
  5. php微信红包算法
  6. 通用后台管理系统源码,响应式布局,Java管理系统源码,零门槛安装部署
  7. Visual Studio的下载安装
  8. Java课堂作业详解
  9. Flask初学者:配置文件
  10. Cyclic Nacklace HDU - 3746 (kmp)