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. MySQL - 表中某个状态字段的状态表示区分最好用数字,如status - [9999:失败,1111:成功]

    表中某个状态字段的状态表示区分最好用数字,如status - [9999:失败,1111:成功]

  2. js动态刷新时间

    <script type="text/javascript"> //取得系统当前时间 function getTime(){ var myDate = new Date ...

  3. web前端-回调函数sort详解

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. mysql的性能优化案例

    在一次项目实现中,以前写了个程序,将在txt文件中的电话号码和对应的类型往数据库中插入,小数据量的情况下,用个数组遍历循环的方式,很容易解决,但是当数据量一下 但是,几十万个电话一次性插入,就变得耗时 ...

  5. 深入解析AJAX的原理

    AJAX:Asynchronous JavaScript And Xml(异步的JS和XML) 同步:客户端发起请求>服务端的处理和响应>客户端重新载入页面(循环) 异步:客户端实时请求& ...

  6. 精通SpringBoot--分页查询功能的实现

    本文将介绍如何实现分页查询功能,推荐使用github的pagehelper插件实现(事实上大家基本都是这么干的),但本文的实现方式和大多数不同,废话少说,现在就带着大家看看区别在哪里.先看pom.xm ...

  7. python3.7 os模块

    #!/usr/bin/env python __author__ = "lrtao2010" #python3.7 os模块 #os模块是与操作系统交互的一个接口 # os.get ...

  8. stm32基本定时器timer6的原理与使用

    /********************基本定时器 TIM 参数定义,只限 TIM6.7************/ /* 一.定时器分类 STM32F1 系列中,除了互联型的产品,共有 8 个定时器 ...

  9. Farm Tour POJ - 2135 (最小费用流)

    When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= ...

  10. 拓扑排序+不是字典序的优先级排列(POJ3687+HDU4857)

    一.前言 在过去的一周里结束了CCSP的比赛,其中有一道题卡了我9个小时,各种调错都没法完整的调处来这题,于是痛下决心开始补题,这个是计划的一部分.事实上,基于错误的理解我写了若干发拓扑排序+字典序的 ...