Week12 Geometry

23
This course material is now made available for public usage. Special acknowledgement to School of Computing, National University of Singapore for allowing Steven to prepare and distribute these teaching materials. CS3233 CS3233 Competitive Programming Dr. Steven Halim Dr. Steven Halim Week 12 – (Computational) Geometry CS3233 Competitive Programming, Steven Halim, SoC, NUS

Transcript of Week12 Geometry

This course material is now made available for public usage.Special acknowledgement to School of Computing, National University of Singapore

for allowing Steven to prepare and distribute these teaching materials.

CS3233CS3233Competitive Programmingp g g

Dr. Steven HalimDr. Steven Halim

Week 12 – (Computational) Geometry

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS

Class Photos Important for documentation/promotional purpose

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS

The major part of the hard copy material of a top ICPC team is usuallya collection of geometric libraries…

GEOMETRY BASICS AND LIBRARIESa collection of geometric libraries…

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS

Some Comp Geometry PrinciplesSome Comp Geometry Principles

• Whenever possible, we prefer test (predicates) than computing numerical answers

• Tests:– Avoid division and square root(and any operations that will produce numerical errors)

– Preferably, all operations are done in integers

Geometry Basics 0DGeometry Basics – 0D

• 0D Objects:– Point

struct point { int x, y }; // preferred modestruct point_d { double x, y };

• Re‐iterate: whenever possible, work with integers

• PS: for floating point comparisons in this lecture use:• PS: for floating point comparisons in this lecture, use:

#define EPS 1e-9

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS

Geometry Basics 1D (1)Geometry Basics – 1D (1)

• 1D Objects:– Lines (ch7_01_lines.cpp)

• Line equation, y = mx + c– Be careful with vertical lines (special case)

• Better line equation ax + by + c = 0Better line equation, ax + by + c = 0

– (Line) Segments: Line with two endpoints

li { d bl b }struct line { double a, b, c; };void points_to_line(point p1, point p2, line *l)void point_and_slope_to_line(point p, double m, line *l)bool parallel_line(line l1, line l2)bool same_line(line l1, line l2)void intersect_point(line l1, line l2, point_d *p)

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS

left turn/ccw Testleft_turn/ccw Test

• Given 3 points p, q, r in a plane, these are equivalent:– The sequence p, q, r is a left turn

– The there points are in an counter‐clockwise manner/ccw

– The triangle pqr has a positive area

– The point r is on the left side of line segment pq r

1 pp01

1

21

21

qqpp

q

1 21 rr

p

q

Geometry Basics 1D (2)Geometry Basics – 1D (2)

– collinear and left_turn (ccw) test

int turn(point p, point q, point r)bool collinear(point p, point q, point r)bool ccw(point p, point q, point r)

– Computing Distances

double dist(point p1, point p2) {}double distToLine(point a, point b, point p, point *c)double distToLineSegment(point a, point b, point p, point *c)

p4

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS

p4

Geometry Basics 1D (3)Geometry Basics – 1D (3)

– Vectors: magnitude + direction, for translation

struct vector { double x, y; }; // similar to pointvector toVector(point_d p1, point_d p2)point_d translate(point_d p, vector_d v)

– Rotation

point_d rotate(point_d p, double degree) // counter clockwise// be careful on using this, translate to origin first

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS

Geometry Basics 2D (1)Geometry Basics – 2D (1)

• 2D Objects:– Circles (ch7_02_circles.cpp)

• A circle centered at (a, b) and radius r is the set of all points (x, y)such that (x ‐ a)2 + (y ‐ b)2 = r2

int in circle(point p, point c, int r)_ (p p, p , )// 0 – inside, 1 – at border, 2 - outside

• π = 2 * acos(0.0)

Di t d 2 *• Diameter d = 2 * r

• Circumference c = π * d

• Arc length of a circle with circumference c + angle = / 360.0 * cArc length of a circle with circumference c + angle    / 360.0   c

• Chord length of a circle with radius r + angle  =sqrt(2 * r * r / (1 ‐ cos())) via the Law of Cosines

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS

Geometry Basics 2D (2)Geometry Basics – 2D (2)

f l * *• Area of circle A = π * r * r

• Sector area of circle with area A + angle  =  / 360.0 * A

• Segment area of circle with area A + angle =Segment area of circle with area A + angle  = • Future:

– Circle through 3 points (related to Triangle)

» Inner circle

» Outer circle

– Circle through 2 points

– in_circle test version 2, given 3 points that forms a circle and another point p, test if p is inside the circle or not

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS

in circle Test v2in_circle Test v2

• Given three points p, q, r– Form a circumcircle

• Test if a point s is in the circle s

1 22 ppppr

?011

111

21

21

22

22

2121

2121

qqpp

qqqqpppp

111

2122

2121

22

2121

rrssssrrrr

q

p

Circle Through 3 PointsCircle Through 3 Points

• Given three points p, q, r– Determine the circumcenter c1 and radius R1 of the inner triangle and (c2, R2) of the outer triangle

– R1 = A / s; R2 = a x b x c / (4 * A)• circumcenter c1, c2: will be added in the future

rr

c2 R2R1

qqc1

R1

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS

pp

Circle Through 2 PointsCircle Through 2 Points

• Given 2 points (p1, p2) + radius R, determine c1 + c2bool circle_2_points_radius(point_d p1, point_d p2, double r,

point d *ctr)point_d ctr)

p1

c1 rc2

c1 r

p2

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS

Geometry Basics 2D (3)Geometry Basics – 2D (3)

– Triangles (ch7_03_triangles.cpp)• Polygon with three vertices and three edges

• Area of Triangle 1: A 0 5 * b x h• Area of Triangle 1: A = 0.5 * b x h

• Perimeter p = a + b + c; where a, b, c are the length of the 3 edges

• Area of Triangle 2: A = sqrt(s) * sqrt(s ‐ a) * sqrt(s ‐ b) * sqrt(s ‐ c),g q ( ) q ( ) q ( ) q ( ),where semi‐perimeter s = 0.5 * pThis is called the Heron’s formula

• Radius of inner circle r = A / s• Radius of inner circle r = A / s

• Radius of outer circle R = (a * b * c) / (4.0 * A)

• Trigonometry/Law of Cosinesg y/– c2 = a2 + b2 ‐ 2 * a * b * cos()

• Trigonometry/Phytagorean Theorem2 2 b2b (90 0 d / i ht l ) 0– c2 = a2 + b2 because cos(90.0 degrees/right angle) = 0

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS

Geometry Basics 2D (4)Geometry Basics – 2D (4)

/ f• Trigonometry/Law of Sines– bc / sin() = ca / sin() = ab / sin()

– Rectangles/Trapezium/Quadrilateral (no sample code)Rectangles/Trapezium/Quadrilateral (no sample code)• Area

• Perimeter

• Etc…

– Polygon (ch7_04_polygon.cpp)• Plane figure that is bounded by a closed circuit composed of a finite sequence of straight line segments

• Basic form vertices are ordered either in cw or ccw orderBasic form, vertices are ordered either in cw or ccw order

vector<point> P; // preferred modevector<point d> P;p _

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS

Geometry Basics 2D (5)Geometry Basics – 2D (5)

f l ( l)• Perimeter of polygon (trivial)

• Area of polygon (half determinant)

• Checking if the polygon is convex (concave otherwise)Checking if the polygon is convex (concave otherwise)

• Checking if a point is inside or outside a (convex/concave) polygon

• Cutting a polygon with a straight line

• Finding a convex hull of a polygon/Graham’s Scan (discussed next)

do ble pe imete ( ecto <point> P)double perimeter(vector<point> P)double area(vector<point> P)bool is_convex(vector<point> P)b l i t i l ( i t t i t P)bool point_in_polygon(point p, vector<point> P)vector<point> cut_polygon(point a, point b, vector<point> P)vector<point> GrahamScan(vector<point> P)

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS

Area of a Polygon (area)Area of a Polygon (area)

• Given the vertices of a polygon in a circular manner (cw or ccw), its area is

11

yxyx

11 33

22

nyxyx

)(21

..

..21

mod1mod11

ininii

i yxyxA

..

nn yx

is_convex; point_in_polygon; cut_polygon

• Explanation via illustration

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS

Convex Hull ProblemConvex Hull Problem

• The Convex Hull of a set of points P is the smallest convex polygon CH(P) for which each point in P is either on the boundary of CH(P) or in its interior

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS

Graham Scan’s Algorithm for CHGraham Scan s Algorithm for CH

1. Find pivot (bottom most, right most point)

2. Angular sorting w.r.t pivot (easy with library)

3. Series of ccw tests (with help of stack)

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS

Geometry Basics 3DGeometry Basics – 3D

• 3D Objects:– Sphere (very rarely used)

• Great circle distance

• See book for formula… this is not that popular…

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS

Not Yet Covered This SemNot Yet Covered This Sem 

• The famous Plane Sweep Paradigm– Event, priority_queue

• Intersection Problems– 1D – 1D; 2D – 2D; 3D – 3D

• Divide and Conquer in Geometry– The closest pair problemThe closest pair problem

– Bisection method in geometry problem

– Geometric data structure (Segment/Fenwick Tree done– Geometric data structure (Segment/Fenwick Tree done,Other DS like Quad Tree: not yet)

CS3233 ‐ Competitive Programming,Steven Halim, SoC, NUS