F - Pipe

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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.
刘汝佳黑书《算法艺术与信息学艺术》第三章 计算几何初步 的例2  P359
题意:给出一个曲折的管道,求出光线能够到达的管道的最远点的横坐标。
题解: 对于任意一条光线,如果它没有和任意一个拐点相交,那么就可以通过平移使得光线变长,
   如果和一个拐点相交,而不和另一个相交,就可以通过旋转使得它更长。所以最优解的光线一定是与两个拐点相交的一条线。
   那么我们就每次枚举两个管道折点,求一直线看该直线与管道边缘线段交点。在求的过程中,可以从左到右依次判断
   是否与各个折点的横断面相交,然后可以得知是否会与管道壁相交。
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
struct point{
double x,y;
}p[],q[];
const double eps=1e-; //精度问题
/* 叉积 以c为原点
若返回值大于0,则cb在ca的逆时针方向
若返回值小于0,则cb在射线ca顺时针方向;
若返回值等于0,则ca和cb共线,方向可能相同,也可能相反*/
double cross(point a,point b,point c)
{
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
double Across(point a,point b,point c,point d) //非规范相交也包含在内
{
double tmp=cross(c,b,a)*cross(d,b,a);
if(tmp<||fabs(tmp)<eps) return true;
return false;
}
//根据ab两点求AB的一般式a1x+b1y+c1=0 cd两点同理
double getIntersect(point a,point b,point c,point d)
{
//直线ab
double a1=b.y-a.y;
double b1=a.x-b.x;
double c1=a.y*b.x-a.x*b.y; //x2y1-x1y2
//直线cd
double a2=d.y-c.y;
double b2=c.x-d.x;
double c2=d.x*c.y-c.x*d.y; //x2y1-x1y2
//ab和cd交点的横坐标
double x=(b1*c2-c1*b2)/(b2*a1-b1*a2);
return x;
}
int main()
{
int k,n,th=;
double best;
while(scanf("%d",&n)&&n) //scanf比cin快很多很多
{
for(int i=;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y); //scanf
q[i].x=p[i].x;
q[i].y=p[i].y-; //根据题意 横坐标相同 纵坐标-1
}
best=p[].x;
th=;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
if(i==j)
continue;
for(k=;k<n;k++)
{
if(!Across(p[i],q[j],p[k],q[k])) //不与横截面相交
break;
}
//注意以下内容不是k循环里面的
if(k==n) th=; //k=n说明through了
else if (k>max(i,j)) //与两个管道壁相交的横坐标最大值
best=max(best,max(getIntersect(p[i], q[j], p[k-], p[k]),getIntersect(p[i], q[j], q[k-], q[k])));
}
}
if(th)
puts("Through all the pipe.");//puts比printf更快
else
printf("%.2f\n",best);
}
return ;
}
如果与所有横截面都相交,说明through了。

poj1039 Pipe(计算几何叉积求交点)的更多相关文章

  1. poj 1654 Area(计算几何--叉积求多边形面积)

    一个简单的用叉积求任意多边形面积的题,并不难,但我却错了很多次,double的数据应该是要转化为long long,我转成了int...这里为了节省内存尽量不开数组,直接计算,我MLE了一发...,最 ...

  2. 【BZOJ1132】【POI2008】Tro 计算几何 叉积求面积

    链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...

  3. POJ 1039 Pipe(直线和线段相交判断,求交点)

    Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8280   Accepted: 2483 Description ...

  4. hdu 2528:Area(计算几何,求线段与直线交点 + 求多边形面积)

    Area Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. 两条线段求交点+叉积求面积 poj 1408

    题目链接:https://vjudge.net/problem/POJ-1408 题目是叫我们求出所有四边形里最大的那个的面积. 思路:因为这里只给了我们正方形四条边上的点,所以我们要先计算横竖线段两 ...

  6. hdu 1174:爆头(计算几何,三维叉积求点到线的距离)

    爆头 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

  7. poj 1269 Intersecting Lines——叉积求直线交点坐标

    题目:http://poj.org/problem?id=1269 相关知识: 叉积求面积:https://www.cnblogs.com/xiexinxinlove/p/3708147.html什么 ...

  8. poj1269 (叉积求直线的交点)

    题目链接:https://vjudge.net/problem/POJ-1269 题意:给出4个顶点,表示两条直线,求这两条直线的相交情况,重合输出LINE,平行输出NONE,相交于一点输出该点的距离 ...

  9. hdu 2105:The Center of Gravity(计算几何,求三角形重心)

    The Center of Gravity Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

随机推荐

  1. PHP下载远程文件到指定目录

    PHP用curl可以轻松实现下载远程文件到指定目录: <?php class Download { public static function get($url, $file) { retur ...

  2. php扩展开发-全局变量

    //php_myext.hZEND_BEGIN_MODULE_GLOBALS(myext) unsigned long counter;//在这里定义需要的全局变量,可以多个,每个变量一行, ZEND ...

  3. NOIP PJ游记

    Day -1 感觉自信满满,一等奖应该稳了,毕竟初一时我这么菜都拿了二等奖,然后就睡觉了... Day 1 在大巴上玩元气骑士可开心了,车上欢欢喜喜,到了考场,一眼看题,以为很简单. T1硬模拟... ...

  4. Dialog BLE 学习之 修改分散加载文件 (2)

    最近搞Dialog的BLE SDK,发现空间不够了,询问原厂,得知可以通过调整分散加载文件而增加空间,一方面是有42KB+8KB的硬件基础,另一方面是原有的程序限制为38KB+8KB.故顺便学习了下把 ...

  5. Sqoop 工具使用

    Sqoop 是什么及安装 Hadoop sqoop Apache sqoop (SQL to Hadoop) Sqoop is a tool designed to transfer data bet ...

  6. 20145202 《Java程序设计》第四周学习总结

    继承:打破了封装性 extends 1.提高了代码的复用性. 2.让类与类之间产生了关系,有了这个关系,才有了多态的特性. 3.必须是类与类之间有所属类关系才可以继承. 4.java只支持单继承不支持 ...

  7. linux压缩和解压缩命令大全--费元星站长

    tar命令 解包:tar zxvf FileName.tar 打包:tar czvf FileName.tar DirName gz命令 解压1:gunzip FileName.gz 解压2:gzip ...

  8. iOS-QQ好友列表实现

    0.QQ好友列表实现 0.首先说说实现思路 自定义UITableView,每一个分组都是一个UITableViewHeaderFooterView,然后自定义cell,这里分组的实现主要是自定义UIT ...

  9. 每天一个Linux命令(10):mv命令

    mv命令用来对文件或目录重新命名,或者将文件从一个目录移到另一个目录中.source表示源文件或目录,target表示目标文件或目录.如果将一个文件移到一个已经存在的目标文件中,则目标文件的内容将被覆 ...

  10. Eclipse 出现“polling news feeds”的解决办法

    小编突然心血来潮,安装了一下Java的环境,eclipse的IDE来写点Java,但是是不是出现以下的弹窗,实在是闹心,后来网上看了前辈们的解决办法,特此记录一下.如有侵权,敬请告知!!! 1. 找到 ...