29
Geometry CSE 232, Fall 2015 Shane Carr

Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code Teams usually elect

Embed Size (px)

Citation preview

Page 1: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

GeometryCSE 232, Fall 2015Shane Carr

Page 2: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Geometry questions are difficult.

Lots of Edge Cases

Floating Point Errors

Tedious Code

Teams usually elect to save the geometry question for near the end because of these issues.

How should we approach geometry questions to maximize our changes for success?

Page 3: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Practical Tips If you have a choice, use a scientific

computing language to solve geometry questions, like Python or Octave/MATLAB. Less prone to floating point errors.

Use library functions as much as possible.

Consider alternative approaches to the problem, especially “binary search the answer.”

Page 4: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

More Practical TipsBe aware of degrees vs. radians. Computers

usually assume you want radians.

Geometry calculations can be take a lot of CPU cycles, so if you have to run a lot of them in a loop and your code gets slow, see if there’s a way to cache the results.

Be aware of vertical lines and division-by-zero errors. Halim recommends storing line equations in standard form, with b=0.0 for vertical and b=1.0 for all others. If dealing with line segments, store the two endpoints rather than the slope between them.

Page 5: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

“Easy” Operations for Geometry Problems

Page 6: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Operations with PointsHalim Section 7.2.1

Compute the distance between two points.

Rotate a point around another point (usually the origin) by a certain number of degrees.

Reflect a point about another point or a line.

Page 7: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Operations with LinesHalim Section 7.2.2

Are two lines parallel?

If two lines are not parallel, What is their point of

intersection? What is the angle

between them?

What is the distance from a point to a line?

A note about line segments: most of the same formulas can be used, but beware of edge cases!!!

Page 8: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Operations with AnglesHalim Section 7.2.2

Given three points a, o, and b, what is the angle aob?

Given a line segment and a point, is the point on the “left side” or “right side” of the line segment (or is it colinear)? This operation, also

known as the “CCW Test”, turns out to have a lot of applications.

Page 9: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Operations with CirclesHalim Section 7.2.3

Is a given point inside, outside, or on the circumference of the circle?

Compute a chord, arc, or tangent line.

Compute the area of a circle or circle segment.

Given two points, find the center of the corresponding circle. (There are two possibilities for any two different points.)

Page 10: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Operations with SpheresHalim Section 9.11

Given two points on the surface of a sphere, what is the angle between them?

Given two points on the surface of a sphere, what is their shortest distance along the surface? (Great Circle Distance)

Page 11: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Operations with TrianglesHalim Section 7.2.4

Given the base and height, what is the area?

Given the length of the three sides, what is the area? (Heron’s Formula)

Law of Sines, Law of Cosines, SohCahToa, etc.

What is the radius and center of a triangle’s incircle and circumcircle?

Page 12: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Operations with QuadrilateralsHalim Section 7.2.5

Calculate the area of a: Square or Rectangle Trapezoid (requires the

height of the trapezoid and the lengths of the two non-parallel edges)

Parallelogram (special case of trapezoid)

Rhumbus (special case of parallelogram)

Kite (requires the lengths of the two diagonals)

If you have an unusual quadrilateral, use the general polygon area formula instead.

Page 13: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Operations with PolygonsHalim Sections 7.3.1-7.3.6

What is the area of the polygon? (There is a 6-line solution that always works.)

Is the polygon convex? (Involves the CCW Test)

Given a point, is it inside the polygon? (Involves CCW)

Cut a convex polygon in half by a line.

Note: Polygons are typically represented as a counter-clockwise list of vertices.

Page 14: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Various Rare TheoremsHalim Section 9.8

How do you relate the area of a polygon to the number of integer-valued points enclosed by the polygon? (Closed-form solution: Pick’s Theorem)

Determine the number of pieces into which a circle is divided if n points on its circumference are joined by chords. (Closed-form solution involving the binomial coefficients)

Page 15: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Example Problems

