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
 
观察样例可以发现,如果边有重叠部分,此题也算相交。
因此我套用多边形面积交的模板http://www.cnblogs.com/Annetree/p/6535294.html
如果有重合面积,有两种情况:包含或相交,特殊判断包含即可
如果没有重合面积,也有两种情况:相交(线)和相离,特殊判断线部分重合即可
 
  1. #include<cstdio>
  2. #include<cmath>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<cstdlib>
  7. #include<queue>
  8. #include<map>
  9. #include<stack>
  10. #include<set>
  11.  
  12. using namespace std;
  13.  
  14. const int maxn=;
  15. const int maxisn=;
  16. const double eps=1e-;
  17. const double pi=acos(-1.0);
  18.  
  19. int dcmp(double x){
  20. if(x>eps) return ;
  21. return x<-eps ? - : ;
  22. }
  23. inline double Sqr(double x){
  24. return x*x;
  25. }
  26.  
  27. #define zero(x)(((x)>0?(x):-(x))<eps)
  28. struct Point{
  29. double x,y;
  30. Point(){x=y=;}
  31. Point(double x,double y):x(x),y(y){};
  32. friend Point operator + (const Point &a,const Point &b) {
  33. return Point(a.x+b.x,a.y+b.y);
  34. }
  35. friend Point operator - (const Point &a,const Point &b) {
  36. return Point(a.x-b.x,a.y-b.y);
  37. }
  38. friend bool operator == (const Point &a,const Point &b) {
  39. return dcmp(a.x-b.x)==&&dcmp(a.y-b.y)==;
  40. }
  41. friend Point operator * (const Point &a,const double &b) {
  42. return Point(a.x*b,a.y*b);
  43. }
  44. friend Point operator * (const double &a,const Point &b) {
  45. return Point(a*b.x,a*b.y);
  46. }
  47. friend Point operator / (const Point &a,const double &b) {
  48. return Point(a.x/b,a.y/b);
  49. }
  50. friend bool operator < (const Point &a, const Point &b) {
  51. return a.x < b.x || (a.x == b.x && a.y < b.y);
  52. }
  53. inline double dot(const Point &b)const{
  54. return x*b.x+y*b.y;
  55. }
  56. inline double cross(const Point &b,const Point &c)const{
  57. return (b.x-x)*(c.y-y)-(c.x-x)*(b.y-y);
  58. }
  59.  
  60. };
  61.  
  62. Point LineCross(const Point &a,const Point &b,const Point &c,const Point &d){
  63. double u=a.cross(b,c),v=b.cross(a,d);
  64. return Point((c.x*v+d.x*u)/(u+v),(c.y*v+d.y*u)/(u+v));
  65. }
  66. double PolygonArea(Point p[],int n){
  67. if(n<) return 0.0;
  68. double s=p[].y*(p[n-].x-p[].x);
  69. p[n]=p[];
  70. for(int i=;i<n;i++){
  71. s+=p[i].y*(p[i-].x-p[i+].x);
  72. }
  73. return fabs(s*0.5);
  74. }
  75. double CPIA(Point a[],Point b[],int na,int nb){
  76. Point p[maxisn],temp[maxisn];
  77. int i,j,tn,sflag,eflag;
  78. a[na]=a[],b[nb]=b[];
  79. memcpy(p,b,sizeof(Point)*(nb+));
  80. for(i=;i<na&&nb>;++i){
  81. sflag=dcmp(a[i].cross(a[i+],p[]));
  82. for(j=tn=;j<nb;++j,sflag=eflag){
  83. if(sflag>=) temp[tn++]=p[j];
  84. eflag=dcmp(a[i].cross(a[i+],p[j+]));
  85. if((sflag^eflag)==-)
  86. temp[tn++]=LineCross(a[i],a[i+],p[j],p[j+]);
  87. }
  88. memcpy(p,temp,sizeof(Point)*tn);
  89. nb=tn,p[nb]=p[];
  90. }
  91. if(nb<) return 0.0;
  92. return PolygonArea(p,nb);
  93. }
  94. double SPIA(Point a[],Point b[],int na,int nb){
  95. int i,j;
  96. Point t1[],t2[];
  97. double res=0.0,if_clock_t1,if_clock_t2;
  98. a[na]=t1[]=a[];
  99. b[nb]=t2[]=b[];
  100. for(i=;i<na;i++){
  101. t1[]=a[i-],t1[]=a[i];
  102. if_clock_t1=dcmp(t1[].cross(t1[],t1[]));
  103. if(if_clock_t1<) swap(t1[],t1[]);
  104. for(j=;j<nb;j++){
  105. t2[]=b[j-],t2[]=b[j];
  106. if_clock_t2=dcmp(t2[].cross(t2[],t2[]));
  107. if(if_clock_t2<) swap(t2[],t2[]);
  108. res+=CPIA(t1,t2,,)*if_clock_t1*if_clock_t2;
  109. }
  110. }
  111. return res;
  112. //return PolygonArea(a,na)+PolygonArea(b,nb)-res;
  113. }
  114.  
  115. Point a[],b[];
  116. Point aa[],bb[];
  117.  
  118. double length(Point p1,Point p2)//求边长
  119. {
  120. double res=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
  121. return res;
  122. }
  123.  
  124. double area_triangle(double l1,double l2,double l3)//求三角形面积
  125. {
  126. double s=(l1+l2+l3)/2.0;
  127. double res=sqrt(s*(s-l1)*(s-l2)*(s-l3));
  128. return res;
  129. }
  130.  
  131. double xmult(Point p1,Point p2,Point p0)
  132. {
  133. return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
  134. }
  135.  
  136. int parallel(Point u1,Point u2,Point v1,Point v2)//判断平行
  137. {
  138. return zero((u1.x-u2.x)*(v1.y-v2.y)-(v1.x-v2.x)*(u1.y-u2.y));
  139. }
  140.  
  141. int dot_online_in(Point p,Point l1,Point l2)
  142. {
  143. return zero(xmult(p,l1,l2))&&(l1.x-p.x)*(l2.x-p.x)<eps&&(l1.y-p.y)*(l2.y-p.y)<eps;
  144. }
  145. int main()
  146. {
  147. int T;
  148. scanf("%d",&T);
  149. while(T--)
  150. {
  151. for(int i=;i<;i++) scanf("%lf %lf",&a[i].x,&a[i].y);
  152. for(int i=;i<;i++) scanf("%lf %lf",&b[i].x,&b[i].y);
  153. double area_double =fabs(SPIA(a,b,,));//重合面积
  154. double l1=length(a[],a[]),l2=length(a[],a[]),l3=length(a[],a[]);
  155. double l4=length(b[],b[]),l5=length(b[],b[]),l6=length(b[],b[]);
  156. if(area_double>eps) //包含或相交
  157. {
  158. if(abs(area_double-area_triangle(l1,l2,l3))<eps)
  159. printf("contain\n");
  160. else if(abs(area_double-area_triangle(l4,l5,l6))<eps)
  161. printf("contain\n");
  162. else
  163. printf("intersect\n");
  164. }
  165. else //相交或相离
  166. {
  167. bool flag=false;
  168. //判断是否有边重合
  169. for(int i=;i<;i++)
  170. {
  171. for(int ii=i+;ii<;ii++)
  172. {
  173. for(int j=;j<;j++)
  174. {
  175. for(int jj=j+;jj<;jj++)
  176. {
  177. if(parallel(a[i],a[ii],b[j],b[jj]))
  178. if(dot_online_in(a[i],b[j],b[jj]))
  179. {
  180. flag=true;
  181. break;
  182. }
  183. }
  184. if(flag)
  185. break;
  186. }
  187. if(flag)
  188. break;
  189. }
  190. if(flag)
  191. break;
  192. }
  193. if(flag)
  194. printf("intersect\n");
  195. else
  196. printf("disjoint\n");
  197.  
  198. }
  199. }
  200. return ;
  201. }

