19
1 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected]) Lecture 13 Geometry and Computational Geometry Euiseong Seo ([email protected])

New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

1 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Lecture 13 Geometry and

Computational Geometry

Euiseong Seo

([email protected])

Page 2: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

2 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

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

Page 3: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

3 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

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

Page 4: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

4 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Lines

General case representation typedef struct {

double a;

double b; // default value is 1

doublec;

} line

• ax + by + c = 0

Page 5: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

5 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

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

Page 6: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

6 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Triangles and Trigonometry

Angle measurement • Radians – more general

• Degrees

Terminologies • Right triangle

• Perpendicular lines

• Internal/external angle

• Equilateral triangle

Page 7: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

7 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Triangle Area

Signed triangle area

c

a

b

positive

b

a

c

negative

Page 8: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

8 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Circles

Representation typedef struct {

point c; /* center */

double r; /* radius */

} circle;

Formula

Problems

• Tangent line

• Intersection points

Page 9: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

9 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

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

Page 10: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

10 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Line Segments

A portion of a line typedef struct {

point p1, p2;

} segment;

Are two segments intersect?

• Deal with degeneracy cases – parallel

– total overlap

Page 11: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

11 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

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

Page 12: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

12 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

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

Page 13: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

13 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

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

Page 14: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

14 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Jordan Curve Theorem

Page 15: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

15 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Lattice Polygon

Pick’s Theorem

Page 16: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

16 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Rope Crisis in Ropeland!

Page 17: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

17 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Rope Crisis in Ropeland!

Page 18: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

18 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Nice Milk

Page 19: New Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S15/Lecture13.pdf · 2015. 6. 8. · SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo

19 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Nice Milk