TCG2061 Computational Geometry Lecture Notes Trimester I, Session 2002/2003 Original notes by Dr. R. Mukundan Modified by Dr. Wayne Brown (Trimester I, Session 2000/2001) & Mr. Wong Ya Ping (Trimester I, Session 2002/2003) Faculty of Information Technology, Multimedia University, Jalan Multimedia, 63100 Cyberjaya, Malaysia TCG2061 Computational Geometry page 2 Section 1. Introduction 1.1 Definitions Computational geometry is the design and analysis of efficient algorithms to solve geometric problems. These notes cover only simple geometrical objects, e.g., points, lines, triangles, and polygons. Most problems will be restricted to 2 dimensions, but, in some cases, extensions to 3 dimensions will be discussed. Some application areas of computational geometry include the following: computer graphics pattern recognition robotics statistics data base searching geographical information systems game design computer aided design; electrical, mechanical, etc. Some example problems in computational geometry include the following: Given a finite set of points in a plane, find a convex polygon with the smallest area that includes all of the points. Possible application: GIS system – determine the smallest area that includes all of the cell phone transmitters within a specified geographic region. Given a finite set of line segments in the plane, find and report all pair-wise intersections among the line segments. Possible application: Error checking of electrical, circuit board design. Given a simple polygon P and a point q inside P, find the region of P visible from q. Possible application: Determine all reachable areas of a microwave transmitter from a specified location. References: 1. Mark de Berg, Marc van Kreveld, Mark Overmars, and Otfried Schwarzkopf, Computational Geometry – Algorithms and Applications, Springer-Verlag (1997). TCG2061 Computational Geometry page 3 2. Michael J. Laszlo, Computational Geometry and Computer Graphics in C++, Prentice Hall (1996). 3. Joseph O’Rourke, Computational Geometry in C, Cambridge Univ. Press (1998). 1.2 Common Notations and Equations a. Points 2D: an ordered pair (x,y). 3D: an ordered triplet (x,y,z). Points are typically designated using single, non-bolded letters; for example, p, q, and r. The individual components of a point are designated using appropriate subscripts; for example p = (px,py). b. Vectors Vectors are typically designated using single bolded letters; for example, p, q, and r. Defined by a single point: A directed line segment beginning at the origin (0,0) and ending at point p. Length of vector: p px p y 2 2 Direction (angle counterclockwise from positive x axis): tan 1 For a 3D vector, its length of is: p px p y pz 2 2 py px 2 Defined by two points: A directed line segment beginning at p = (px,py) and ending at point q = (qx,qy). Length of vector: pq (q x p x ) 2 (q y p y ) 2 Direction (angle counterclockwise from positive x axis): tan 1 2 (q y p y ) (q x p x ) For a 3D vector, its length of is: pq (q x p x ) 2 (q y p y ) (q z p z ) A unit vector is a vector of length 1. 2 TCG2061 Computational Geometry page 4 The letter i is used to designate a unit vector parallel to the x axis. The letter j is used to designate a unit vector parallel to the y axis. The letter k is used to designate a unit vector parallel to the z axis. Equivalent vectors Two vectors p and q are equal if they have the same length and direction. That is: p q and p q . Cross product (vector product) of two vectors - Produces a new vector with the following properties: 1) Its direction is normal to (at a right angle to) both of the original vectors 2) Its length is equal to the sine of the angle between two the original two vectors times the magnitude of both original vectors. - For 2D vectors, calculate as: pxq = (pxqy – pyqx)k (a vector parallel to the z axis) - For 3D vectors, calculate as: pxq = ((pyqz – pzqy), (pzqx – pxqz), (pxqy – pyqx)) - Given two vectors p and q, the angle from p to q in an anti-clockwise direction is: pxq sin p q or sin 1 ( pxq p q ) Note: Since the arc sine is not unique in the range –180 to +180, this does not specify the exact angle. Dot product (scalar product): Cosine of the angle between two vectors. - Produces a scalar value equal to the cosine of the angel between the two vectors times the magnitude of the two vectors. - Given two vectors p and q, the angle from p to q in an anti-clockwise direction is: p.q = (pxqx + pyqy) = cos p q or cos 1 ( pq ) p q - for a 3D vector TCG2061 Computational Geometry page 5 p.q = (pxqx + pyqy+ pzqz) Note: Since the arc cosine is not unique in the range –180 to +180, this does not specify the exact angle. Unique angle between two vectors (in the range –180 to +180) - for 2D vectors in the xy plane, this can be calculated as tan 1 ( px q y p y qx px qx p y q y ) Note: To calculate this in software, use the atan2(y,x) function which uses the sign of the y and x parameters to determine the correct quadrant for the angle. - for 3D vector : sin pxq p q cos pq p q Therefore : tan pxq sin cos pq Normalized vector (a vector of length 1.0) (a unit vector) Dividing each component by its length will normalize any vector. p( px p y , ) p p Given a unit vector, each component is referred to a direction cosine of the vector. c. 2D Line Equations Two-point form (assume that the two points (x1,y1) and (x2,y2) are known). x x1 y y1 x 2 x1 y 2 y1 Linear form (General equation of a line). Ax + By + C = 0 Note: A, B and C are not unique for a given line. For example 3x + 4y + 5 = 0 is the same line as 6x + 8y + 10 = 0. TCG2061 Computational Geometry page 6 Normal form Y x cos a + y sin a = p, where p is the length and a is the angle of a vector perpendicular to the line from the origin Note: a and p are unique for a given line. Slope of a line (change in y divided by the change in x) m y 2 y1 A x 2 x1 B Parallel lines (lines with equivalent slope) The line Ax + By + C = 0 and the line Ax + By = 0 are parallel. The line Ax + By + C = 0 is parallel to the vector p = (B, -A) Lines L1 and L2 are parallel if A1 A2 B1 B 2 or A1B2 = A2B1 Perpendicular lines; “normal to”; “at right angles to” Two lines L1 and L2 are normal if their slopes have the property m1 = 1 m2 A vector p, normal to the line Ax + By + C = 0, is defined by p = (A,B) d. 3D Line Equation Given two points (x1,y1,z1),and (x2,y2,z2). x x1 y y1 z z1 x2 x1 y 2 y1 z 2 z1 e. 3D Plane Equation General equation of a plane Ax + By +Cz + D = 0 Note: a vector normal to the surface of the plane at any point is (A,B,C). Given three points (x1,y1,z1), (x2,y2,z2), and (x3,y3,z3) p a X TCG2061 Computational Geometry x y z x1 y1 z1 1 x2 y2 z2 1 x3 y3 z3 1 page 7 1 0 Note: Expand this determinate to calculate the coefficients (A,B,C,D) of the plane. f. Polygons A polygon is a series of straight line segments, such that the endpoint of the last line segment is identical to the beginning point of the first line segment. Each line segment is called an edge. Simple polygon: no edges intersect (non-intersecting polygon) Interior of a polygon: the area enclosed by its edges. Adjacent edges: two edges with a common vertex. Neighbors: two vertices that have a common edge. Convex vertex: the interior angle at the vertex is less than or equal to 180 degrees. Reflex vertex: the interior angle at the vertex is greater than 180 degrees. Diagonal: a line segment between two, non-neighbor edge vertices. Chord: a diagonal that lies totally within the interior of the polygon. g. Triangles A polygon with exactly 3 edges p Area: given vectors p and q as two adjacent edges, Area = (pxq) q 2 1.3 Time and Space Complexity Most nontrivial problems can be solved by more than one method or technique. It is often helpful to characterize a method’s use of resources in terms of both space (memory) and time (CPU cycles). Algorithms are often classified using the following general growth rate functions. These classifications characterize how the resources needed to solve a problem grow as the size of the problem grows. The letter N is traditionally used to designate the size of the input data set. 1 Constant The resource requirements are the same, regardless of the size of the input data. TCG2061 Computational Geometry Log N Logarithmic page 8 Resource requirements grow very slowly, even as the size of the input data grows very large. N Linear Resource requirements grow at the same rate as the input data. NLog N N Log N Resource requirements grow only slightly faster than the growth in size of the input data. N2 Quadratic Resource requirements grow very quickly as the size of the data grows. The solution to large problems is not practical. N3 Cubic Resource requirements grow very quickly as the size of the data grows. The solution to large problems is not practical. 2N Exponential Resource requirements grow extremely quickly as the size of the data grows. The solution to even relatively small problems is not practical. The following chart gives some representative values for each growth rate function. if N = 2 16 256 1024 1048576 1 1 1 1 1 1 Log N 1 4 8 10 20 N 2 16 256 1,024 1,048,576 N Log N 2 64 2,048 10,240 20,971,520 N2 4 256 65,536 1,048,576 1,099,511,627,776 3 8 4,096 16,777,216 1,262,485,504 1.152e+18 2N 4 65,536 1.85e+78 overflow overflow N All of the algorithms studied in this course will be classified according to these growth rate functions. Because most algorithms perform differently on different data sets, the best classification that can be made is an upper bound. That is, an algorithm can be classified according to its worst case behavior on any data set it might process. This classification is known as its ‘big-O’ classification, or O(f(N)). If an algorithm has a classification of O(NLogN), then its need for resources (in terms of memory or CPU cycles) is guaranteed not to grow faster than NLogN as N grows large. It is sometimes possible to determine the least Processing Time upper bound O(f(N)) time for a particular data set amount of resources an algorithm will need for lower bound (g(N))) N Data set size TCG2061 Computational Geometry page 9 any data set it might process. This lower bound on resources is specified using the notation (f(N)). ( is the Greek letter, capital Omega.) In some rare cases, an algorithm’s resources will grow at exactly the same rate for all data sets it might process. In these rare cases, the algorithm is classified using the notation (f(N)). ( is the Greek letter, capital Omicron.) 1.4 Data Structures for Computational Geometry Problems The organization of a data set for computational geometry problems often has a large impact on the efficiency of an algorithm. Common data structures used to store data sets for computational geometry problems include arrays, linked lists (both singly and doubly linked) and binary trees. Each data structure has advantages and disadvantages based on the processing needs of a particular algorithm. Some very broad classifications of these various data structures are listed below. Array Insert into a sorted list – O(N) Delete from a sorted list – O(N) Search – O(logN); binary search Linked lists Insert into a sorted list – O(1) Delete from a sorted list – O(1) Search – O(N); linear search Doubly linked lists Insert into a sorted list – O(1) Delete from a sorted list – O(1) Search – O(N); linear search Binary trees Insert into a sorted binary search tree – O(1) Delete from a sorted binary search tree – O(1) Search – O(logN) One goal of computational geometry is the selection of appropriate data structures that minimize the execution time need to solve a geometric problem. 1.5 Summary and Conclusions Problem solving in computational geometry involves: TCG2061 Computational Geometry page 10 Understanding the geometric properties of the problem, including the analytical equations of the geometric objects and their mathematical characteristics. Proper application of algorithmic techniques and data structures, including selection of appropriate data structures and algorithms that maximize accuracy and minimize execution time.