Page 16: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Geometry Example #1Based on ICPC RMRC 2014 Problem G

You have an aquarium with a flat top and bottom. It is a polygon on one side, and it extends away from you with depth d. You fill the aquarium with l liters of water. The aquarium is bolted to the table so that it won’t tip over. What is the height of the water?

Page 17: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Edge Cases What if the polygon is not

convex? (Note: the problem actually guarantees convexity)

What if the polygon has only four vertices?

What if the depth is zero?

What if the height is zero?

What if all the water doesn’t fit inside the aquarium?

Page 18: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

How to Solve: Naïve Approach Split the polygon into a

series of trapezoids. “Fill” each trapezoid from the bottom, until you reach a trapezoid that you can’t completely fill.

Problems How do you construct

the trapezoids? Lots of code.

Prone to floating point errors since you have to perform a lot of division operations.

Page 19: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

How to Solve: Better Approach Guess a height. Draw a

horizontal line through the polygon, and calculate the resulting polygon’s area. Refine your guess via binary search until you converge to the correct height.

Benefits Cutting a polygon by a

line is one of our “easy” operations.

Computing the area of a polygon is also one of our “easy” operations.

Page 20: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Geometry Example #2UVa 535, Globetrotter

As a member of an ACM-ICPC programming team, you'll soon find yourself always traveling around the world: Zürich, Philadelphia, San José, Atlanta. At the contest site it would be interesting to know how many miles you are away from home. For this sake, your job is to write a program to compute the geographical distance between two locations on the Earth's surface, given in latitude and longitude. (Assume the earth is a perfect sphere with radius 6378 km.)

Page 21: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Edge Cases What if the points are the

same?

What if the points are on exactly opposite sides of the sphere?

What if one or both of the points is at a pole?

Page 22: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

How to Solve: Naïve Approach Get out a sheet of paper,

draw some triangles, figure out how to deal with latitude and longitude, and derive the formula to compute the distance between two points along the surface of a sphere.

Problems Prone to human error Derivation can take a

long time

Page 23: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

How to Solve: Better Approach Recognize that this is a

“great circle distance” problem, one of our “easy” operations to code. Look up the code for in a book like CP3 and use it.

Benefits Less time wasted doing

complicated derivations Steven and Felix have

already checked for edge cases and made sure the code is bug-free

Page 24: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Computational Geometry

Page 25: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

What’s the difference?With classical geometry problems, you can

usually use well-documented formulas and right triangles to derive a closed-form solution. The pain is then how to get it working in code.

With computational geometry, the core problem is a nontrivial algorithm, and the geometry is a less important sub-problem.

Page 26: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Convex HullHalim Section 7.3.7

Given a set of points, what is the smallest convex polygon that encloses all the points?

Can be solved in O(n log n) time using Graham’s Scan.

Page 27: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Sample Problem: Convex HullLoosely based on UVa 218, Moth Eradication

A section of forest has been taken over by an invasive species of moth. You are to install sensors around the perimeter of the forest in order to monitor the influx of the insects. Given a list of coordinates of the trees in the forest, how many sensors do you need to install?

Page 28: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Art GalleryHalim Section 9.2

You are given a polygon representing the walls of an art gallery, and you want to place guards such that every spot in the gallery is protected by a guard. How many guards do you

need? (NP-hard) Is it possible to protect

the gallery with just one guard? (a non-obvious linear time solution exists; see Halim section 9.2)

Page 29: Geometry CSE 232, Fall 2015 Shane Carr. Geometry questions are difficult. Lots of Edge Cases Floating Point Errors Tedious Code  Teams usually elect

Sample Problem: Art GalleryBased on UVa 588, “Video Surveillance”

You are to install a video surveillance system in a new department store. The floor plan forms a rectangular polygon whose edges do not intersect each other and touch each other only at the corners. Can you protect the entire store with just one camera? (The camera can see in all directions.)