Lecture#9 - Computational Geometry
Lecture#9 - Computational Geometry
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
• 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
• Area of a polygon = 1/2 * [(x1.y2 + x2.y3 +…+ xn.y1) - (y1.x2 + y2.x3 + … + yn, x1)]
x1 x2
p1 p2
y1 y2
x1 y 2 x2 y1
p2 p1
Computational Geometry
CROSS-PRODUCT ...
• 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
Computational Geometry
CONVEX VS AFFINE COMBINATION
• 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
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
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.
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]
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
Computational Geometry
FINDING INTERSECTION POINT ...
Computational Geometry
FINDING 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.
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.
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
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.
Computational Geometry
INTERSECTION OF SEGMENTS - SWEEP
LINE ...
Computational Geometry
CONVEX HULL
Computational Geometry
CONVEXITY AND CONVEX HULL
Computational Geometry
CONVEX HULL FINDING ALGORITHMS
• 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
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 ...
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
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
Computational Geometry
FINDING THE CLOSEST PAIR OF POINTS ...
• 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 ...
Computational Geometry
BEST WISHES
for NEXT SEMESTER