0% found this document useful (0 votes)
13 views

Lecture#9 - Computational Geometry

Uploaded by

Pritom Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lecture#9 - Computational Geometry

Uploaded by

Pritom Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

COMP GEOMETRY

CSE-237 : ALGORITHM DESIGN AND


A N A LY S I S
BASIC

Computational Geometry
REPRESENTATION

• Points
• (x, y) Cartesian coordinates
• (r, Θ) Polar coordinates

• Segments
• Position of the end points of a line L

• Lines
• 2-tuple (m, c) using y = mx + c

Computational Geometry ‹#›


REPRESENTATION ...

• Lines p2
• 3-tuple (a, b, c) using ax + by = c p2
• Two points P0, P1 on the line using P(t) = (1-t)P0 + t P1
p1

• End-Points p1
p0
• For the given line segment p1p2, the two distinct points p1 and p2 are the
endpoints.

• Polygon
• Ordered list of points

Computational Geometry ‹#›


BASIC GEOMETRIC PROBLEMS

• Area of polygon • Closest point

• CW/CCW • Angle between two lines

• Line segment intersection • Point in a polygon

• Distance between a point and a • Convex hull


line

Computational Geometry ‹#›


AREA OF POLYGON

• A polygon is a piecewise-linear, closed curve in the plane. It is a curve ending


on itself.

• Sides - the sequence of straight-line segments those formed the polygon.


(x2, y2) …
• Vertex - point joining two consecutive sides.
• Simple - it does not cross itself.
(x1, y1)
• Boundary - the set of points on the polygon itself.
• Interior - the set of points by a simple polygon
• Exterior - the set of points surrounding the polygon
(xn, yn)

• Area of a polygon = 1/2 * [(x1.y2 + x2.y3 +…+ xn.y1) - (y1.x2 + y2.x3 + … + yn, x1)]

Computational Geometry ‹#›


CROSS-PRODUCT

• Consider vectors P1 and P2, the cross product P1 × P2 can be interpreted


as the signed area of the parallelogram formed by the points (0, 0), P1,
P2, and (x1 + x2, y1 + y2).

x1 x2
p1 p2 
y1 y2
 x1 y 2  x2 y1
 p2 p1

Computational Geometry
CROSS-PRODUCT ...

• If P1× P2 is positive, then P1 is CW from P2; if P1× P2 is negative, then P1 is CCW


from P2.

The lightly shaded region contains vectors that are CW from P.


The darkly shaded region contains vectors that are CCW from P.

• A boundary condition arises if the cross product is 0; in this case, the vectors are
collinear, pointing in either the same or opposite directions.

Computational Geometry
A SEGMENT STRADLES OTHER SEGMENT

• A segment P1P2 straddles a line P3P4 if point P1 lies on one side of P3P4
and point P2 lies on the other side.
p3 p2 p3 p2

p4 p4

p1 p1
P1P2 and P3P4 straddle each P1P2 straddle the line
other’s lines containing P3P4

• A boundary case arises if P1 or P2 lies directly on the line P3P4.

Computational Geometry
CONVEX VS AFFINE COMBINATION

• A convex combination is a linear combination of points where all


coefficients are non-negative and sum to 1
• Given a finite number of points P1, P2, ..., Pn in a real vector space, a
convex combination of these points is a point of the form
1P1 + 2P2 + . . . + nPn where i ≥ 1 and ∑i = 1

• Example
• A set is convex if it contains all convex combinations of its points.
• Given two distinct points p1 and p2, the line segment p1p2 is the
set of convex combinations of p1 and p2.

Computational Geometry ‹#›


LINE-SEGMENTS INTERSECTION

Computational Geometry
CROSS-PRODUCT-BASED GEOMETRIC
PRIMITIVES
• Some fundamental geometric questions:

p2 p2

p3 p2

p1 p1 p4

p0 p0 p1

Computational Geometry
DETERMINING WHETHER CW OR CCW?

