segment intersect

Upload: simona-mihalca

Post on 02-Jun-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Segment Intersect

    1/27

    Two Segments Intersect?

    One method: solve for the intersection point of the two lines containing the

    two line segments, and then check whether this point lies on both segments.

    In practice, the two input segments often do notintersect.

    Stage 1: quick rejection if their bounding boxesdonot intersect

    Case 1: bounding boxes

    do not intersect; neither

    will the segments.

    bounding boxes

    (x ,y )

    (x ,y)2 2

    (x ,y)

    (x , y )

    1 1

    3 3

    4 4

    x < x x >x y y4 1 3 2 4 1 3 2if and only if

    L

    M

    Lright ofM? L left ofM? LaboveM? LbelowM?

  • 8/10/2019 Segment Intersect

    2/27

    Bounding Box

    Case 2: Bounding boxes intersect; the segments may or may

    not intersect. Needs to be further checked in Stage 2.

  • 8/10/2019 Segment Intersect

    3/27

    Stage 2

    Two line segments do notintersect if and only if one segmentlies entirely to one side of the line containing the other segment.

    p1

    p2

    p3

    p4

    (p p ) (p p ) and

    (p p ) (p p ) are

    both positive!

    3 2 1 2

    4 2 1 2

  • 8/10/2019 Segment Intersect

    4/27

    Necessary and Sufficient Condition

    p

    p

    1

    2

    p

    p

    3

    4

    Two line segments intersect iff eachof the two pairs of cross

    products below have different signs (or one cross product in

    the pair is 0).

    (p

    p )(p

    p ) and (p

    p )

    (p

    p )(p p ) (p p ) and (p p ) (p p )1 4 3 4 2 4 3 43 2 1 2 4 2 1 2

    p1

    p2

    p3

    p4

    3

    1

    // the line throughp p

    // intersects pp.

    4

    2

    3

    // the line throughp p

    // intersectspp .4

    1 2

  • 8/10/2019 Segment Intersect

    5/27

    nLine Segments

    Input: a set of nline segments in the plane.Output: all intersections and for each intersection the involved

    segments. .

  • 8/10/2019 Segment Intersect

    6/27

    A Brute-Force Algorithm

    Simply take each pair of segments, and check if they intersect.

    If so, output the intersection.

    Running time (n

    ).

    2

    Most segments do not intersect, or if they do,

    only with a few other segments.

    Need a faster algorithm that deals with such situations!

    Nevertheless,sparsedistributionin practice:

  • 8/10/2019 Segment Intersect

    7/27

    The Sweeping Algorithm

    Avoid testing pairs of segments that are far apart.

    Idea: imaginea vertical sweep line passes through the given

    set of line segments, from left to right.

    Sweep

    line

  • 8/10/2019 Segment Intersect

    8/27

    Assumption on Non-degeneracy

    No segment is vertical. // the sweep line always hits a segment at// a point.

    If 1 vertical segment, imagine all segments are rotatedclockwise by a tiny angle and then test for intersection.

    This means:

    For each vertical segment, we will consider its lower endpoint

    before upper point.

  • 8/10/2019 Segment Intersect

    9/27

    Sweep Line Status

    The set of segments intersecting the sweep line.

    It changes as the sweep line moves, but not continuously.

    Updates of status happen only at event points.endpoints

    intersections

    event points

    AG

    C

    T

  • 8/10/2019 Segment Intersect

    10/27

    Ordering Segments

    Atotal orderover the segments that intersect the current

    position of the sweep line:

    A

    B

    C

    D

    E

    B > C > D(A andE not in

    the ordering)

    C > D

    (B drops out of

    the ordering)

    At an event point, the sequence of segments changes:

    Update the status.

    Detect the intersections.

  • 8/10/2019 Segment Intersect

    11/27

    Status Update (1)

    A new segmentLintersecting

    the sweep line

    L

    M

    K

    Check ifLintersects with the

    segment above (K) and the

    segment below (M).

    new eventpoint

    Intersection(s) are new event points.

    Event point is the left endpoint of a segment.

    N

    K, M, N K, L, M, N

    O

  • 8/10/2019 Segment Intersect

    12/27

    Status Update (2)

    The two intersecting segments

    (L andM) change order.

    L

    M

    K

    Check intersection with new

    neighbors (Mwith OandL withN).

    Intersection(s) are new event points.

    Event point is an intersection.

    N

    O

    O, L, M, N O, M, L, N

  • 8/10/2019 Segment Intersect

    13/27

    Status Update (3)

    The two neighbors (O andL)

    become adjacent.

    L

    M

    K

    Check if they (OandL) intersect.

    Intersection is new event point.

    Event point is a lower endpoint of a segment.

    N

    O, M, L, N O, L, N

    O

  • 8/10/2019 Segment Intersect

    14/27

    Correctness

    I nvar iantholding at any time during the plane sweep.

    All intersection points to the left of the sweep line have

    been computed correctly.

    The correctness of the algorithm thus follows.

  • 8/10/2019 Segment Intersect

    15/27

    Data Structure for Event Queue

    Data structure: balanced binary search tree (e.g., red-blacktree).

    Ordering of event points:

    byx-coordinates

    byy-coordinates in case of a tie inx-coordinates.

    Supports the following operations on a segments.

    inserting an event

    fetching the next event

    Every event pointpis stored with all segments starting atp.

    // O(log m)// O(log m)

    m = #event points in the queue

  • 8/10/2019 Segment Intersect

    16/27

    Data Structure for Sweep-line Status

    Describes the relationships among the segments intersected

    by the sweep line.

    Use a balanced binary search tree Tto support the followingoperations on a segments.

    Insert(T,s)

    Delete(T,s)

    Above(T,s) // segment immediately aboves

    Below(T,s) // segment immediately belows

    e.g, Red-blacktrees, splay trees(key comparisons replaced

    by cross-product comparisons).

    O(log n)for each operation.

  • 8/10/2019 Segment Intersect

    17/27

    An Example

    L

    K

    M

    N

    O

    K

    L O

    N M

    K

    L

    N

    O

    The bottom-up order of the segments correspond to the left-to-right

    order of the leaves in the tree T.

    Each internal node stores the segment from the rightmost leaf in its

    left subtree.

  • 8/10/2019 Segment Intersect

    18/27

    Additional Operation

    L

    K

    M

    N

    O

    K

    L O

    N M

    K

    L

    N

    O

    Searching for the segment immediately below some pointp on the sweep line.

    p

    Descend binary search tree all the way down to a leaf.

    Outputs either this leaf or the leaf immediately to its left .

    L

    O(log n) time

  • 8/10/2019 Segment Intersect

    19/27

    The Algorithm

    FindIntersections(S)

    Input: a set S of line segments

    Ouput: all intersection points and for each intersection the

    segment containing it.

    1. Q // initialize an empty event queue

    2. Insert the segment endpoints into Q// store with every left endpoint// the corresponding segments

    3. T // initialize an empty status structure4. whileQ

    5. doextract the next event pointp

    6. QQ{p}7. HandleEventPoint(p)

  • 8/10/2019 Segment Intersect

    20/27

    Handling Event Points

    Status updates (1)(3) presented earlier.Degeneracy: several segments are involved in one event point (tricky).

    A

    B

    C

    C

    A

    D

    G

    E

    H

    l

    D

    D

    AG E

    HC

    A

    C

    G

    ET:

    G C

    HA

    BA

    G

    C

    B

    (a) DeleteD, E,A, C(b) InsertB,A, C

    p

  • 8/10/2019 Segment Intersect

    21/27

    The Code of Event HandlingHandleEventPoint(p)

    1. L(p)

    { segments with left endpointp} // they are stored withp2. S(p) { segments containingp} // alladjacent in T and found by a search

    3. R(p)={ segments with right endpointp} //L(p) S(p)

    4. C(p) = { segments withpin interior } // C(p) S(p)

    5. if |LRC| > 1

    6. thenreportpas an intersection along withL, R, C

    7. TT(RC)8. TT(LC) // order as intersected by sweep line just to the

    // right ofp. segments in C(p) have order reversed.

    9. ifLC= // right endpoint only

    10. then lets ands be the neighbors right below and abovepin T

    11. FindNewEvent(s , s , p)

    12. else slowest segment inLC13. s segment right belows

    14. FindNewEvent(s ,s,p)

    15. shighest segment inLC

    16. s segment right aboves

    17. FindNewEvent(s,s ,p)

    b

    b

    b

    a

    a

    a

    b a

    sa

    sbs

    sp

  • 8/10/2019 Segment Intersect

    22/27

    Finding New Event

    FindNewEvent(s,s ,p)

    1. ifs ands intersect to the right ofp// sweep line position2. then insert the intersection point as an event in Q

    l

    l

    r

    r

  • 8/10/2019 Segment Intersect

    23/27

    Correctness

    LemmaAlgorithm FindIntersectionscomputes all interesectionsand their containing segments correctly.

    Proof By induction. Letpbe an intersection point and assume all

    intersections with a higher priority have been computed correctly.

    pis an endpoint. stored in the event queue Q.

    L(p) is also stored in Q.

    R(p) and C(p) are stored in T and will be found.

    All involved

    are determined

    correctly.

    pis not an endpoint. We show thatpwill be inserted into Q.

    All involved segments havepin interior. Order them by polar angle.A

    Bpq

    LetAandBbe neighboring segments.

    There exists event point q

  • 8/10/2019 Segment Intersect

    24/27

    Output Sensitivity

    An algorithm is output sensitiveif its running time is sensitiveto the size of the output.

    The plane sweeping algorithm is output sensitive.

    running time O((n+k) log n)

    output size

    (intersections and their

    containing segments)

    But one intersection may consist of (n) segments.

    When this happens, running time becomes .)log( 2 nnO

    Not tight! the total number of intersections may still be (n).

  • 8/10/2019 Segment Intersect

    25/27

    A Tighter Bound

    The running time is O(nlog n +Ilog n).

    # intersections

    FindIntersections(S)

    1. Q // initialize an empty event queue

    2. Insert the segment endpoints into Q// balanced binary search tree O(nlog n)

    3. T // initialize an empty status structure

    4. whileQ 5. doextract the next event pointp

    6. QQ{p} // deletion from Qtakes time O(log n).7. HandleEventPoint(p) // 1 or 2 calls to FindNewEvent

    // each call may result in an insertion into Q:O(log n).

    // each operation on Tinsertion, deletion, neighbor

    // findingtakes time O(log n).// #operations on Tis (|L(p)R(p) C(p)|)

    Idea in analysis all time cost is spent on maintaining the data structures: the event

    queue Qand the status structure tree T.

  • 8/10/2019 Segment Intersect

    26/27

    #Tree Operations

    |)()()(| pCpRpLmp

    Let

    Then the running time is O((m+n) log n).

    Claim m= O(n+I).Proof View the set of segments as aplanar graph.

    |L(p) R(p) C(p)| deg(p)

    mdeg(p)

    Every edge contributes one each to the degree of

    its two vertices. So deg(p) 2 n.

    Letn = #edges, n = #vertices.e v

    m2 ne

    Butn 2n+I. The conclusion follows.v

    n = O(n ) // Eulers equationve

  • 8/10/2019 Segment Intersect

    27/27

    Storage

    The tree Tstores every segment once. O(n)

    The size of the event queue Q: O(n+I).

    Store intersections among adjacent segments.

    Delete those of segments that stop being adjacent.

    Reduce the storage toO(n).

    Theorem AllIintersections of nline segments in the plane can

    be reported in O((n+I) log n) time and O(n) space.