FZU 2273 Triangles 第八届福建省赛 (三角形面积交 有重边算相交)的更多相关文章

  1. FZU 2272 Frog 第八届福建省赛 (鸡兔同笼水题)

    Problem Description Therearex frogs and y chicken in a garden. Kim found there are n heads and m leg ...

  2. FOJ Problem 2273 Triangles

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

  3. 第八届河南省赛B.最大岛屿(dfs)

    B.最大岛屿 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 30  Solved: 18 [Submit][Status][Web Board] De ...

  4. TZOJ 2519 Regetni(N个点求三角形面积为整数总数)

    描述 Background Hello Earthling. We're from the planet Regetni and need your help to make lots of mone ...

  5. ytu 1058: 三角形面积(带参的宏 练习)

    1058: 三角形面积 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 190  Solved: 128[Submit][Status][Web Boar ...

  6. UVa 11437:Triangle Fun(计算几何综合应用,求直线交点,向量运算,求三角形面积)

    Problem ATriangle Fun Input: Standard Input Output: Standard Output In the picture below you can see ...

  7. OpenJudge计算概论-计算三角形面积【海伦公式】

    /*============================================== 计算三角形面积 总时间限制: 1000ms 内存限制: 65536kB 描述 平面上有一个三角形,它的 ...

  8. bzoj 1845: [Cqoi2005] 三角形面积并 扫描线

    1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 848  Solved: 206[Submit][Statu ...

  9. nyoj 67 三角形面积【三角形面积公式】

    三角形面积 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 给你三个点,表示一个三角形的三个顶点,现你的任务是求出该三角形的面积   输入 每行是一组测试数据,有6个 ...

随机推荐

  1. 【其他】【Restful】【1】简单了解Restful概念

    内容: REST是一种设计风格,不是一种标准,是一种思想.符合REST原则的架构方式即可称为RESTful. 在Restful之前的操作:http://127.0.0.1/user/query/1 G ...

  2. python-Tornado 框架

    概述 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了 ...

  3. http.cof

    ## This is the main Apache HTTP server configuration file. It contains the# configuration directives ...

  4. jQuery-全屏滚动插件【fullPage.js】API 使用方法总结

    jQuery-全屏滚动插件[fullPage.js]API 使用方法总结   jQuery-全屏滚动插件fullPage.js使用方法总结 作者github及下载地址:https://github.c ...

  5. Android 之常用布局

    LinearLayout 线性布局. android:orientation="horizontal" 制定线性布局的排列方式 水平 horizontal 垂直 vertical ...

  6. Tomcat压缩传输设置

    1.配置位于server.xml文件中的Connector节点下,具体参数如下: 参数 默认值 参数说明 compression off 是否开启压缩传输 compressableMimeType t ...

  7. ES6中的Promise.resolve()的作用

    var foo = { then: (resolve, reject) => resolve('foo') }; var resolved = Promise.resolve(foo); 相当于 ...

  8. 【MVC】快速构建一个图片浏览网站

    当抄完MusicStore时,你应该对MVC有一个比较清晰的认识了.接下来就需要做个网站来继续增加自己的知识了.那么,该做个什么网站呢.做个图片浏览网站吧,简单而实用. 简单设计 1.首先,页面中间是 ...

  9. HTML5 ②

    html代码中的各种标签和特殊符号: 1.<p>段落标签 默认会在段落前后都有空行</p>: 2.<hr/> 水平线标签     <br/>换行标签   ...

  10. docker 部署 flask(三)高级编写及生成镜像,安装requirements.txt

    简介: 上一篇,我写了如何使用别人的docker基础镜像,生成我们的docker镜像. 也就最基本的flask,没有别的库(包)支持.连数据库支持都没有. 也就让大家了解一下怎么生成镜像而已. 本篇介 ...