《usaco计算几何》word版.doc_第1页
《usaco计算几何》word版.doc_第2页
《usaco计算几何》word版.doc_第3页
《usaco计算几何》word版.doc_第4页
《usaco计算几何》word版.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

usaco 计算几何usaco计算几何2010-05-10 09:09Computational Geometry Prerequisites Graph TheoryShortest PathTools This module discusses several algorithms that calculate various geometric properties,mostly based on only two operations described below:cross product and arctangent.Cross Product The cross product of uand vis written as ux v.Computationally,the cross product of two three-dimensional vectors uand vis the vector determinant of the following matrix(where i,j,and kare unit vectors in the x,y,and zdirections respectively):|i jk|ux uy uz|vx vy vz|That equation works out to:(uyvz-vyuz)i+(uzvx-uxvz)j+(uxvy-uyvx)k This definition can be used for vectors in two dimensions by using three-dimensional vectors with az component of 0.The resulting vector will only have az value.The cross product has three properties:The cross product of two vectors is perpendicular to both vectors.The length of the cross product is equal to the product of:the length of u,the length of v,andthe sine of the angle between the vectors.Of the two different directions that are perpendicular to both uand v,the direction the cross product points depends on whether uisto the rightof vorto the left.Dot product The dot product of two vectors uand vis ascalar written as uv.Computationally,it is defined in three dimensions as:uxvx+u yvy+uzv zThe dot product is actually equal to the product of:the length of uthe length of vthe cosine of the angle between uand v.Presuming uand vare non-zero,if the dot product if negative,u and vmake an angle greater than 90 degrees.If it is zero,then uand vare perpendicular.If ucdot vis positive,then the two vectors form an acute angle.Arctangent The arctangentfunction calculates the(an)angle whose tangent is its argument and generally returns areal number between-pi/2 and pi/2.An additional function in C,atan2,takes two arguments:a DELTA yvalue and aDELTA xvalue(in that order!).It determines the angle between the given vector and the positive xaxis and returns avalue between-pi and pi.This has the advantage of removing concerns about dividing by zero or writing code to repair angles in order to handle the negative xcases.The atan2 function is almost always easier to use than the simpler atan function that takes only one argument.Particular Debugging Problems The main problem with geometric problems is that they spawn alot of special cases.Be on the lookout for these special cases and make sure your program works for all of them.Floating point calculations also create anew set of problems.Floating point calculations are rarely precise,as the computer only maintains so many bits(digits)of accuracy:be aware of this.In particular,when checking if two values are equal,check to see if they are within some small tolerance of each other not precisely equal.Geometric Algorithms Here are some of snippets that can help you solve geometry problems.Area of Triangle To cal culate the area of atriangle with vertices(a,b,c),pick avertex(say a)and create avector to the other two vertices(let u=b-a,and v=c-a).The area of the triangle(a,b,c)is one half the length of cross product ux v.An alternative method to find the area of triangle is to use Heros formula.If the lengths of the sides of atriangle are a,b,and c,let s=(a+b+c)/2.The area of the triangle is then sqrt(s*(s-a)*(s-b)*(s-c).Are Two Line Segments Parallel?To check if two line segments are parallel,create vectors along each line segment and check to see if their cross product is(almost)zero.Area of polygon The area of apolygon with vertices(x 1,y 1),.,(x n,y n)is equal to the determinant:1|x1 x2.xn|-|2|y1 y2.yn|where the determinate is defined to be similar to the 2by 2determinant:x1 y2+x2y3+.+xn y1-y1 x2-y2x3-.-yn x1 Distance from apoint to aline The distance from apoint Pto aline AB is given by the magnitude of the cross product.In particular,d(P,AB)=|(P-A)x(B-A)|/|B-A|.To determine the distance from apoint Pto the plane defined by A,B,and C,let n=(B-A)x(C-A).The distance is then give by the following equation:d(P,ABC)=(P-A)n/|n|.Points on aline Apoint is on aline if the distance from the point to the line is 0.Points on the same side of line This notion only makes sense for two dimensions.To check if points Cand Dare on the same side of line AB,calculate the zcomponent of(B-A)x(C-A)and(B-A)x(D-A).If the zcomponents have the same sign(i.e.,their product is positive),then Cand Dare on the same side of the line AB.Point on line segment To calculate if apoint Cis on the line segment AB,check if Cis on the line AB.If it is,then check if the length of AB is equal to the sum of the lengths of AC and CB.Point in triangle To check if apoint Ais in atriangle,find another point Bwhich is within the triangle(the average of the three vertices works well).Then,check if the point Ais on the same side of the three lines defined by the edges of the triangle as B.Point in convex polygon The same trick works for aconvex polygon:Four(or more)points are coplanar To determine if acollection of points is coplanar,select three points,A,B,and C.Now,if,for any other point D,(B-A)x(C-A)(D-A)=0,then the collection of points resides in some plane.Two lines intersect Two lines intersect if and only if they are not parallel in two dimensions.In three dimensions,two lines AB and CD intersect if they are not parallel and A,B,C,and Dare coplanar.Two line segments intersect In two dimensions,two line segments AB and CD intersect if and only if Aand Bare on opposite sides of the line CD and Cand Dare on opposite sides of line AB.Note that both of the checks are necessary,as for the last case one of the checks returns true,while the other testifies to the fact that AB and CD do not intersect.In three dimensions,solve following system of equations,where iand jare the unknowns:Ax+(Bx-Ax)i=Cx+(Dx-Cx)j Ay+(By-Ay)i=Cy+(Dy-Cy)j Az+(Bz-Az)i=Cz+(Dz-Cz)j If this system has asolution(i,j),where 0=i=1 and 0=j=1,then the line segments intersect at:(Ax+(Bx-Ax)i,Ay+(By-Ay)i,Az+(Bz-Az)i.Point of Intersection of Two Lines For the lines AB and CD in two dimensions,the most straight-forward way to calculate the intersection of them is to solve the system of two equations and two unknowns:Ax+(Bx-Ax)i=Cx+(Dx-Cx)j Ay+(By-Ay)i=Cy+(Dy-Cy)jThe point of intersection is:(Ax+(Bx-Ax)i,Ay+(By-Ay)i)In three dimensions,solve the same system of equations as was used to check line intersection,and the point of intersection is:(Ax+(Bx-Ax)i,Ay+(By-Ay)i,Az+(Bz-Az)i)Checking convexity of 2-dimensional polygon To check the convexity of a2-dimensional polygon,walk the polygon in clock-wise order.For each triplet of consecutive points(A,B,C),calculate the cross product(B-A)x(C-A).If the zcomponent of each of these vectors is positive,the polygon is convex.Point in non-convex polygon To calculate if apoint is within anonconvex polygon,make aray from that point in arandom direction and count the number of times it intersects the polygon.If the ray intersects the polygon at avertex or along an edge,pick anew direction.Otherwise,the point is within the polygon if and only if th eray intersects the polygon an odd number of times.This method also extends to three dimensions(and higher),but the restriction on intersection is that it only intersects at faces and not at either avertex or an edge.Geometry Methodologies Geometric problems introduce several different tricks that can be used to either reduce the run-time or approximate the solution.Monte Carlo The first geometric trick is based on randomness.Instead of calculating the probability that something occurs,simulate arandom event and calculate the fraction of times it occurs.If enough events are simulated,the difference between these two values becomes very small.This can be helpful to determine something like the area of afigure.Instead of calculating the area directly,determine abounding box,and throwdartsat the box,and estimate what the probability of hitting the figure is.If this is calculated accurately enough,this can give agood estimate of the actual area.The problem with this method is to get agood relative error(error divided by the actual value)requires alarge number of successful events.If the probability of the event occurring is very small,the method does not yield good results.Partitioning Partitioning is amethod to improve the speed of ageometric algorithm.This entails dividing the plane up into sections(usually by agrid but sometimes into radial sections or some other method),and bucketing the objects into appropriate section(s).When looking for objects within some figure,only those sections which have anon-zero intersection with that figure need to be examined,thereby greatly reducing the cost of the algorithm.This is helpful to determine the set of objects within some distance of agiven point(the figure is acircle)or to check for intersections(the figure is aline).Graph Problems Sometimes what may look like ageometric problem is really agraph problem.Just because the input is points in the plane does not mean its ageometric algorithm.Example Problems Poi nt Moving Given aset of line segments in the plane,and two points Aand B,is it possible to move from Ato Bwithout crossing any of the segments?The line segments partition the plane into regions.Determine these regions,and see if Aand Breside in the same region.Bicycle Routing Given acollection of non-intersecting buildings along with start and end locations,find the shortest path from Ato Bthat doesnt go through any buildings.Analysis:This is really agraph problem.The nodes are the start and end locations,along with the vertices of the buildings.There are edges between any two nodes such that the line segment between them does not intersect any buildings,with weight equal to the length of the length of the line segments.Once that graph has been calculated,the problem is shortest path.Maximizing Line Intersections Given acollection of segments in the plane,find the greatest number of segments which can by intersected by drawing asingle line.Analysis:With alittle bit of thought,it is clear that the line segment must pass through two of the vertices of the collection of line segments.Thus,try all pairs of vertices,and cal culate the crossing for each.Combining this with partitioning gives an algorithm that runs fairly quickly.Polygon Classification Given acollection of segments defining apolygon,determine if it is simple(no two non-consecutive line segments intersect)and convex.计算机应用中的解析几何译by zhougu学习该内容的基础*图论问题*最短路径问题工具这个模块论述了一些具有几何特性的多方面做计划的算法,而这些算法大多都建立在以下的两个概念的基础上:向量乘积和反正切。向量的乘积:向量u和v的乘积被记做u xv。而对于计算机程序来说,两个三维空间内的向量u和的积是决定于下面的这个矩阵。(i,j,k是单位向量,而x,y,z分别是它们的方向)|i jk|ux uy uz|vx vy vz|可以通过这个方程计算出:(uyvz-vyu z)i+(uzvx-u xvz)j+(uxvy-uvx)k这个方法完全可以被用在二维的空间内,那时我们认为z=0,自然结果向量中的z也为0。向量的乘法有三个性质:*两个向量的积在空间范围内同时垂直于这两个向量。*向量u和v的积的长度等于向量u的长度、向量v的长度和两向量夹角的正弦值的乘积。*在两个可能出现的方向中,向量u和向量v的积的方向取决于u和v的位置关系。向量的数量积:向量u和向量v的数量积记做u?v,用与刚才类似的方程表示为:u xvx+u yvy+u zvz而事实上我们可以用标量u的长度、v标量的长度和两向量夹角的余弦值的乘积来表示向量u和向量v的数量积。如果向量u和向量v的夹角大于90度,而u和v都是非0的,那么它们的数量积是负数。如果它们的数量积等于0,那么它们是互相垂直的。如果u?v的结果是正数,那么它们夹的是一个锐角。反正切:反正切作为函数通常可以通过正切值计算出一个处在-Pi/2到Pi/2之间的角的度数。C语言中额外的atan2函数带有两个引数:DELTA y的值和DELTA x的值。它会测定所给向量与x轴的夹角并返回一个处在-Pi/2到Pi/2之间的角度。它的好处就在于消去了涉及到0做被除数和为处理多种角度情况时所写的代码。大多时候,函数atan2要比只带有一个引数的函数atan简单。特殊的调试问题:解决几何问题时最主要是问题是它们往往有很多特殊情况,所以要确认你的程序可以应付所有的特殊情况。浮点数据的计算总会产生一系列的问题。浮点数据的计算很少是100%精确的,因为电脑本身也只能维持一定位数的准确度。尤其是判断两个值是否相等时,电脑只是看它们的差是否在一个很小的容差值的范围内。几何算法:以下是几个可以帮助我们解决几何问题的资料。三角形区域:计算一个顶点为(a,b,c)的三角形所在的区域,要先选择一个顶点(比如顶点a),并创建两个与另外三角形的三边有如下关系的向量:使向量u=b-a,向量v=c-a。三角形a、b、c的区域就是向量u和向量v的乘积的1.5倍。还有一个求三角形区域可以选择的方法是用hero定理。如果三角形三边的长度分别为a、b、c,设s=(a+b+c)/2,这个三角形的区域就等于:sqrt(s*(s-a)*(s-b)*(s-c)两条线段是否平行:检验两条线段是否平行,可以沿着两条线段的方向创建两个向量,然后看它们的乘积是否为0。多边形区域:一个顶点为(x 1,y 1),.,(x n,y n)的多边形的区域等于下面这个行列式。1|x1 x2.xn|-|2|y1 y2.yn|这个行列阵的算法类似于2 X2的行列阵:x1 y2+x2y3+.+xn y1-y1 x2-y2x3-.-yn x1。点到直线的距离:值得注意的是点P到直线AB的距离往往是由向量乘积量得出的:d(P,AB)=|(P-A)x(B-A)|/|B-A|。要决定点P到A、B、C三点所在的平面的距离,需先设n=(B-A)x(C-A),然后用这个公式来计算:d(P,ABC)=(P-A)?n/|n|。在直线上的点:在直线上的点到直线的距离为0。在直线同侧的点:这个想法只对二维的空间有意义。要检验点C和点D是否在直线AB同侧,只需计算(B-A)x(C-A)和(B-A)x(D-A)的值。如果它们同号,C和D就在直线AB的同侧。三角形内的点:检验点A是否在三角形内部,我们可以先在三角形内部找一个点B(三个顶点的平均值即可)。然后检验A相对三边是否都在B的同侧。凸多边形内的点:这和三角形是同样的做法。多个(四个以上)点共面:要判断一系列点是否共面,我们可以先选择三个点A、B、C,它们必然是共面的,在任意挑选一点D,如果(B-A)x(C-A)?(D-A)=0,则这个点与点A、B、C在一个平面内。两线段相交:在二维空间内,两条线段AB和CD相交,如果A、B分别在线段CD的两侧而且C、D分别在线段AB的两侧。把这两个核对全记录下来往往是不必要的,如果其中一个的核对证明AB和CD是不相交的就返回false并结束核对。在三维空间内,利用以下的等式(i,j不是已知的):Ax+(Bx-Ax)i=Cx+(Dx-Cx)j Ay+(By-Ay)i=Cy+(Dy-Cy)j Az+(Bz-Az)i=Cz+(Dz-Cz)j如果已知了(i,j),且i、j在0和1之间,那么两线段相交于(Ax+(Bx-Ax)i,Ay+(By-Ay)i,Az+(Bz-Az)i。两条直线交点:要计算二维空间内的两直线AB、CD的交点,最容易

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论