湖南大学ACM程序设计新生杯大赛(同步赛)C - Do you like Banana ?

Posted Fighting Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了湖南大学ACM程序设计新生杯大赛(同步赛)C - Do you like Banana ?相关的知识,希望对你有一定的参考价值。

题目描述

技术分享图片
Two endpoints of two line segments on a plane are given to determine whether the two segments are intersected (there is a common point or there is a partial coincidence that intersects). If intersected, output "Yes", otherwise output "No".

输入描述:

The first line is a number of T, indicating the number of tests inputed (1 <= T <= 1000)
For next T line,each line contains 8 numbers , x1,y1,x2,y2,x3,y3,x4, y4. (8-10 ^ < = xi, yi < = 10 ^ 8)
(the two endpoints of line 1 are x1, y1, |, x2, y2, and two of the endpoints of line 2 are x3, y3, |, x4, y4).

输出描述:

For each test case, output"Yes"  if the two segments intersected, else output"No".
示例1

输入

2
1 2 2 1 0 0 2 2
-1 1 1 1 0 0 1 -1

输出

Yes
No

题解

判断线段非严格相交。

#include<cstdio>
using namespace std;
 
const double eps=1e-8;
#define zero(x)(((x)>0?(x):(-x))<eps)
 
struct point
{
  double x, y;
};
 
double xmult(point p1,point p2,point p0)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
 
int dots_inline(point p1,point p2,point p3)
{
    return zero(xmult(p1,p2,p3));
}
 
int same_side(point p1,point p2,point l1,point l2)
{
    return xmult(l1,p1,l2)*xmult(l1,p2,l2)>eps;
}
 
int dot_online_in(point p,point l1,point l2)
{
    return zero(xmult(p,l1,l2))&&(l1.x-p.x)*(l2.x-p.x)<eps&&(l1.y-p.y)*(l2.y-p.y)<eps;
}
 
int intersect_in(point u1,point u2,point v1,point v2)
{
    if(!dots_inline(u1,u2,v1)||!dots_inline(u1,u2,v2)) return !same_side(u1,u2,v1,v2)&&!same_side(v1,v2,u1,u2);
    return dot_online_in(u1,v1,v2)||dot_online_in(u2,v1,v2)||dot_online_in(v1,u1,u2)||dot_online_in(v2,u1,u2);
}
 
//调用intersect_in,相交返回1
 
int main() {
  int T;
  scanf("%d", &T);
  while(T --) {
    point a, b, c, d;
    scanf("%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y);
    scanf("%lf%lf%lf%lf", &c.x, &c.y, &d.x, &d.y);
    if(intersect_in(a,b,c,d)) printf("Yes\n");
    else printf("No\n");
  }
  return 0;
}

  

 

以上是关于湖南大学ACM程序设计新生杯大赛(同步赛)C - Do you like Banana ?的主要内容,如果未能解决你的问题,请参考以下文章

湖南大学ACM程序设计新生杯大赛(同步赛)B - Build

湖南大学ACM程序设计新生杯大赛(同步赛)L - Liao Han

湖南大学ACM程序设计新生杯大赛(同步赛)A - Array

湖南大学ACM程序设计新生杯大赛(同步赛)G - The heap of socks

湖南大学ACM程序设计新生杯大赛(同步赛)I - Piglet treasure hunt Series 1

湖南大学ACM程序设计新生杯大赛(同步赛)J - Piglet treasure hunt Series 2