• To determine whether the directed segment P0P1 is CW from a directed segment P0P2
with respect to their common endpoint P0,
p2

1. Simply translate to use P0 as the origin and recompute P1 as


P1 (x1-x0, y1-y0) and P2 as P2 (x2-x0, y2-y0))
p1
2. Compute the cross product (P1 − P0) × (P2 − P0)
( p1  p0 ) ( p2  p0 ) ( x1  x0 )( y2  y0 ) - ( x2  x0 )( y1  y0 ) p0

3. If this cross product is positive, then P0P1 is CW from P0P2; if negative, it is CCW.

Computational Geometry
MAKE A LEFT-TURN OR RIGHT-TURN?

• To determine whether two consecutive line segments P0P1 and P1P2 turn left or
right at point P1; Equivalently, determine which way the given angle P0P1P2 turns.

1. Simply compute the cross product (P2 − P0) × (P1 − P0)


2. Check whether directed segment P0P2 is CW or CCW relative to P0P1.
p2

a) If P0P2 is CCW with respect to P0P1, then we make a left turn at P1.
p1
b) If P0P2 is CW with respect to P0P1, then we make a right turn at P1.
c) If cross-product is 0, then all points P0, P1, P2, are collinear. p0

Computational Geometry
INTERSECTION OF TWO LINE SEGMENTS

• Two line segments intersect if and only if either (or both) of the
following conditions holds :
1. Each segment straddles the line containing the other.
2. An endpoint of one segment lies on the other segment. [Boundary Case]

• Technique #1: Bounding Box Test


p2 p2
p3 p3

p4 p4

p1 p1

Computational Geometry
INTERSECTION OF TWO LINE SEGMENTS ...

• Technique #2 : Does each segment straddle the line containing the other?

Computational Geometry
INTERSECTION OF TWO LINE SEGMENTS ...

Computational Geometry
FINDING INTERSECTION POINT

• Two approaches to find the actual


intersection point are : i) parametric
and ii) slope or intercept.

1. Parametric : Given 2 points on each


line L1 : (x1, y1), (x2, y2) and L2 : (x3,
y3), (x4, y4)
• The intersection P (px, py), of line L1 and
L2 can be defined using determinants.

Computational Geometry
FINDING INTERSECTION POINT ...

• The intersection point P (Px, Py) of the lines is found by

where the values of t and u are :

Computational Geometry
FINDING INTERSECTION POINT ...

2. Slope/Intercept : Suppose that two lines have the equations y = ax


+ c and y = bx + d
where a, b are the slopes (gradients) of the lines and c, d are the y-
intercepts of the lines.
• At the intersection-point, both y are same, hence : ax + c = bx + d
• By rearranging and substituting, we get the values of x and y, hence the
intersection-point :

• Note : IfGeometry
Computational a = b then the two lines are parallel. If c ≠ d as well, the lines are
INTERSECTION OF SEGMENTS - SWEEP LINE

• Sweep line (plane sweep) algorithm : have a vertical (sweep) line far
left on the plane which is swept across the plane rightwards, and
captures intersections.

• For the sake of simplicity, we assume that the


segments in set S do not form the 4 special cases:

Computational Geometry
INTERSECTION OF SEGMENTS - SWEEP
LINE ...
• Problem : Imagine that a set of n line segments as S = {S1, · · · , Sn} are given
and we want to find I, the set of all pairwise intersections of segments in S.

• Ordering segments : Two segments S1 and S2 are comparable at X if the


vertical sweep line with x-coordinate X intersects both of them.
• S1 is above S2 at X, written S1 >X S2, if S1 and S2 are comparable at X and Y1 > Y2
• The relation “>X” is a total order on segments that intersect the sweep line at X.
• A segment enters the ordering when its left endpoint is encountered by the sweep,
• A segment leaves the ordering when its right endpoint is encountered.

Computational Geometry
INTERSECTION OF SEGMENTS - SWEEP
LINE ...
• Moving the sweep line : Sweeping algorithms typically manage two
sets of data :
1. Sweep-line status gives the
relationships among the objects
intersected by the sweep line.
2. Event-point schedule is a sequence
of x-coordinates, ordered from left to
right, that defines the halting
positions (event points) of the
sweep line. Changes to the sweep-
line status occur only at event points.

Computational Geometry
INTERSECTION OF SEGMENTS - SWEEP
LINE ...
• Segment-
intersection
pseudocode :
a) takes as input a set
S of n line
segments, returning
the TRUE if any pair
of segments in S
intersects, and
FALSE otherwise.
b) The total order T is
implemented by a
red-black tree.

Computational Geometry
INTERSECTION OF SEGMENTS - SWEEP
LINE ...
• Explanation of Segment-intersection pseudocode

• Line 1 initializes the total order to be empty.


• Line 2 determines the event-point schedule by sorting the 2N segment endpoints from
left to right, breaking ties as described above.
• Each iteration of the for loop of lines 3–11 processes one event point p.
• If p is the left endpoint of a segment s, line 5 adds s to the total order, and lines 6–7 return
TRUE if s intersects either of the segments it is consecutive with in the total order defined
by the sweep line passing through p.
• If p is the right endpoint of a segment s, then s is to be deleted from the total order. Lines 9–
10 return TRUE if there is an intersection between the segments surrounding s in the total
order

Computational Geometry
INTERSECTION OF SEGMENTS - SWEEP
LINE ...
• defined by the sweep line passing through p; these segments will become consecutive in
the total order when s is deleted. If these segments do not intersect, line 11 deletes
segment s from the total order.
• Finally, if no intersections are found in processing all the 2n event points, line 12
returns FALSE.

• Running-time : The total time is O(n lg n)


• Line 1 takes O(1) time. Line 2 takes O(n lg n) time, using merge sort or heapsort.
Since there are 2n event points, the for loop of lines 3–11 iterates at most 2n
times. Each iteration takes O(lg n) time, since each red-black-tree operation takes
O(lg n) time and each intersection test takes O(1) time.

Computational Geometry
INTERSECTION OF SEGMENTS - SWEEP
LINE ...

Computational Geometry
CONVEX HULL

Computational Geometry
CONVEXITY AND CONVEX HULL

• A set of points S is convex if whenever two points P and Q are inside S,


then the whole line segment PQ is also in S.

• The convex hull of a set of points S, denoted by CH(S) is the smallest


convex polygon PG for which each point in S is either on the boundary
of PG or in its interior.

Computational Geometry
CONVEX HULL FINDING ALGORITHMS

• There are several methods to compute convex hulls :

• Rotational sweep - processes vertices in the order of the polar angles they form with a
reference vertex. Example : Graham’s Scan, Jarvis’s March
• Incremental method - the points are sorted from L2R : P1, P2, . . . , Pn. At the ith stage, the
CH({P1, P2, . . . , Pi-1}), is updated according to Pi from the left to find CH({P1, P2, . . . , Pi}).
• Divide-and-Conquer method - first divide n points into two subsets: the leftmost n/2 and
the rightmost n/2, then recursively computes the CHs and combine the CHs ceverly.
• Prune-and-Search method - finds the upper portion (or “upper chain”) of the CH by
repeatedly throwing out a constant fraction of the remaining points until only the upper chain
of the CH remains. It then does the same for the lower chain.

Computational Geometry
GRAHAM'S SCAN ALGORITHM

• Graham’s scan solves the convex-hull problem by maintaining a stack


S of candidate points P1, P2, . . . , Pn.
1) Find the point P(x, y) with the lowest y as well as lowest x [more than
one].
2) Next, the set of points must be sorted in increasing order of the angle they
and the point P make with the x-axis.
3) For each point in the sorted array in sequence :
a) Determine whether traveling from the two points immediately preceding this
point constitutes making a left turn or a right turn.
b) If a right turn, the second-to-last point is not part of the convex hull, and lies
'inside' it. The same determination is then made for the set of the latest point
and the two points that immediately
Computational Geometry
GRAHAM'S SCAN ALGORITHM ...

