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. C#简单的文件阅读器

    写一个简单的文件阅读器  1.可以读取大文件(2G)2.实现首页.下一页.前一页.末页的跳转3.实现到指定页面的跳转,比如跳转到第**页4.限制每页显示字符数 1029-4069byte,且用户可自定 ...

  2. C语言指针篇(二)多级指针

        多级指针         多级指针常常使用于数组.这里仅仅介绍一下它长什么样,后文会再次详细对比使用.         多级指针呢,常见的有二级指针.见图.             二级指针的 ...

  3. 最小生成数kruskal算法和prim算法

    定义 连通图:在无向图中,若任意两个顶点vivi与vjvj都有路径相通,则称该无向图为连通图. 强连通图:在有向图中,若任意两个顶点vivi与vjvj都有路径相通,则称该有向图为强连通图. 连通网:在 ...

  4. ArrayList & Vector的源码实现

    #ArrayList & Vector #####前言: 本来按照计划,ArrayList和Vector是分开讲的,但是当我阅读了ArrayList和Vector的源码以后,我就改变了注意,把 ...

  5. TouTiao开源项目 分析笔记15 新闻详情之两种类型的实现

    1.预览效果 1.1.首先看一下需要实现的效果. 第一种,文字类型新闻. 第二种,图片类型新闻. 1.2.在NewsArticleTextViewBinder中设置了点击事件 RxView.click ...

  6. python面向对象的约束和自定义异常

    基于人为来约束: 即人为主动抛出异常 class BaseMessage(object): def send(self,x1): """ 必须继承BaseMessage, ...

  7. 使用CSS3制作各种形状

    CSS3的一个非常酷的特性是允许我们创建各种规则和不规则形状的图形,从而可以减少图片的使用.以前只能在Photoshop等图像编辑软件中制作的复杂图形现在使用CSS3就可以完成了.通过使用新的CSS属 ...

  8. 读json文件发生错误,所遇到的坑

    当我们生产者生产json 文件的时候   消费时用JSON读文件时,如下: val values = kafkardd.map(t=>JSON.parseObject(t._2)) 如果发生以下 ...

  9. PaaS服务之路漫谈(一)

    此文已由作者尧飘海授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. PaaS服务之路漫谈(一) 1983年,SUN公司提出的网络即计算的理念:2006年亚马逊(Amazon)推 ...

  10. Javascript 属性高级写法

    http://www.cnblogs.com/YuanSong/p/3899287.html