POJ3304 Segments(直线与线段相交)

题目链接:

  http://poj.org/problem?id=3304

题目描述:

Segments
 

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 x1 y1 x2 y2 follow, in which (x1y1) and (x2y2) 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!

题目大意:

  给出一些线段,问是否存在直线,使得他们在直线上的投影共点。

思路:

  题意即为问是否存在一条直线与所有线段都相交

  假设存在这样一条直线,保持它与所有线段均相交的性质进行运动,那么我们先平移它,直到不能再平移(这时因为到达了某个线段的端点),我们在绕这个端点旋转,直到不能旋转(到达了另一个线段的端点)

  所以说如果存在这样一条直线,必存在一条直线过其中某两个线段的某两个端点

  所以枚举线段端点(注意两个线段的端点有四种组合情况)组成的直线,然否判断是否与所有线段均相交

 

  判断线段(两端点为a1, a2)与直线(A+t*v)相交,用( (a1 - A) × v) * ( (a2 - A) × v ) <= 0

  

  注意:精度1e-8

     枚举端点时若重合则跳过

     n <= 2 时必成立

     端点在直线上也是算作线段与直线相交

 

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 const int N = 110;
 9 
10 const double EPS = 1e-10;    //精度系数
11 
12 struct Point {
13     double x, y;
14     Point(double x = 0, double y = 0) :x(x), y(y) {}
15 };    //点的定义
16 
17 typedef Point Vector;    //向量的定义
18 
19 double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }    //向量叉乘
20 
21 int dcmp(double x) {
22     if (fabs(x) < EPS)return 0; else return x < 0 ? -1 : 1;
23 }    //与0的关系
24 
25 double Dot(Vector& A, Vector& B) { return A.x*B.x + A.y*B.y; }    //向量点乘
26 double Length(Vector A) { return sqrt(Dot(A, A)); }    //向量长度
27 
28 Vector operator - (Vector& A, Vector& B) { return Vector(A.x - B.x, A.y - B.y); }    //向量减法
29 
30 bool SegmentIntersectWithLine(Point& a1, Point& a2, Point& st, Vector v) {
31     if (dcmp(Length(st - a1)) == 0 || dcmp(Length(st - a2)) == 0)return true;
32     double c1 = Cross(a1 - st, v), c2 = Cross(a2 - st, v);
33     return dcmp(c1)*dcmp(c2) <= 0;
34 }    //判断线段是否与直线相交(包括端点)
35 
36 int n;
37 Point p1[N], p2[N];
38 
39 bool act(int k, int i, int j) {
40     Point st; Vector v;
41     if (k == 0)    st = p1[i], v = p1[j] - p1[i];
42     else if (k == 1)st = p1[i], v = p2[j] - p1[i];
43     else if (k == 2)st = p2[i], v = p1[j] - p2[i];
44     else st = p2[i], v = p2[j] - p2[i];
45     if (dcmp(Length(v)) == 0)return false;    //两个端点重合,不考虑
46     for (int u = 0; u < n; ++u)
47         if (!SegmentIntersectWithLine(p1[u], p2[u], st, v))return false;
48     return true;    //与每个线段均相交
49 }
50 
51 bool ok() {
52     for (int i = 0; i < n - 1; ++i)
53         for (int j = i + 1; j < n; ++j)
54             for (int k = 0; k < 4; ++k)
55                 if (act(k, i, j))return true;    //两条线段端点两两组合有四种情况
56     return false;
57 }
58 
59 int main() {
60     int q;
61     cin >> q;
62     while (q--) {
63         scanf("%d", &n);
64         for (int i = 0; i < n; ++i)
65             scanf("%lf%lf%lf%lf", &p1[i].x, &p1[i].y, &p2[i].x, &p2[i].y);
66         if (n == 1 || n == 2) { printf("Yes!\n"); continue; }    // n<=2 必然存在
67         cout << (ok() ? "Yes!" : "No!") << endl;
68     }
69 }

 

转载于:https://www.cnblogs.com/hyp1231/p/6991452.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值