09 computational geometry

Upload: p4patelkeyur

Post on 01-Jun-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 09 Computational Geometry

    1/46

    CS 97SI: INTRODUCTION TOPROGRAMMING CONTESTS

    Jaehyun Park

  • 8/9/2019 09 Computational Geometry

    2/46

    Computational Geometry

    Cross Product

    Segment-Segment Intersection

    Convex Hull Problem

    Graham Scan

    Sweep Line Algorithm

    Intersecting Half-planes

    A Useful Note on Binary/Ternary Search

  • 8/9/2019 09 Computational Geometry

    3/46

    Cross Product

    Arguably the most important operation in 2D

    geometry

    Well use it all the time

    Applications:

    Determining the (signed) area of a triangle

    Testing if three points are collinear Determining the orientation of three points

    Testing if two line segments intersect

  • 8/9/2019 09 Computational Geometry

    4/46

    Cross Product

    Define ccw , , =

    ccw , , > 0

    is a left turn

    ccw , , < 0

    is a right turn

  • 8/9/2019 09 Computational Geometry

    5/46

    Segment-Segment Intersection Test

    Given two segmentsand

    Want to determine if they intersect properly: two

    segments meet at a single point that are strictly

    inside both segments

    ProperNot Proper

    Non-intersecting

  • 8/9/2019 09 Computational Geometry

    6/46

    Segment-Segment Intersection Test

    Assume that the segments intersect

    Froms point of view, looking straight to , and

    must lie on different sides

    Holds true for the other segment as well

    The intersection exists and is proper if:

    ccw , , ccw , , < 0

    AND ccw , , ccw , , < 0

  • 8/9/2019 09 Computational Geometry

    7/46

    Segment-Segment Intersection Test

    Determining non-proper intersections

    We need more special cases to consider!

    e.g. If ccw ,, , ccw ,, , ccw ,, ,

    ccw ,, are all zeros, then two segments arecollinear

    Very careful implementation is required

  • 8/9/2019 09 Computational Geometry

    8/46

    Convex Hull Problem

    Given points on the plane, find the smallest

    convex polygon that contains all the given points

    For simplicity, assume that no three points are collinear

  • 8/9/2019 09 Computational Geometry

    9/46

    Simple algorithm

    is an edge of the convex hull iff ccw ,,

    have the same sign for all other given points

    This gives us a simple algorithm

    For eachand :

    If ccw , , > 0for all , :

    Record the edge Walk along the recorded edges to recover the

    convex hull

  • 8/9/2019 09 Computational Geometry

    10/46

    Faster Algorithm: Graham Scan

    We know that the leftmost given point has to be in

    the convex hull

    We assume that there is a unique leftmost point

    Make the leftmost point the origin So that all other points have positive coordinates

    Sort the points in increasing order of /

    Increasing order of angle, whatever you like to call it Incrementally construct the convex hull using a stack

  • 8/9/2019 09 Computational Geometry

    11/46

    Incremental Construction

    We maintain a convex chainof the given points

    For each , we do the following:

    Append point to the current chain

    If the new point causes a concave corner, remove the

    bad vertex from the chain that causes it

    Repeat until the new chain becomes convex

  • 8/9/2019 09 Computational Geometry

    12/46

    Example

    Points are numbered in increasing order of /

    new origin

    1

    23

    4

    5

    6

    7

    89

  • 8/9/2019 09 Computational Geometry

    13/46

    2

    Example

    Add the first two points in the chain

    new origin

    1

    34

    5

    6

    7

    89

  • 8/9/2019 09 Computational Geometry

    14/46

    2

    Example

    Adding point 3 causes a concave corner 1-2-3

    Remove 2

    new origin

    1

    34

    5

    6

    7

    89

  • 8/9/2019 09 Computational Geometry

    15/46

    Example

    Thats better

    new origin

    1

    34

    5

    6

    7

    89

  • 8/9/2019 09 Computational Geometry

    16/46

  • 8/9/2019 09 Computational Geometry

    17/46

    Example

    Continue adding points

    new origin

    1

    4

    5

    6

    7

    89

  • 8/9/2019 09 Computational Geometry

    18/46

    Example

    Continue adding points

    new origin

    1

    4

    5

    6

    7

    89

  • 8/9/2019 09 Computational Geometry

    19/46

    Example

    Continue adding points

    new origin

    1

    4

    5

    6

    7

    89

  • 8/9/2019 09 Computational Geometry

    20/46

    Example

    Bad corner!

    new origin

    1

    4

    5

    6

    7

    89

  • 8/9/2019 09 Computational Geometry

    21/46

    Example

    Bad corner again!

    new origin

    1

    4

    5

    7

    89

  • 8/9/2019 09 Computational Geometry

    22/46

    Example

    Continue adding points

    new origin

    1

    4

    7

    89

  • 8/9/2019 09 Computational Geometry

    23/46

    Example

    Continue adding points

    new origin

    1

    4

    7

    89

  • 8/9/2019 09 Computational Geometry

    24/46

  • 8/9/2019 09 Computational Geometry

    25/46

    Example

    Done!

    new origin

    1

    4

    7

    89

  • 8/9/2019 09 Computational Geometry

    26/46

    Pseudocode

    Set the leftmost point 0,0 , and sort the rest of the

    points in increasing order of /

    Initialize stack

    For = 1 :

    Letbe the second topmost element of , be the

    topmost element of , be the th point

    If ccw , , < 0, pop and go back Push to

    Points in form the convex hull

  • 8/9/2019 09 Computational Geometry

    27/46

    Sweep Line Algorithm

    A problem solving strategy for geometry problems

    The main idea is to maintain a line (with some

    auxiliary data structure) that sweeps through the

    entire plane and solve the problem locally We cant simulate a continuous process, (e.g.

    sweeping a line) so we define eventsthat causes

    certain changes in our data structure And process the events in the order of occurrence

    Well cover one sweep line algorithm

  • 8/9/2019 09 Computational Geometry

    28/46

    Sweep Line Algorithm

    Problem: Given axis-aligned rectangles, find the

    area of the union of them

    We will sweep the plane from left to right

    Events: left and right edges of the rectangles

    The main idea is to maintain the set of active

    rectangles in order

    It suffices to store the -coordinates of the rectangles

  • 8/9/2019 09 Computational Geometry

    29/46

    Example

    Sweep

    line

  • 8/9/2019 09 Computational Geometry

    30/46

    Example

    Blue interval is added

    to the data structure

  • 8/9/2019 09 Computational Geometry

    31/46

    Example

    green area = blue segment length

    (distance that the sweep line traveled)

  • 8/9/2019 09 Computational Geometry

    32/46

    Example

  • 8/9/2019 09 Computational Geometry

    33/46

    Example

  • 8/9/2019 09 Computational Geometry

    34/46

    Example

  • 8/9/2019 09 Computational Geometry

    35/46

    Example

  • 8/9/2019 09 Computational Geometry

    36/46

    Example

  • 8/9/2019 09 Computational Geometry

    37/46

    Example

  • 8/9/2019 09 Computational Geometry

    38/46

    Example

    One of the segments is removed

  • 8/9/2019 09 Computational Geometry

    39/46

    Example

  • 8/9/2019 09 Computational Geometry

    40/46

    Pseudopseudocode

    If the sweep line hits the left edge of a rectangle

    Insert it to the data structure

    Right edge?

    Remove it

    Move to the next event, and add the area(s) of the

    green rectangle(s)

    Finding the length of the union of the blue segments isthe hardest step

    There is an easy method for this step

  • 8/9/2019 09 Computational Geometry

    41/46

    Notes on Sweep Line Algorithms

    Sweep line algorithm is a generic concept

    Come up with the right set of events and data structures

    for each problem

    Exercise problems Finding the perimeter of the union of rectangles

    Finding all intersections of line segments in

    + log time

  • 8/9/2019 09 Computational Geometry

    42/46

    Intersecting Half-planes

    Representing a half-plane: + + 0

    The intersection of half-planes is a convex area

    If the intersection is bounded, it gives a convex polygon

    Given half-planes, how do we compute theintersection of them?

    i.e. Find vertices of the convex area

    There is an easy algorithm and a hard log one

    We will cover the easy one

  • 8/9/2019 09 Computational Geometry

    43/46

  • 8/9/2019 09 Computational Geometry

    44/46

    Intersecting Half-planes

    The intersection of half-planes can be unbounded

    But usually, we are given limits on the min/max values

    of the coordinates

    Add four half-planes , , , (for large ) to ensure that the intersection is

    bounded

    Time complexity:

    Pretty slow, but easy to code

  • 8/9/2019 09 Computational Geometry

    45/46

  • 8/9/2019 09 Computational Geometry

    46/46

    Ternary Search

    Another useful method in many geometry problems

    Finds the minimum point of a convex function

    Not exactly convex, but lets use this word anyway

    Initialize the search interval [,]

    Until becomes small:

    = + ( )/3, 2= ( )/3

    If (2), then set to 2Otherwise, set to