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 continue和break的用法(深入理解)

    对于刚入门的PHP童鞋们来说,在循环体中的continue和break的作用总是分不清到底是什么意思, 怎么用, 两者到底有什么区别? 接下来说几个例子,其实它们是很好区分的. <?php $t ...

  2. Java NIO 教程

    Java NIO(New IO)是从Java 1.4版本开始引入的一个新的IO API,可以替代标准的Java IO API.本系列教程将有助于你学习和理解Java NIO. Java NIO提供了与 ...

  3. strak组件(10):批量操作

    效果图: 批量删除只是一个例子,可以根据需求定制自己想要的批量操作. 新增函数 def get_action_list(self) 钩子方法,获取要处理的批量操作的函数 def action_mult ...

  4. 适配IE8+等浏览器的适配播放插件

    function myBrowser(){ var userAgent = navigator.userAgent; //ȡ���������userAgent�ַ� var isOpera = us ...

  5. python,多线程

    多线程编程,模型复杂,容易发生冲突,必须用锁加以隔离,同时,又要小心死锁的发生. Python解释器由于设计时有GIL全局锁,导致了多线程无法利用多核.多线程的并发在Python中就是一个美丽的梦. ...

  6. spark练习--统计xxx大学的各个少数名族的情况

    最近,有一份数据,是关于学校的数据,这个里面有所有学生的信息,今天闲来没事,我就想用spark的方式来读取文件,并且来统计这个学校的各个民族的情况,以前我用hadoop中mapReduce来计算,不得 ...

  7. 阿里巴巴Java开发规约Eclipse插件安装及使用

    技术交流群:233513714 插件安装 环境:JDK1.8,Eclipse4+.有同学遇到过这样的情况,安装插件重启后,发现没有对应的菜单项,从日志上也看不到相关的异常信息,最后把JDK从1.6升级 ...

  8. mybatis异常:There is no getter for property named 'xxx' in 'xxx'

    在使用mybatis查询的时候出现了下面的异常: org.apache.ibatis.reflection.ReflectionException: There is no getter for pr ...

  9. Android学习记录(3)—Android中ContentProvider的基本原理学习总结

    一.ContentProvider简介        当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据.虽然使用其他方法也可以对外共享数据 ...

  10. 打开Vim/Vi代码高亮

    由于新装Vim/Vi 默认是没有打开代码高亮配置的,就看到有朋友一次次到网上去找各种配置.其实Vim默认带来配置文件的样本的,只需拷贝过来就可使用. 在用户根目录(~)中新建vim的配置文件 .vim ...