POJ1039 Pipe(直线交点、判断直线与线段相交)

题目链接:

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

题目描述:

Pipe
 

Description

The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic light pipeline. During the design phase of the new pipe shape the company ran into the problem of determining how far the light can reach inside each component of the pipe. Note that the material which the pipe is made from is not transparent and not light reflecting. 

Each pipe component consists of many straight pipes connected tightly together. For the programming purposes, the company developed the description of each component as a sequence of points [x1; y1], [x2; y2], . . ., [xn; yn], where x1 < x2 < . . . xn . These are the upper points of the pipe contour. The bottom points of the pipe contour consist of points with y-coordinate decreased by 1. To each upper point [xi; yi] there is a corresponding bottom point [xi; (yi)-1] (see picture above). The company wants to find, for each pipe component, the point with maximal x-coordinate that the light will reach. The light is emitted by a segment source with endpoints [x1; (y1)-1] and [x1; y1] (endpoints are emitting light too). Assume that the light is not bent at the pipe bent points and the bent points do not stop the light beam.

Input

The input file contains several blocks each describing one pipe component. Each block starts with the number of bent points 2 <= n <= 20 on separate line. Each of the next n lines contains a pair of real values xi, yi separated by space. The last block is denoted with n = 0.

Output

The output file contains lines corresponding to blocks in input file. To each block in the input file there is one line in the output file. Each such line contains either a real value, written with precision of two decimal places, or the message Through all the pipe.. The real value is the desired maximal x-coordinate of the point where the light can reach from the source for corresponding pipe component. If this value equals to xn, then the message Through all the pipe. will appear in the output file.

Sample Input

4
0 1
2 2
4 1
6 4
6
0 1
2 -0.6
5 -4.45
7 -5.57
12 -10.8
17 -16.55
0

Sample Output

4.67
Through all the pipe.

 

题目大意:

  给一根曲曲折折的管子,判断是否存在一道光穿透管子,若不能,求最远交点横坐标

思路:

  以不属于同一线段的线段端点两两组成直线

  依次判断直线在每个拐点的纵坐标值是否在上下两个拐点之间

  注意判断时用dcmp处理精度

 

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 using namespace std;
 7 
 8 const double EPS = 1e-10;        //精度系数
 9 const double PI = acos(-1.0);    //π
10 const int INF = 0x3f3f3f3f;
11 const int N = 23;
12 
13 struct Point {
14     double x, y;
15     Point(double x = 0, double y = 0) :x(x), y(y) {}
16     const bool operator < (Point A)const {
17         return x == A.x ? y < A.y : x < A.x;
18     }
19 };    //点的定义
20 
21 typedef Point Vector;    //向量的定义
22 
23 Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }    //向量加法
24 Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }    //向量减法
25 Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }        //向量数乘
26 
27 int dcmp(double x) {
28     if (fabs(x) < EPS)return 0; else return x < 0 ? -1 : 1;
29 }    //与0的关系
30 
31 double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }    //向量点乘
32 double Length(Vector A) { return sqrt(Dot(A, A)); }    //向量长度
33 double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }    //向量叉乘
34 
35 Point LineIntersectionPoint(Point A1, Point B1, Point A2, Point B2) {
36     double t1 = ((A1.y - A2.y)*(B2.x - A2.x) - (A1.x - A2.x)*(B2.y - A2.y)) /
37         ((B1.x - A1.x)*(B2.y - A2.y) - (B1.y - A1.y)*(B2.x - A2.x));
38     return A1 + (B1 - A1)*t1;
39 }    //返回直线交点
40 
41 double LineInterY_axisPoint(double x, Point p, Vector v) {
42     double t = (x - p.x) / v.x;
43     return p.y + t*v.y;
44 }    //直线与x=x交点纵坐标
45 
46 bool SegmentIntersectWithLine(Point& a1, Point& a2, Point& st, Vector v) {
47     double c1 = Cross(a1 - st, v), c2 = Cross(a2 - st, v);
48     return dcmp(c1)*dcmp(c2) < 0;
49 }    //判断线段是否与直线相交(不含端点)
50 
51 double ans;
52 
53 bool ok(int n, Point P[][2], Point p, Vector v) {
54     for (int i = 0; i < n; ++i) {    //枚举管子折点横坐标
55         double y = LineInterY_axisPoint(P[i][0].x, p, v);
56         if (dcmp(y - P[i][0].y) > 0 || dcmp(y - P[i][1].y) < 0) {    //如果位于上下点纵坐标之外
57             if (i)
58                 for (int j = 0; j < 2; ++j)
59                     if (SegmentIntersectWithLine(P[i - 1][j], P[i][j], p, v)) {
60                         Point tmp = LineIntersectionPoint(P[i - 1][j], P[i][j], p, p + v);
61                         ans = max(ans, tmp.x);
62                     }
63             return false;
64         }
65     }
66     return true;
67 }
68 
69 bool solve(int n, Point P[][2]) {
70     for (int i = 0; i < n - 1; ++i)
71         for (int j = i + 1; j < n; ++j)
72             for (int k = 0; k < 2; ++k) {    //枚举直线
73                 Point p = P[i][k];
74                 Vector v = P[j][(1 + k) % 2] - p;
75                 if (ok(n, P, p, v))return true;
76             }
77     return false;
78 }
79 
80 int main() {
81     int n;
82     Point P[N][2];
83     while (cin >> n && n) {
84         ans = -INF;
85         for (int i = 0; i < n; ++i) {
86             scanf("%lf%lf", &P[i][0].x, &P[i][0].y);
87             P[i][1].x = P[i][0].x, P[i][1].y = P[i][0].y - 1;
88         }
89         if (solve(n, P))printf("Through all the pipe.\n");
90         else printf("%.2lf\n", ans);
91     }
92 }

 

Pipe
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10871 Accepted: 3383

Description

The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic light pipeline. During the design phase of the new pipe shape the company ran into the problem of determining how far the light can reach inside each component of the pipe. Note that the material which the pipe is made from is not transparent and not light reflecting. 

Each pipe component consists of many straight pipes connected tightly together. For the programming purposes, the company developed the description of each component as a sequence of points [x1; y1], [x2; y2], . . ., [xn; yn], where x1 < x2 < . . . xn . These are the upper points of the pipe contour. The bottom points of the pipe contour consist of points with y-coordinate decreased by 1. To each upper point [xi; yi] there is a corresponding bottom point [xi; (yi)-1] (see picture above). The company wants to find, for each pipe component, the point with maximal x-coordinate that the light will reach. The light is emitted by a segment source with endpoints [x1; (y1)-1] and [x1; y1] (endpoints are emitting light too). Assume that the light is not bent at the pipe bent points and the bent points do not stop the light beam.

Input

The input file contains several blocks each describing one pipe component. Each block starts with the number of bent points 2 <= n <= 20 on separate line. Each of the next n lines contains a pair of real values xi, yi separated by space. The last block is denoted with n = 0.

Output

The output file contains lines corresponding to blocks in input file. To each block in the input file there is one line in the output file. Each such line contains either a real value, written with precision of two decimal places, or the message Through all the pipe.. The real value is the desired maximal x-coordinate of the point where the light can reach from the source for corresponding pipe component. If this value equals to xn, then the message Through all the pipe. will appear in the output file.

Sample Input

4
0 1
2 2
4 1
6 4
6
0 1
2 -0.6
5 -4.45
7 -5.57
12 -10.8
17 -16.55
0

Sample Output

4.67
Through all the pipe.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值