Post on 26-Sep-2020
1 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Lecture 13 Geometry and
Computational Geometry
Euiseong Seo
(euiseong@skku.edu)
2 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Geometry
a Branch of mathematics concerned with questions of shape, size, relative position of figures, and the properties of space
We know geometry
We don’t know how to represent geometry in programming languages
3 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Lines
Lines are the shortest distance between any two points
Lines are of infinite length in both directions
• Cf. line segments
Line representations
• Two points: (x1, y1) and (x2, y2)
• A single point and a slope: y = mx+b – m = (y1-y2) / (x1-x2)
– what if (x1-x2) = 0?
• General case – ax + by + c = 0
• These representations can be coverted to any other
4 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Lines
General case representation typedef struct {
double a;
double b; // default value is 1
doublec;
} line
• ax + by + c = 0
5 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Line Problems
Find an intersection of two lines
• check if they are parallel
• else find
What else?
• Angles of two lines
• Closest point on a line
• Rays – half line with an origin
6 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Triangles and Trigonometry
Angle measurement • Radians – more general
• Degrees
Terminologies • Right triangle
• Perpendicular lines
• Internal/external angle
• Equilateral triangle
7 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Triangle Area
Signed triangle area
c
a
b
positive
b
a
c
negative
8 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Circles
Representation typedef struct {
point c; /* center */
double r; /* radius */
} circle;
Formula
Problems
• Tangent line
• Intersection points
9 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Faster Than a Speeding Bullet
Superman flies from s to t
He can see through the bullets
He can’t go through the bullets
Find the distance he must travel to reach t
10 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Line Segments
A portion of a line typedef struct {
point p1, p2;
} segment;
Are two segments intersect?
• Deal with degeneracy cases – parallel
– total overlap
11 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Polygons
Closed chains of non-intersecting line segments typedef struct {
int n; /* number of vertices */
point p[MAXPOLY]; /* vertices are ordered? */
} polygon;
Convex polygons
• A polygon P is convex if any line segment defined by two points within P lies entirely within P
• Counter clock wise predicate
12 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Convex Hull
The smallest polygon containing a set of points
Graham scan algorithm
1. Find an extreme point
2. Sort the points in order of increasing angle about the pivot
3. Build the hull by adding edges when we make a left turn, and back-tracking when we make a right turn
13 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Finding Area of a Polygon
Triangulation
• A polygon can be broken down into triangles
• Van Gogh’s Algorithm
Area of a polygon is the sum of the triangles
14 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Jordan Curve Theorem
15 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Lattice Polygon
Pick’s Theorem
16 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Rope Crisis in Ropeland!
17 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Rope Crisis in Ropeland!
18 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Nice Milk
19 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo (euiseong@skku.edu)
Nice Milk