线段相交

 1 #include <iostream>
 2 #include <queue>
 3 #include <cstdio>
 4 #include <algorithm>
 5 using namespace std;
 6 struct point
 7 {
 8  double x,y;
 9 };
10 point p[4];
11 int kuai()
12 {
13  if(min(p[0].x,p[1].x)<=max(p[2].x,p[3].x)&&
14   max(p[0].x,p[1].x)>=min(p[2].x,p[3].x)&&
15   min(p[0].y,p[1].y)<=max(p[2].y,p[3].y)&&
16   max(p[0].y,p[1].y)>=min(p[2].y,p[3].y))
17   return 1;
18  return 0;
19 }
20 double cha(point p0,point p1,point p2)
21 {
22  return (p2.y-p0.y)*(p1.x-p0.x)-(p2.x-p0.x)*(p1.y-p0.y);
23 }
24 int kua()
25 {
26   if(cha(p[2],p[3],p[1])*cha(p[2],p[3],p[0])<=0&&cha(p[0],p[1],p[2])*cha(p[0],p[1],p[3])<=0)
27     return 1;
28   return 0;
29 }
30 int main()
31 {
32  int i,n;
33  //freopen("in.txt","r",stdin);
34  cin>>n;
35  while(n--)
36  {
37   for(i=0;i<4;i++)
38    cin>>p[i].x>>p[i].y;
39   if(kua())
40     cout<<"Yes"<<endl;
41   else
42     cout<<"No"<<endl;
43  }
44 }
时间: 2024-07-30 06:33:33

线段相交的相关文章

HDOJ1086-You can Solve a Geometry Problem too(线段相交)

Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is ve

POJ 2653 Pick-up sticks [线段相交 迷之暴力]

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12861   Accepted: 4847 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to fin

poj 3304 直线与线段相交

Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12161   Accepted: 3847 Description Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments

poj 2653 线段与线段相交

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11884   Accepted: 4499 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to fin

poj2826(线段相交)

传送门:An Easy Problem?! 题意:用两条线段接雨水,雨水是垂直落下的,问我们用给定的两条线段能接到多少水. 分析:看起来很简单,写起来略麻烦,先排除不能接到水的情况: 1. 两条线段不相交: 2. 其中任意一条线段水平: 3. 两条线段重合: 4. 相交的情况下,最高的端点遮住了次高的端点 最后求线段交点确定三角形并用叉积求面积. #include <iostream> #include <stdio.h> #include <string.h> #in

hdu 1086(判断线段相交)

传送门:You can Solve a Geometry Problem too 题意:给n条线段,判断相交的点数. 分析:判断线段相交模板题,快速排斥实验原理就是每条线段代表的向量和该线段的一个端点与 另一条线段的两个端点构成的两个向量求叉积,如果线段相交那么另一条线段两个端点必定在该线段的两边,则该线段代表的向量必定会顺时针转一遍逆时针转一遍,叉积必定会小于等于0,同样对另一条线段这样判断一次即可. #include <algorithm> #include <cstdio>

hdu1086(线段相交)

题目意思: 给出n个线段,判断这n条线段中,线段相交的对数. http://acm.hdu.edu.cn/showproblem.php?pid=1086 题目分析: 此题主要写出判断线段相交的函数,然后判断每一对线段即可,时间复杂度O(n*n).详细解释见代码. AC代码: /** *判断AB和CD两线段是否有交点: *同时满足两个条件:('x'表示叉积) * 1.C点D点分别在AB的两侧.(向量(ABxAC)*(ABxAD)<=0) * 2.A点和B点分别在CD两侧.(向量(CDxCA)*(

HDU 1086 You can Solve a Geometry Problem too(判断线段相交)

题目地址:HDU 1086 就这么一道仅仅判断线段相交的题目写了2k多B的代码..是不是有点浪费...但是我觉得似乎哪里也优化不了了.... 判断线段相交就是利用的叉积.假如现在两条线段分别是L1和L2,先求L1和L2两个端点与L1的某个端点的向量的叉积,如果这两个的叉积的乘积小于0的话,说明L1在是在L2两个端点之间的,但此时并不保证一定相交.此时需要用同样的方法去判断L2是否在L1的两个端点之间,如果L2也在L1的两个端点之间的话,那就足以说明L1与L2相交.但是这题还需要判断是否端点也相交

直线与直线相交 直线与线段相交 线段与线段相交

int sgn(double x) { if(fabs(x) < eps) return 0; return x < 0 ? -1:1; } struct Point { double x,y; Point() {} Point(double _x,double _y) { x = _x,y = _y; } Point operator -(const Point &b)const { return Point(x - b.x,y - b.y); } //叉积 double opera

POJ2653 Pick-up sticks(线段相交)

题目链接: http://poj.org/problem?id=2653 题目描述: Pick-up sticks Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks su