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

Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

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

Lecture 13Geometry and

Computational Geometry

Euiseong Seo([email protected])

Page 2: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

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

Geometry

§ a Branch of mathematics concerned withquestions of shape, size, relative position offigures, and the properties of space

§ We know geometry§ We don’t know how to represent geometry

in programming languages

Page 3: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

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

Lines

§ Lines are the shortest distance between any twopoints

§ 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: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

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

Lines

§ General case representationtypedef struct {

double a;double b; // default value is 1doublec;

} line

• ax + by + c = 0

Page 5: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

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

Line Problems

§ Find an intersectionof two lines

• checkiftheyareparallel• elsefind

§ Whatelse?• Anglesoftwolines• Closestpointonaline• Rays– halflinewithanorigin

Page 6: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

6SWE2004: Principles in Programming | Spring 2016 | 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: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

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

Triangle Area

§ Signed triangle area

c

a

b

positive

b

a

c

negative

Page 8: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

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

Circles

§ Representationtypedef struct {

point c; /* center */double r; /* radius */

} circle;

§ Formula

§ Problems• Tangent line• Intersection points

Page 9: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

9SWE2004: Principles in Programming | Spring 2016 | 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: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

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

Line Segments

§ A portion of a linetypedef struct {

point p1, p2;

} segment;

§ Are two segmentsintersect?• Dealwithdegeneracycases

– parallel– totaloverlap

Page 11: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

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

Polygons

§ Closed chains of non-intersecting line segmentstypedef 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: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

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

Convex Hull

§ The smallest polygon containing a set of points§ Graham scan algorithm

1. Find an extreme point2. Sort the points in order of increasing angle

about the pivot3. Build the hull by adding edges when

we make a left turn, and back-tracking when we make a right turn

Page 13: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

13SWE2004: Principles in Programming | Spring 2016 | 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: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

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

Jordan Curve Theorem

Page 15: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

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

Lattice Polygon

§ Pick’s Theorem

Page 16: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

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

Rope Crisis in Ropeland!13.6. Problems 305

13.6.2 Rope Crisis in Ropeland!PC/UVa IDs: 111302/10180, Popularity: B, Success rate: average Level: 2

Rope-pulling (also known as tug of war) is a very popular game in Ropeland, justlike cricket is in Bangladesh. Two groups of players hold different ends of a rope andpull. The group that snatches the rope from the other group is declared winner.

Due to a rope shortage, the king of the country has declared that groups will not beallowed to buy longer ropes than they require.

Rope-pulling takes place in a large room, which contains a large round pillar of acertain radius. If two groups are on the opposite side of the pillar, their pulled ropecannot be a straight line. Given the position of the two groups, find out the minimumlength of rope required to start rope-pulling. You can assume that a point representsthe position of each group.

Two groups with the round pillarbetween them.

Two groups unaffected by the pillar.

InputThe first line of the input file contains an integer N giving the number of input cases.Then follow N lines, each containing five numbers X1, Y1, X2, Y2, and R, where (X1, Y1)and (X2, Y2) are the coordinates of the two groups and R > 0 is the radius of the pillar.

The center of the pillar is always at the origin, and you may assume that neitherteam starts in the circle. All input values except for N are floating point numbers, andall have absolute value ≤ 10,000.

OutputFor each input set, output a floating point number on a new line rounded to the thirddigit after the decimal point denoting the minimum length of rope required.

Sample Input21 1 -1 -1 11 1 -1 1 1

Sample Output3.5712.000

Page 17: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

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

Rope Crisis in Ropeland!

13.6. Problems 305

13.6.2 Rope Crisis in Ropeland!PC/UVa IDs: 111302/10180, Popularity: B, Success rate: average Level: 2

Rope-pulling (also known as tug of war) is a very popular game in Ropeland, justlike cricket is in Bangladesh. Two groups of players hold different ends of a rope andpull. The group that snatches the rope from the other group is declared winner.

Due to a rope shortage, the king of the country has declared that groups will not beallowed to buy longer ropes than they require.

