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 ) × ( Q2 - Q1 )代表X1*Y2 + X3*Y1 + X2*Y3 - X3*Y2 - X1*Y3 - X2*Y1,

AC代码:

#include<bits/stdc++.h>
using namespace std;
int t;
struct node
{
double x;
double y;
}ma[9];
double xx(node a,node b,node c)
{
return a.x*b.y+b.x*c.y+a.y*c.x-a.x*c.y-c.x*b.y-b.x*a.y;
}
int main()
{
cin>>t;
while(t--)
{
for(int i=1;i<=4;i++)
{
cin>>ma[i].x>>ma[i].y;
}
double x1=xx(ma[1],ma[3],ma[4]);
double x2=xx(ma[4],ma[3],ma[2]);
double x3=xx(ma[3],ma[1],ma[2]);
double x4=xx(ma[2],ma[1],ma[4]);
if(x1*x2>=0&&x3*x4>=0)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}

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

51nod 1264 线段相交的相关文章

51nod 1264 线段相交(几何)

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

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 线段相交

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

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

(计算几何 线段判交) 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