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 防止 E-mail 注入( PHP 过滤器)

    PHP 防止 E-mail 注入 防止 e-mail 注入的最好方法是对输入进行验证. 下面的代码与上一节类似,不过我们已经增加了检测表单中 email 字段的输入验证程序: <html> ...

  2. python-3高级特征

    1-切片 L = ["qinzb",'fengyong','bingyan'] L[0:3] #截取从索引0开始到索引3结束(不包含索引3) L[:3] #如果索引是从0开始则可省 ...

  3. java线程安全总结 - 1 (转载)

    原文地址:http://www.jameswxx.com/java/java%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E6%80%BB%E7%BB%93/ 最近想将ja ...

  4. SpringMVC---web.xml配置详解

    web.xml中需要配置的内容 1.配置监听器<listener> 它有两个监听器: 1). <!--配置文件加载监听器--> <listener> <lis ...

  5. android MotionEvent

    getAction() 获取事件的类型,这是一个组合值,由pointer的index值和事件类型值组合而成的 getActionMasked() 获取事件的类型,不具有其他信息 参考: http:// ...

  6. 关于我的Android 博客

    我是曹新雨,我为自己代言.现在的菜鸟,3年以后我就是大神.为自己加油.微信:aycaoxinyu 关于我的Android博客,都是我当初遇到困难,克服之后,写上去的.后来,有人加我微信,问我一些问题, ...

  7. python学习笔记一:数据类型

    一.Python文件类型 1.源代码 hello.py: 1 #!/usr/bin/python 2 print "hello world" 2.字节代码:python源文件经编译 ...

  8. iOS-QQ好友列表实现

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

  9. SpringMVC 集成 Velocity 模板引擎

    本文通过 maven 项目中集成 1.引入 SpringMVC 与 Velocity 需要的依赖 <!-- SpringMVC --> <dependency> <gro ...

  10. .net 匿名方法

    匿名方法 核心就是lambda语法,下面是使用举例: var conn= MySqlHelper.GetConn(); var list=conn.Query<User>("SE ...