precede the point found to have been inside the hull,


and is repeated until a "left turn" set is found.
c) At this point the algorithm moves on to the next
point in the set of points in the sorted array minus
any points that were found to be inside the hull;
there is no need to consider these points again.
4) This process will eventually return to the point at
which it started, at which point the algorithm is
completed and the stack now contains the points
on the convex hull in counterclockwise order.

Computational Geometry
GRAHAM'S SCAN ALGORITHM ...

• Pseudocode :

• The procedure
GRAHAM-SCAN
takes as input a set
Q of points, where |
Q| ≥ 3.

Computational Geometry
GRAHAM'S SCAN ALGORITHM ...

• Following figures illustrates the progress of GRAHAM-SCAN algorithm :

Computational Geometry
CLOSEST PAIR OF POINTS

Computational Geometry
DISTANCE BETWEEN A POINT AND A LINE

• Given a point P (x0, y0) and a line L, determine the distance between
them.
• Line defined by an equation : L is defined as ax + by +c = 0 where a * b
ax0  by0  c P
distance
0 ( ax  by  c 0, (x 0 , y 0 )) 
2 2
a b

B
A

b(bx0  ay0 )  ac a (  bx0  ay0 )  bc


x 2 2
and y  2 2
• a  b a  b
The point on this line which is closest to (x0,y0) has coordinates:

Computational Geometry ‹#›


DISTANCE BETWEEN A POINT AND A LINE ...

P
h
• Given a point P (x0, y0) and a line L, determine the distance between
them. B
A
• Line defined by two points : L has two end-points A(x1, y1) and B(x2, y2)
( y  y1 ) x0  ( x2  x1 ) y0  x2 y1  y2 x1
distance( P1 , P2 , (x 0 , y 0 ))  2
( y2  y1 ) 2  ( x2  x1 ) 2

2A 1
h A bh
b 2

• The expression is equivalent to . Here A is the area of a triangle


where b is the length of a side, and h is the perpendicular
height from the opposite vertex.
Computational Geometry ‹#›
FINDING THE CLOSEST PAIR OF POINTS

• The closest pair of points can be


computed in O(n2) time by
performing a brute-force search.

• To do that, one could compute the


distances between all the
n(n−1)/2 pairs of points, then pick
the pair with the smallest
distance.

Computational Geometry
FINDING THE CLOSEST PAIR OF POINTS ...

• The closest pair of points can be computed in O(nLogn) time using


Divide-and-Conquer strategy.

• Input : An array of n points P[]


• Output : The smallest distance between two points in the given array.
• Pre-Processing : the input array is sorted according to x coordinates.

• Steps :
1) Find the middle point in the sorted array, we can take P[n/2] as middle
point.
2) Divide the given array in two halves. The first subarray contains points
from P[0] to P[n/2].
Computational Geometry
FINDING THE CLOSEST PAIR OF POINTS ...

The second subarray contains points


from
P[n/2+1] to P[n-1].
3) Recursively find the smallest distances
in both subarrays. Let the distances be
dl and dr. Find the minimum of dl and
dr. Let the minimum be d.
4) From the above 3 steps, we have an
upper bound d of minimum distance.
Now we need to consider the pairs
such that one point in pair is from the
left half and the other is from the right
Computational Geometry
half. Consider the vertical line passing
FINDING THE CLOSEST PAIR OF POINTS ...

P[n/2] and find all points whose x


coordinate
is closer than d to the middle vertical
line. Build
an array strip[] of all such points.
5) Sort the array strip[] according to y
coordinates. This step is O(nLogn). It can
be optimized to O(n) by recursively
sorting and merging.
6) Find the smallest distance in strip[].

Computational Geometry
BEST WISHES
for NEXT SEMESTER

You might also like