码迷,mamicode.com
首页 > 其他好文 > 详细

poj1039 Pipe【计算几何】

时间:2018-10-02 20:11:05      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:ini   nes   written   star   search   pes   code   ==   oge   

含【求直线交点】、【判断直线与线段相交】模板
 
Pipe
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions:11940   Accepted: 3730

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.

Source

 

题意:

给n个点 构成两条平行的折线

问从管道口出发的光线最远能到达的横坐标

思路:

最远的光线一定是贴着管道的某两个端点走的

现在枚举这两个端点  判断其与后面折线的交点

刚开始没想到判断交点时 可以先判断line 和 line(up[k], down[k])

这样得到的k就是最小的不能达到的k

用这个k就可以拿来算line 和 line(up[k-1], up[k])以及line(down[k -1], down[k])的交点了

  1 //#include <bits/stdc++.h>
  2 #include<iostream>
  3 #include<cmath>
  4 #include<algorithm>
  5 #include<stdio.h>
  6 #include<cstring>
  7 
  8 using namespace std;
  9 typedef long long int LL;
 10 
 11 #define zero(x) (((x) > 0? (x) : -(x)) < eps)
 12 const double eps = 1e-8;
 13 int sgn(double x)
 14 {
 15     if(fabs(x) < eps) return 0;
 16     if(x < 0) return -1;
 17     else return 1;
 18 }
 19 
 20 struct point{
 21     double x, y;
 22     point(){}
 23     point(double _x, double _y)
 24     {
 25         x = _x;
 26         y = _y;
 27     }
 28     point operator -(const point &b)const
 29     {
 30         return point(x - b.x, y - b.y);
 31     }
 32     double operator ^(const point &b)const
 33     {
 34         return x * b.y - y * b.x;
 35     }
 36     double operator *(const point &b)const
 37     {
 38         return x * b.x + y * b.y;
 39     }
 40     void input()
 41     {
 42         scanf("%lf%lf", &x, &y);
 43     }
 44 };
 45 
 46 struct line{
 47     point s, e;
 48     line(){}
 49     line(point _s, point _e)
 50     {
 51         s = _s;
 52         e = _e;
 53     }
 54     //0表示直线重合,1表示平行,2相交
 55     pair<int, point>operator &(const line &b)const
 56     {
 57         point res = s;
 58         if(sgn((s - e) ^ (b.s - b.e)) == 0){
 59             if(sgn((s - b.e) ^ (b.s - b.e)) == 0){
 60                 return make_pair(0, res);
 61             }
 62             else return make_pair(1, res);
 63         }
 64         double t = ((s - b.s) ^ (b.s - b.e)) / ((s - e) ^ (b.s - b.e));
 65         res.x += (e.x - s.x) * t;
 66         res.y += (e.y - s.y) * t;
 67         return make_pair(2, res);
 68     }
 69 };
 70 
 71 //判断直线与线段相交
 72 bool seg_inter_line(line l1, line l2)
 73 {
 74     return sgn((l2.s - l1.e) ^ (l1.s - l1.e)) * sgn((l2.e - l1.e) ^ (l1.s - l1.e)) <= 0;
 75 }
 76 
 77 int n;
 78 point up[100], down[100];
 79 int main()
 80 {
 81     while(scanf("%d", &n) != EOF && n != 0){
 82         for(int i = 0; i < n; i++){
 83             up[i].input();
 84             down[i].x = up[i].x;
 85             down[i].y = up[i].y - 1.0;
 86         }
 87 
 88 
 89         bool flag = false;
 90         double ans = -100000000.0;
 91         int k;
 92         for(int i = 0; i < n; i++){
 93             for(int j = i + 1; j < n; j++){
 94                 for(k = 0; k < n; k++){
 95                     if(seg_inter_line(line(up[i], down[j]), line(up[k], down[k])) == 0){
 96                         break;
 97                     }
 98                 }
 99                 if(k >= n){
100                     flag = true;
101                     break;
102                 }
103                 if(k > max(i, j)){
104                     if(seg_inter_line(line(up[i], down[j]), line(up[k - 1], up[k]))){
105                         pair<int, point>pr = line(up[i], down[j]) & line(up[k - 1], up[k]);
106                         point p = pr.second;
107                         ans = max(ans, p.x);
108                     }
109                     if(seg_inter_line(line(up[i], down[j]), line(down[k - 1], down[k]))){
110                         pair<int, point>pr = line(up[i], down[j]) & line(down[k - 1], down[k]);
111                         point p = pr.second;
112                         ans = max(ans, p.x);
113                     }
114                 }
115 
116                 for(k = 0; k < n; k++){
117                     if(seg_inter_line(line(down[i], up[j]), line(up[k], down[k])) == 0){
118                         break;
119                     }
120                 }
121                 if(k >= n){
122                     flag = true;
123                     break;
124                 }
125                 if(k > max(i, j)){
126                     if(seg_inter_line(line(down[i], up[j]), line(up[k - 1], up[k]))){
127                         pair<int, point>pr = line(down[i], up[j]) & line(up[k - 1], up[k]);
128                         point p = pr.second;
129                         ans = max(ans, p.x);
130                     }
131                     if(seg_inter_line(line(down[i], up[j]), line(down[k - 1], down[k]))){
132                         pair<int, point>pr = line(down[i], up[j]) & line(down[k - 1], down[k]);
133                         point p = pr.second;
134                         ans = max(ans, p.x);
135                     }
136                 }
137             }
138             if(flag){
139                 break;
140             }
141         }
142         //cout<<ans<<endl;
143         if(flag){
144             printf("Through all the pipe.\n");
145         }
146         else{
147             printf("%.2f\n", ans);
148         }
149     }
150     return 0;
151 }

 

poj1039 Pipe【计算几何】

标签:ini   nes   written   star   search   pes   code   ==   oge   

原文地址:https://www.cnblogs.com/wyboooo/p/9737596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!