1264 线段相交

1264 线段相交

基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题

给出平面上两条线段的两个端点,判断这两条线段是否相交(有一个公共点或有部分重合认为相交)。 如果相交,输出"Yes",否则输出"No"。

Input

第1行:一个数T,表示输入的测试数量(1 <= T <= 1000) 第2 - T + 1行:每行8个数,x1,y1,x2,y2,x3,y3,x4,y4。(-10^8 <= xi, yi <= 10^8) (直线1的两个端点为x1,y1 | x2, y2,直线2的两个端点为x3,y3 | x4, y4)

Output

输出共T行,如果相交输出"Yes",否则输出"No"。

Input示例

2

1 2 2 1 0 0 2 2

-1 1 1 1 0 0 1 -1

Output示例

Yes

No

//线段相交模板题,学习了判断线段相交后,竟然debug一下午,因为我用的 long long ,

算完叉积后,相乘超过数据范围了,会出现错误,但如果使用的是double,超数据范围也不会改变符号,所以答案依旧是对的。。。

几何问题,这篇博客不错,几何

double:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define eps 1e-8
 5 #define MOD 1000000007
 6 #define MX 1005
 7 /*
 8 判断P1P2跨立Q1Q2的依据是:
 9 ( P1 - Q1 ) × ( Q2 - Q1 ) * ( Q2 - Q1 ) × ( P2 - Q1 ) ≥ 0
10 判断Q1Q2跨立P1P2的依据是:
11 ( Q1 - P1 ) × ( P2 - P1 ) * ( P2 - P1 ) × ( Q2 - P1 ) ≥ 0
12 */
13 struct Point
14 {
15     double x,y;
16     Point(){}
17     Point(double _x,double _y){x=_x;y=_y;}
18     Point operator - (Point b)const{
19         return Point(x-b.x,y-b.y);
20     }
21     double operator ^ (Point b)const{
22         return x*b.y-b.x*y;
23     }
24 }pt[5];
25
26 LL cha(int p1,int p2,int q1,int q2)
27 {
28     Point sa, sb, sc;
29     sa = pt[p1]-pt[q1];
30     sb = pt[q2]-pt[q1];
31     sc = pt[p2]-pt[q1];
32     if ((sa^sb)*(sb^sc)<0) return 0;
33     return 1;
34 }
35
36 int main()
37 {
38     int T;
39     scanf("%d",&T);
40     while (T--)
41     {
42         for (int i=1;i<=4;i++)
43             scanf("%lf%lf",&pt[i].x,&pt[i].y);
44         if (cha(1,2,3,4)&&cha(3,4,1,2))
45             printf("Yes\n");
46         else
47             printf("No\n");
48     }
49     return 0;
50 }

long long:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define MOD 1000000007
 5 #define MX 1005
 6 struct Point
 7 {
 8     LL x,y;
 9     Point(){}
10     Point(LL _x,LL _y){x=_x;y=_y;}
11     Point operator - (Point b)const{
12         return Point(x-b.x,y-b.y);
13     }
14     LL operator ^ (Point b)const{
15         return x*b.y-b.x*y;
16     }
17 }pt[5];
18
19 LL cha(int p1,int p2,int q1,int q2)
20 {
21     Point sa, sb, sc;
22     sa = pt[p1]-pt[q1];
23     sb = pt[q2]-pt[q1];
24     sc = pt[p2]-pt[q1];
25     LL c1 = sa^sb;
26     LL c2 = sb^sc;
27     if (c1>0&&c2>0) return 1;
28     if (c1<0&&c2<0) return 1;
29     if (c1==0||c2==0) return 1;
30     return 0;
31 }
32
33 int main()
34 {
35     int T;
36     scanf("%d",&T);
37     while (T--)
38     {
39         for (int i=1;i<=4;i++)
40             scanf("%lld%lld",&pt[i].x,&pt[i].y);
41         if (cha(1,2,3,4)&&cha(3,4,1,2))
42             printf("Yes\n");
43         else
44             printf("No\n");
45     }
46     return 0;
47 }

时间: 2024-07-28 18:28:49

1264 线段相交的相关文章

51nod 1264 线段相交(判线段相交 包括端点和部分重合)

1264 线段相交 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出平面上两条线段的两个端点,判断这两条线段是否相交(有一个公共点或有部分重合认为相交). 如果相交,输出"Yes",否则输出"No". Input 第1行:一个数T,表示输入的测试数量(1 <= T <= 1000) 第2 - T + 1行:每行8个数,x1,y1,x2,y2,x3,y3,x4,y4.(-10^8 <= xi, yi 

51nod 1264 线段相交(几何)

题目链接:51nod 1264 线段相交 如果两条线段相交,则需满足一条线段的一个端点在另一条线段上,或者 两条线段都分别跨越另一条线段延伸的直线上.(如果点p1位于直线p3p4的一边,而点p2位于该直线的另一边,则称p1p2跨越了这条直线p3p4.) 可以用叉乘来判断p3p1.p3p2是否在p3p4的不同方向(顺.逆时针)(线段p1p2跨越了直线p3p4)以及p1p4.p1p3是否在p1p2的不同方向(线段p3p4跨越了直线p1p2)来判断是否相交. 关于叉乘方向的判断,来,伸出你的右手...

判断线段相交 -- 51nod 1264 线段相交

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1264 三角形的有向面积:a.x*b.y+b.x*c.y+c.x*a.y - a.x*c.y - c.x*b.y - b.x*a.y; 上面得到的即是以点A,B,C三点组成的三角形面积的两倍. 如果area >0 则点A,B,C呈逆时针排列. 如果area<0  则点A,B,C呈顺时针排列. 如果area=0  则点A,B,C三点共线. 那么判断线段1(两个端点poin

51nod 1264 线段相交

题意:求两线段是否相交 使用叉乘与向量的方法 数论资料 :http://dev.gameres.com/Program/Abstract/Geometry.htm# ( P1 - Q1 ) × ( Q2 - Q1 ) * ( Q2 - Q1 ) × ( P2 - Q1 ) >= 0 ( Q1 - P1 ) × ( P2 - P1 ) * ( P2 - P1 ) × ( Q2 - P1 ) >= 0 其中P1P2是一条线段的两端点,Q1Q2是另一个线段的两段点. ( P1 - Q1 ) × (

(计算几何 线段判交) 51nod1264 线段相交

1264 线段相交 给出平面上两条线段的两个端点,判断这两条线段是否相交(有一个公共点或有部分重合认为相交). 如果相交,输出"Yes",否则输出"No". 输入 第1行:一个数T,表示输入的测试数量(1 <= T <= 1000) 第2 - T + 1行:每行8个数,x1,y1,x2,y2,x3,y3,x4,y4.(-10^8 <= xi, yi <= 10^8) (直线1的两个端点为x1,y1 | x2, y2,直线2的两个端点为x3,y

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