Rope-pulling takes place in a large room, which contains a large round pillar of acertain radius. If two groups are on the opposite side of the pillar, their pulled ropecannot be a straight line. Given the position of the two groups, find out the minimumlength of rope required to start rope-pulling. You can assume that a point representsthe position of each group.

Two groups with the round pillarbetween them.

Two groups unaffected by the pillar.

InputThe first line of the input file contains an integer N giving the number of input cases.Then follow N lines, each containing five numbers X1, Y1, X2, Y2, and R, where (X1, Y1)and (X2, Y2) are the coordinates of the two groups and R > 0 is the radius of the pillar.

The center of the pillar is always at the origin, and you may assume that neitherteam starts in the circle. All input values except for N are floating point numbers, andall have absolute value ≤ 10,000.

OutputFor each input set, output a floating point number on a new line rounded to the thirddigit after the decimal point denoting the minimum length of rope required.

Sample Input21 1 -1 -1 11 1 -1 1 1

Sample Output3.5712.000

Page 18: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

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

Nice Milk336 14. Computational Geometry

14.7.8 Nice MilkPC/UVa IDs: 111408/10117, Popularity: C, Success rate: low Level: 4

Little Tomy likes to cover his bread with milk. He does this by dipping it so that itsbottom side touches the bottom of the cup, as in the picture below:

Since the amount of milk in the cup is limited, only the area between the surface ofthe milk and the bottom side of the bread is covered. Note that the depth of the milkis always h and remains unchanged with repeated dippings.

Tomy wants to cover this bread with largest possible area of milk in this way, butdoesn’t want to dip more than k times. Can you help him out? You may assume thatthe cup is wider than any side of the bread, so it is possible to cover any side completely.

InputEach test case begins with a line containing three integers n, k, and h (3 ≤ n ≤ 20,0 ≤ k ≤ 8, 0 ≤ h ≤ 10). A piece of bread is guaranteed to be a convex polygon of nvertices. Each of the following n lines contains two integers xi and yi (0 ≤ xi, yi ≤ 1,000)representing the Cartesian coordinates of the ith vertex. The vertices are numbered incounterclockwise order. The test case n = 0, k = 0, h = 0 terminates the input.

OutputOutput (to two decimal places) the area of the largest possible bread region which canbe covered with milk using k dips. The result for test case should appear on its ownline.

Sample Input4 2 11 03 05 20 40 0 0

Sample Output7.46

Page 19: Lecture 13 Geometry and Computational Geometrycsl.skku.edu/uploads/SWE2004S16/Lecture13.pdf · 2016-06-09 · 336 14. Computational Geometry 14.7.8 Nice Milk PC/UVa IDs: 111408/10117,

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

Nice Milk

336 14. Computational Geometry

14.7.8 Nice MilkPC/UVa IDs: 111408/10117, Popularity: C, Success rate: low Level: 4

Little Tomy likes to cover his bread with milk. He does this by dipping it so that itsbottom side touches the bottom of the cup, as in the picture below:

Since the amount of milk in the cup is limited, only the area between the surface ofthe milk and the bottom side of the bread is covered. Note that the depth of the milkis always h and remains unchanged with repeated dippings.

Tomy wants to cover this bread with largest possible area of milk in this way, butdoesn’t want to dip more than k times. Can you help him out? You may assume thatthe cup is wider than any side of the bread, so it is possible to cover any side completely.

InputEach test case begins with a line containing three integers n, k, and h (3 ≤ n ≤ 20,0 ≤ k ≤ 8, 0 ≤ h ≤ 10). A piece of bread is guaranteed to be a convex polygon of nvertices. Each of the following n lines contains two integers xi and yi (0 ≤ xi, yi ≤ 1,000)representing the Cartesian coordinates of the ith vertex. The vertices are numbered incounterclockwise order. The test case n = 0, k = 0, h = 0 terminates the input.

OutputOutput (to two decimal places) the area of the largest possible bread region which canbe covered with milk using k dips. The result for test case should appear on its ownline.

Sample Input4 2 11 03 05 20 40 0 0

Sample Output7.46