http://poj.org/problem?id=1039
叉乘真的是个好东西,把许多原来很难用计算机实验的情况编程数字的相乘,加,减,就连除几乎都没有了。。。
这题作为模板吧,因为我刚刚学计算几何,刚刚遇到这个题目的时候只是想到用叉乘,还有直线和线段相交的一些思想。。但是我不知道怎么样字寻找那些直线,因为在几何里面,两个点之间可以有无数个点,就是说,直线会有无数种情况,上网搜了一下才知道,原来只要求考虑没两个拐点所构成的直线的情况,因为那样肯定就是符合题目条件的情况,(想想应该就是这样的。。)
这道题就当作我求直线和线段相交的模板吧。。。
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 5744 | Accepted: 1727 |
Description
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
Output
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;
}
本文探讨如何利用计算几何解决管道光路计算问题,详细解释了如何通过叉乘等数学概念来确定光线能到达的最大横坐标,以及如何在管道内进行光路追踪。
803

被折叠的 条评论
为什么被折叠?



