[CG]Intersection of Line Segments(0163)(计算几何,求线段是否相交)
2017-08-16 19:25
302 查看
Giving 2 line segments on a plane, decide whether they intersect or not. Pay attention, endpoint contacts should be considered intersection.
Description
The input starts with a line containing a single integer T, the number of test cases. Each test case consists of 2 lines, with each describing a line segment. For each line, there're 4 integers within [-100, 100]. The first two denote one endpoint and the last
two denote the other. Points are represented by x and y coordinates.
Input
If the 2 line segments given intersect, display "True", otherwise display "False".
Output
Sample Input
Sample Output
"Cross Product" is essential in "Computational Geometry". One of the many uses of cross product is to decide the relative position of geometric objects, such as points and line segments. This problem can also be solved efficiently and easily using cross product
with no precision loss(no division operation is used).
/*
*
题意:
给你两个点,代表一条线段,再给你另外两个点代表另外一条线段
问你这两条线段是否相交
题解:
用矢量叉积求解。什么?
你不知道什么是矢量叉积,去看我的上一篇博客吧。
*/
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
struct node
{
int x, y;
};
int dig(node x0, node x1, node x2)
{
return (x2.x - x0.x)*(x1.y - x0.y) - (x1.x - x0.x)*(x2.y - x0.y);
}
int main()
{
int t;
node a1, a2, b1, b2;
cin >> t;
while (t--)
{
cin >> a1.x >> a1.y >> a2.x >> a2.y;
cin >> b1.x >> b1.y >> b2.x >> b2.y;
if ((b1.x >= min(a1.x, a2.x) && b1.x <= max(a1.x, a2.x) && b1.y >= min(a1.y, a2.y) && b1.y <= max(a1.y, a2.y))
|| (b2.x >= min(a1.x, a2.x) && b2.x <= max(a1.x, a2.x) && b2.y >= min(a1.y, a2.y) && b2.y <= max(a1.y, a2.y)))
{
if (dig(a1, a2, b1)*dig(a1, a2, b2) <= 0)
{
cout << "True" << endl;
}
else
{
cout << "False" << endl;
}
}
else
{
cout << "False" << endl;
}
}
return 0;
}
Description
The input starts with a line containing a single integer T, the number of test cases. Each test case consists of 2 lines, with each describing a line segment. For each line, there're 4 integers within [-100, 100]. The first two denote one endpoint and the last
two denote the other. Points are represented by x and y coordinates.
Input
If the 2 line segments given intersect, display "True", otherwise display "False".
Output
1 2 3 | 1 0 0 1 0 1 0 1 1 |
1 | True |
"Cross Product" is essential in "Computational Geometry". One of the many uses of cross product is to decide the relative position of geometric objects, such as points and line segments. This problem can also be solved efficiently and easily using cross product
with no precision loss(no division operation is used).
/*
*
题意:
给你两个点,代表一条线段,再给你另外两个点代表另外一条线段
问你这两条线段是否相交
题解:
用矢量叉积求解。什么?
你不知道什么是矢量叉积,去看我的上一篇博客吧。
*/
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
struct node
{
int x, y;
};
int dig(node x0, node x1, node x2)
{
return (x2.x - x0.x)*(x1.y - x0.y) - (x1.x - x0.x)*(x2.y - x0.y);
}
int main()
{
int t;
node a1, a2, b1, b2;
cin >> t;
while (t--)
{
cin >> a1.x >> a1.y >> a2.x >> a2.y;
cin >> b1.x >> b1.y >> b2.x >> b2.y;
if ((b1.x >= min(a1.x, a2.x) && b1.x <= max(a1.x, a2.x) && b1.y >= min(a1.y, a2.y) && b1.y <= max(a1.y, a2.y))
|| (b2.x >= min(a1.x, a2.x) && b2.x <= max(a1.x, a2.x) && b2.y >= min(a1.y, a2.y) && b2.y <= max(a1.y, a2.y)))
{
if (dig(a1, a2, b1)*dig(a1, a2, b2) <= 0)
{
cout << "True" << endl;
}
else
{
cout << "False" << endl;
}
}
else
{
cout << "False" << endl;
}
}
return 0;
}
相关文章推荐
- POJ 3304:Segments 计算几何 是否有直线与所有线段相交
- POJ 3304 Segments (计算几何、判断直线与线段是否相交)
- 计算几何-判断两线段是否相交(模板)
- POJ 3304 Segments(计算几何 判断直线与线段相交)
- 判断两条线段是否相交 计算几何
- 计算几何--判断线段是否相交
- 计算几何之判断两线段是否相交
- POJ 3304 Segments <计算几何(直线与线段相交判断)>
- POJ 3304 Segments(计算几何:直线与线段相交)
- POJ 1410 Intersection(计算几何---线段相交--跨立试验)
- COJ 1645计算几何:判断线段是否相交
- Educational Codeforces Round 2D. Area of Two Circles' Intersection(计算几何+圆相交的面积)
- POJ 3304 Segments [枚举+叉乘判断线段相交]【计算几何】
- 2017 ACM-ICPC乌鲁木齐网络赛 B. Out-out-control cars【计算几何||判断射线与线段是否相交】
- pku 1556 The Doors 计算几何 之 叉积判断线段是否相交
- 判断线段与圆是否相交(计算几何)
- POJ 1410 Intersection <计算几何(线段相交判断)>
- POJ3304---Segments (基础计算几何:叉积判断线段相交)
- POJ 3304 Segments(计算几何:直线与线段相交)
- POJ1410_Intersection(几何/线段是否相交/模板)