cg assign.pdf

Upload: ritajit-majumdar

Post on 02-Jun-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 CG Assign.pdf

    1/8

    Computational GeometryAssignment

    Ritajit Majumdar

    Roll: 91/CSE/111006

    April 27, 2014

    Contents

    1 Problem 1 1

    1.1 Solution . . . . . . . . . . . . . . . . . . . . . . . 1

    2 Problem 2 3

    2.1 Solution . . . . . . . . . . . . . . . . . . . . . . . 3

    3 Problem 3 4

    3.1 Solution . . . . . . . . . . . . . . . . . . . . . . . 5

    4 Problem 4 6

    4.1 Solution . . . . . . . . . . . . . . . . . . . . . . . 6

  • 8/10/2019 CG Assign.pdf

    2/8

    1 Problem 1

    Given a convex hull with h points stored in clockwise order C1, C2, . . . , C h.Give an O(logh) time algorithm to determine if a new point is within theconvex hull. Find a O(h) time algorithm to find the new convex hull.

    1.1 Solution

    Part 1

    The convex hull is given as a set of points (C1, C2, . . . , C h). Let the pointthat is to be seen is P. Consider a point Cwithin the polygon structure.Join all the points with C. This will give us h regions. We also join the point

    P with CWe start from any one such line, say CCi.

    So, now we have all the lines sorted according to angle from the line C Ci.Hence by using a binary search we can find the region in which the line P Clies in O(logh) time. Suppose it lies in the region CiCjC. So now all we needit to check whether P C intersects the line CiCj . If it does, then P is outsidethe hull, else it is inside.

    Algorithm

    Inputs

    An existing convex hull with hpoints C1, C2, . . . , C h.

    A point P.

    Steps

    1. Determine a point Cwithin the convex hull and choose any edge CCias the starting point.

    2. Find the angle betweenC P andC Ci. Now using a binary search it can

    be determined in which region the point P lies.

    3. Say the region is CiCjC. Check whether CP intersects the line CiCj .If yes, report outside, else inside.

    1

  • 8/10/2019 CG Assign.pdf

    3/8

    Part 2

    If the new point P is within the convex hull, then the convex hull need notbe changed. However, ifP is outside the hull, then the new hull would becreated with Pas one of its points.

    To create the new hull, first draw tangents to any two points, one to theimmediate left and the other to the immediate right point of the existinghull. The immediate left and right point is not necessary, but it is a prettyconvincing starting point. Now check whether all the hull points are to thesame side of the tangent i.e. orientation check. If yes, then we can includePwith those points to form the new hull. If not, then progress to the left aswell as right to find such a point.

    Algorithm

    Inputs

    An existing convex hull with hpoints C1, C2, . . . , C h.

    A point Poutside the convex hull.

    Steps

    1. FromP, draw tangent to its immediate left point, say Cl. Cl is a point

    on the existing Convex hull.

    2. Check that all the points of the existing convex hull are on the sameside of the tangent. If yes, add new line Cl Pas a new hull line. Ifnot, then try the next point to the left.

    3. Repeat steps 1 and 2 for points to the right ofP.

    4. Report the new convex hull withP.

    The time complexity of this algorithm is O(n) since at mostn points needto be checked. And since all the points are being checked, this algorithm isguaranteed to find the correct solution.

    2

  • 8/10/2019 CG Assign.pdf

    4/8

    2 Problem 2

    You are given a collection of vertical line segments in the first quadrant ofthe x, y plane. Each line segment has one endpoint on the x-axis and theother endpoint has a positive y-coordinate. Imagine that from the top of eachsegment a horizontal bullet is shot to the left. The problem is to determinethe index of the segment that is first hit by each of these bullet paths. If nosegment is hit, return the index 0.

    The input is a sequence of top endpoints of each segment pi = (xi, yi), for1 i n, which are sorted in increasing order by x-coordinate. The outputis the sequence of indices, one for each segment.

    Present an O(n) time algorithm to solve this problem.

    2.1 Solution

    To solve this problem we make explicit use of the data structure stack S.We consider the top points of each segment as Pi= (xi, yi). Initially the stackis empty. We start with x = 0 and move right. We push the y-coordinate ofeach segment in the stack and pop only when a new point arrives that has ycoordinate greater than every other segment before.

    So the 1st segment will definitely return 0. We push this segment in thestack. If the 2nd segment has greater y coordinate, then we pop the 1st oneand push the 2nd one. Else we push the 2nd one above the 1st one andreturn. We continue this process for all the segments.

    Algorithm

    Input A collection of n vertical line segments, with their x, y co-ordinates

    given i.e Pi= (xi, yi). The points are arranged according to increasing orderof x.

    Output Indices for each point.

    3

  • 8/10/2019 CG Assign.pdf

    5/8

    Steps

    1. Set S [], set the stack. Push S[0] = . This gives the initialboundary which is expected to has an infinite y coordinate.

    2. Output (n tuple)

    3. for (j = 1; j n; j++) do

    4. Retrieve Pj = (xj, yj)

    5. While yj >y coordinate of the stack top, do

    6. Pop Pi = (xi, yi) from S

    7. end while

    8. Output[j] top most index of the stack after pop

    9. Push Pj = (xj, yj)

    10. end for

    Since every item is pushed and popped out of the stack only once, hencethe complexity of the algorithm will be O(n).

    4

  • 8/10/2019 CG Assign.pdf

    6/8

    3 Problem 3

    Given a set S of n disjoint line segments in a plane and a set T of m trianglesin the plane. The triangles in T are disjoint, but a triangle could lie insideanother triangle. Devise an algorithm to report all triangles of T that donot intersect any line segment of S. [Hint: Use plane sweep algorithm.] Givetime complexity.

    3.1 Solution

    Here we consider each triangle merely as a set of 3 red lines. The set I, usedin the algorithm, is not necessary. However, it is used so that input infor-

    mation is not lost. Since the functions associated with T and I are deleteand append respectively, an array seems to be the proper data structure toimplement them.

    Time required to embed the id of a triangle to each of the sides is O(n).Delete and append functions are performed in constant time.

    So the time complexity of the algorithm will be the time complexity ofplane sweep algorithm i.e. O((n+m+I)log(n+m+)). However, the actualwork done will be much less. Since, to check whether a triangle undergoesintersection, it is not necessary to see all intersections of a triangle. And so, if

    a triangle undergoes one intersection, its colour is turned blue. Hence numberof intersections to be reported by the plane sweep algoritm is expected to bequite less.

    Algorithm

    Inputs

    SSet of n disjoint line segments.

    T Set of m disjoint traingles.

    Output All triangles of T that do not intersect any line segment of S.

    Steps

    1. Define a set I = {}, initially empty. T I always gives all the triangles.

    5

  • 8/10/2019 CG Assign.pdf

    7/8

    2. Define id(Ti) = i, i.e the id of a triangles is considered to be its position

    in the set T.3. Consider the edges of the triangle to be red in colour and those of the

    line segments is blue.

    4. Embed the id of a triangle to each of its 3 sides.

    5. Apply the plane sweep algorithm with the constraint that the neigh-bourhood search and intersection search is carried out only betweenlines of different colours.

    6. If there is an intersection of any blue line with a red line having id =

    j, then

    T := T - Tj

    I := I Tj

    7. Turn the colour of all the red lines having id = j blue.

    8. After the plane sweep algorithm terminates, report set T.

    Here I have not explained the plane sweep algorithm again. The initialpreprocessing will take O(n + m) times to embed colour and id to all lines.

    Hence the final algorithm runs in time O((n+m+I)log(n+m)).

    4 Problem 4

    Let S1 be a set of n disjoint horizontal line segments and S2 be a set of mvertical disjoint line segments. Give a plane sweep algorithm that counts thenumber of intersections in S1 S2 in O((n + m) log(n + m)) time.

    4.1 Solution

    For this problem, I have used the concept of rotating the coordinate system.

    If

    x

    y

    be the current coodinate system, then we can rotate the coordinate

    to get a new system

    x

    y

    where the lines will not be horizontal or vertical.

    6

  • 8/10/2019 CG Assign.pdf

    8/8

    Then in this system we can apply the plane sweep algorithm to find the

    output.

    Algorithm

    Inputs

    S1 set of n disjoint horizontal line segments.

    S2 set of m disjoint vertical line segments.

    Output Number of intersections in S1 S2.

    Steps

    1. Define a new coorindate system (x

    , y

    ) such that

    x

    y

    = R

    x

    y

    whereR =

    cos sin

    sin cos

    rotation matrix

    2. Rewrite all the nhorizontal and vertical line segments in (x

    , y

    ) coor-

    dinate system.

    3. Apply plane sweep algorithm in (x

    , y

    ) coordinate system.

    The vital assumption of this algorithm is that after rotation of the coordi-nate system, all the lines still remain in the first quadrant. If not, necessarytranslation may be applied.

    Since matrix multiplication takes constant time, so rewriting the lines inthe new coordinate system takes O(n+m) time.

    Now time complexity of plane sweep algorithm is O((n+I)log(n)). Hence,the time complexity in this case will be O((n+m+I)log(n+m)).

    N.B: This algorithm can be approaches in a different manner by applyingrange query.

    7