POJ 1039 Pipe 计算几何题目。。

 http://poj.org/problem?id=1039


叉乘真的是个好东西,把许多原来很难用计算机实验的情况编程数字的相乘,加,减,就连除几乎都没有了。。。

这题作为模板吧,因为我刚刚学计算几何,刚刚遇到这个题目的时候只是想到用叉乘,还有直线和线段相交的一些思想。。但是我不知道怎么样字寻找那些直线,因为在几何里面,两个点之间可以有无数个点,就是说,直线会有无数种情况,上网搜了一下才知道,原来只要求考虑没两个拐点所构成的直线的情况,因为那样肯定就是符合题目条件的情况,(想想应该就是这样的。。)

这道题就当作我求直线和线段相交的模板吧。。。

Pipe
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5744 Accepted: 1727

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.

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <vector>
#define INF 999999999.0
using namespace std;

struct Point {
    double x,y;
};

double tmX;
int bent;

int dep(double num)     //控制精确度。。
{
    if(fabs(num)<1e-11) return 0;
    return (num>0)?1:-1;
}

double Area(Point p0,Point p1,Point p2)  //叉乘。。
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

bool intersect(Point A,Point B,Point C,Point D)   //判断AB这条直线是否和CD线段相交,若相交,求出交点的横坐标。。
{
    double s1,s2;
    int d1,d2;
    s1=Area(A,B,C);
    s2=Area(A,B,D);
    d1=dep(s1);   
    d2=dep(s2);
    if(d1*d2<0)   //符号不同就是相交。。
    {
        tmX=(C.x*s2-D.x*s1)/(s2-s1);    //这个一直没有弄明白,估计是推出来的一个公式吧。希望大牛看到指教指教,这是求交点的横坐标。
        return true;
    }
    if(d1*d2==0)  return true;  //经过拐点也成立。
    return false;
}

int main()
{
    int i;
    while(scanf("%d",&bent),bent)
    {
        vector<Point> up(bent);
        vector<Point> down(bent);
        for( i=0;i<bent;i++)
        {
            scanf("%lf%lf",&up[i].x,&up[i].y);
            down[i]=up[i];
            down[i].y--;
        }
        double TALL = -INF;
        tmX=-INF;
        for(i=0;i<bent;i++)
        {
            for(int j=0;j<bent;j++)
            {
                int k=0;
                while(k<bent)
                {
                    if(!intersect(up[i],down[j],up[k],down[k]))  break;   //当没有与边相交就break。
                    k++;
                }
                if(k==bent)
                    TALL=up[bent-1].x;    
                else if(k>max(i,j))
                {
                    intersect(up[i],down[j],up[k-1],up[k]);  //判断是否与上壁相交。
                    if(tmX>TALL)  TALL=tmX;
                    intersect(up[i],down[j],down[k-1],down[k]);//判断是否与下壁相交。
                    if(tmX>TALL) TALL=tmX;
                }
            }
        }
        if(TALL==up[bent-1].x)
              printf("Through all the pipe.\n");
         else   printf("%.2lf\n",TALL);
    }
    return 0;
}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值