GEOMETRY - cs.mcgill.cadbecer/courses/Winter2017/321/Lecture7… · treat the vertical lines...
Transcript of GEOMETRY - cs.mcgill.cadbecer/courses/Winter2017/321/Lecture7… · treat the vertical lines...
GEOMETRY COMP 321 – McGill University These slides are mainly compiled from the following resources.
- Professor Jaehyun Park’ slides CS 97SI - Top-coder tutorials. - Programming Challenges books.
Computational Geometry • Geometry • Cross Product • Convex Hull Problem • Sweep Line Algorithm
Geometry Basics - Line • A Line can be described with mathematical equation: y = mx + c or ax + bx + c = 0. • The y = mx + c equation involves ‘gradient’ / ‘slope’ m.
• Note: Be careful with vertical lines with ‘infinite’ slope. Usually, we treat the vertical lines separately in the solution code (example of the special cases in geometry problems).
• A Line Segment is a line with two end points with finite length.
Geometry Basics - Circles
Geometry Basics - Circles • In a 2-D Cartesian coordinate system, the Circle centered
at (a, b) with radius r is the set of all points (x, y) such that (x − a)2 + (y − b)2 = r2.
• The constant π is the Ratio of any circle’s circumference to its diameter in the Euclidean space. To avoid precision error, the safest value for programming contest is pi = 2 X acos(0.0), unless if this constant is defined in the problem description!
• The Circumference c of a circle with a Diameter d is c = π X d where d = 2 X r.
• The length of an Arc of a circle with a circumference c and an angle α (in degrees) is (α/360.0) x c
Geometry Basics - Circles • The length of a Chord of a circle with a radius r and an
angle α (in degrees) can be obtained with the Law of Cosines: 2r2X(1−cos(α))
• The Area A of a circle with a radius r is A = π X r2
• The area of a Sector of a circle with an area A and an angle α (in degrees) is (α /360.0) X A
• The area of a Segment of a circle can be found by subtracting the area of the corresponding Sector with the area of an Isosceles Triangle with sides: r, r, and Chord-length.
Geometry Basics - Triangles
Geometry Basics - Triangles • A Triangle is a polygon with three vertices and three edges.
• Equilateral Triangle, all three edges have the same length and all inside/interior angles are 60 degrees;
• Isosceles Triangle, two edges have the same length; • Scalene Triangle, no edges have the same length; • Right Triangle, one of its interior angle is 90 degrees (or a right angle).
• The Area A of triangle with base b and height h is A = 0.5 X b X h
• The Perimeter p of a triangle with 3 sides: a, b, and c is p = a + b + c.
• The Heron’s Formula: The area A of a triangle with 3 sides: a, b, c, is A = sqrt(s X (s − a) X (s − b) X (s − c)), where s = 0.5 X p (the Semi-Perimeter of the triangle).
Geometry Basics - Triangles • The radius r of the Triangle’s Inner Circle with area A and
the semi-perimeter s is r = A/s. • The radius R of the Triangle’s Outer Circle with 3 sides: a,
b, c and area A is R =a X b X c/(4 X A). • Also please check
• Law of Cosines. • Law of Sines • Pythagorean Theorem. • Pythagorean Triple.
Geometry Basics - Quadrilateral • It is a polygon with four edges (and four vertices).
Geometry Basics - Rectangles • A Rectangle is a polygon with four edges, four vertices,
and four right angles. • The Area A of a rectangle with width w and height h is A =
w X h. • The Perimeter p of a rectangle with width w and height h
is p = 2 X (w + h). • A Square is a special case of rectangle where w = h.
Geometry Basics - Polygons • A Polygon is a plane figure that is bounded by a closed
path or circuit composed of a finite sequence of straight line segments.
• A polygon is said to be Convex if any line segment drawn inside the polygon does not intersect any edge of the polygon. Otherwise, the polygon is called Concave.
• The area A of an n-sided polygon (either convex or concave) with n pairs of vertex coordinates given in some order (clockwise or counter-clockwise) is:
Geometry Basics - Polygons • Testing if a polygon is convex (or concave) is easy with a
quite robust geometric predicate test called CCW (Counter Clockwise).
• This test takes in 3 points p, q, r in a plane and determine if the sequence p → q → r is a left turn. • Or in other words: p → q → r is counter-clockwise
CCW • We’ll use it all the time
• Applications: • Determining the (signed) area of a triangle • Testing if three points are collinear • Determining the orientation of three points • Testing if two line segments intersect
Counterclockwise Predicate • Define ccw(A,B,C) = (B − A) x (C − A) = (bx − ax)(cy − ay) − (by − ay)(cx − ax).
Segment-Segment Intersection Test • Given two segments AB and CD • Want to determine if they intersect properly: two segments
meet at a single point that are strictly inside both segments
Segment-Segment Intersection Test • Assume that the segments intersect
• From A ’s point of view, looking straight to B , C and D must lie on different sides.
• Holds true for the other segment as well
• The intersection exists and is proper if: • ccw(A,B,C) X ccw(A,B,D) < 0 • and ccw(C,D,A) X ccw(C,D,B) < 0
Non-proper Intersections • We need more special cases to consider!
• e.g., If ccw(A,B,C), ccw(A,B,D), ccw(C,D,A), ccw(C,D,B) are all zeros, then two segments are collinear
• Very careful implementation is required
Convex Hull Problem • Given n points on the plane, find the smallest convex
polygon that contains all the given points • For simplicity, assume that no three points are collinear
Convex Hull Problem: Simple Algorithm • AB is an edge of the convex hull iff ccw(A,B,C) have the
same sign for all other points C • This gives us a simple algorithm
• For each A and B: • If ccw(A,B,C) > 0 for all C ≠ A,B:
• Record the edge A -> B
• Walk along the recorded edges to recover the convex hull
Convex Hull Problem: Graham Scan • We know that the leftmost given point has to be in the
convex hull. • We assume that there is a unique leftmost point
• Make the leftmost point the origin • So that all other points have positive x coordinates
• Sort the points in increasing order of y/x • Increasing order of angle, whatever you like to call it
• Incrementally construct the convex hull using a stack
Convex Hull Problem: Graham Scan • Points are numbered in increasing order of y/x
Convex Hull Problem: Graham Scan • Add the first two points in the chain
Convex Hull Problem: Graham Scan • Adding point 3 causes a concave corner 1-2-3: remove 2
Convex Hull Problem: Graham Scan • That’s better...
Convex Hull Problem: Graham Scan • Adding point 4 to the chain causes a problem: remove 3
Convex Hull Problem: Graham Scan • Continue adding points...
Convex Hull Problem: Graham Scan • Continue adding points...
Convex Hull Problem: Graham Scan • Continue adding points...
Convex Hull Problem: Graham Scan • Bad corner!
Convex Hull Problem: Graham Scan • Bad corner again!
Convex Hull Problem: Graham Scan • Continue adding points...
Convex Hull Problem: Graham Scan • Continue adding points...
Convex Hull Problem: Graham Scan • Continue adding points...
Convex Hull Problem: Graham Scan • Done!
Convex Hull Problem: Graham Scan • Set the leftmost point as (0, 0), and sort the rest of the
points in increasing order of y/x • Initialize stack S • For i = 1, . . . , n:
• Let A be the second topmost element of S, B be the topmost element of S , and C be the i-th point
• If ccw(A,B,C) < 0 , pop S and go back • Push C to S
• Points in S form the convex hull
Sweep Line Algorithm • A problem solving strategy for geometry problems • The main idea is to maintain a line (with some auxiliary
data structure) that sweeps through the entire plane and solve the problem locally
• We can’t simulate a continuous process, (e.g. sweeping a line) so we define events that causes certain changes in our data structure • And process the events in the order of occurrence
• We’ll cover one sweep line algorithm
Sweep Line Algorithm
Sweep Line Algorithm
Sweep Line Algorithm
Sweep Line Algorithm
Sweep Line Algorithm
Sweep Line Algorithm
Sweep Line Algorithm
Sweep Line Algorithm
Sweep Line Algorithm
Sweep Line Algorithm
Sweep Line Algorithm
Sweep Line Algorithm • If the sweep line hits the left edge of a rectangle
• Insert it to the data structure
• Right edge? • Remove it
• Move to the next event, and add the area(s) of the green rectangle(s) • Finding the length of the union of the blue segments is the hardest
step • There is an easy O(n) method for this step
Sweep Line Algorithm • Sweep line algorithm is a generic concept
• Come up with the right set of events and data structures for each problem