poj 3304 (计算机图论_直线和线段相交)
2014-08-19 09:59
405 查看
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 on it, all projected segments have at least one point in common.
Input
Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing a positive integer n≤ 100 showing the number of segments. After that, n lines containing
four real numbers x1y1x2y2 follow, in which (x1, y1) and (x2, y2) are the coordinates of the two
endpoints for one of the segments.
Output
For each test case, your program must output "Yes!", if a line with desired property exists and must output "No!" otherwise. You must assume that two floating point numbers a and b are equal if |a - b| < 10-8.
Sample Input
Sample Output
这个题目是目的是找一条直线是否能和所有的线段相交,可以证明如果存在那么必有一条过所有线段的端点的其中两个,可以通过判断直线与线段的相交关系来判断是否存在这一条直线;注意,用到判断两个double之间的差值大小,
代码:
Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments on it, all projected segments have at least one point in common.
Input
Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing a positive integer n≤ 100 showing the number of segments. After that, n lines containing
four real numbers x1y1x2y2 follow, in which (x1, y1) and (x2, y2) are the coordinates of the two
endpoints for one of the segments.
Output
For each test case, your program must output "Yes!", if a line with desired property exists and must output "No!" otherwise. You must assume that two floating point numbers a and b are equal if |a - b| < 10-8.
Sample Input
3 2 1.0 2.0 3.0 4.0 4.0 5.0 6.0 7.0 3 0.0 0.0 0.0 1.0 0.0 1.0 0.0 2.0 1.0 1.0 2.0 1.0 3 0.0 0.0 0.0 1.0 0.0 2.0 0.0 3.0 1.0 1.0 2.0 1.0
Sample Output
Yes! Yes! No!
这个题目是目的是找一条直线是否能和所有的线段相交,可以证明如果存在那么必有一条过所有线段的端点的其中两个,可以通过判断直线与线段的相交关系来判断是否存在这一条直线;注意,用到判断两个double之间的差值大小,
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #define MAX 105 #define INF 1e-8 using namespace std; const double PI = acos(-1.0); struct point { double x; double y; }p[MAX*2]; int num; double cross ( point& a, point& b, point& c) { return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x); } int sig( double d)/// 判断两个double之间的差值; { return ( d > INF)-(d < -INF); } bool linecross( point& a, point& b, point& c, point& d) { int d1, d2; d1 = sig(cross(a,b,c)); d2 = sig(cross(a,b,d)); if ((d1^d2) == -2 )///两条直线规范相交,(d1^d2) == -2表示d1,d2异号; return 1; if (d1 == 0||d2 == 0)///表示两条直线不规范相交; return 1; return 0; } bool Test(point& a, point& b) { if ( sig(a.x-b.x)==0&&sig(a.y-b.y)==0 )///如果两个点离得很近就看做两个点重合,看做一个点; return false; for ( int i = 1; i <= 2*num; i += 2) { if (!linecross(a,b,p[i],p[i+1]))///判断a,b,点组成的这条线是否能和所有的线段相交 return false; } return true; } bool Find () { for ( int i = 1; i <= 2*num; i++) { for ( int j = i+1; j <= 2*num; j++) { if ( Test(p[i],p[j])) return true; } } return false; } int main() { //freopen("in.txt","r",stdin); int test; cin >> test; while (test--) { cin >> num; for ( int i = 1; i <= num*2; i++) { scanf("%lf %lf",&p[i].x,&p[i].y); } if (Find()) printf("Yes!\n"); else printf("No!\n"); } }
相关文章推荐
- POJ 3304 判断线段和直线相交
- poj 3304 直线与线段相交
- POJ 3304 Segments (计算几何、判断直线与线段是否相交)
- poj 3304 Segments(叉积+直线和线段相交判断)
- POJ 3304 Segments <计算几何(直线与线段相交判断)>
- POJ 3304(线段与直线相交)[改]
- POJ 3304 Segments (直线和线段相交判断)
- poj 3304 Segments 线段与直线相交的判断
- POJ 3304 Segments 判断直线和线段相交
- POJ 3304 segments 线段和直线相交
- POJ 3304 || Segments (线段,直线相交
- POJ 3304(计算几何初步——判断线段和直线相交,加上枚举)
- poj 3304 Segments 线段与直线相交
- POJ 3304 Segments(计算几何:直线与线段相交)
- POJ 3304 计算几何 直线与线段相交
- POJ 3304 Segment 直线与线段相交
- poj 3304 Segments 线段与直线相交
- poj 3304 Segments(线段与直线相交)
- POJ 3304 Segments (直线与线段是否相交)
- POJ3304 Segments 【线段直线相交】