Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

Dr.

Pratyay Kuila

Dept. of Computer Science & Engineering


NIT Sikkim-737139
➢ Computational geometry is studied to solve geometric problems.
➢ It has applications in such diverse fields as:
▪ Computer graphics
▪ Robotics
▪ VLSI design
▪ Computer-aided design
▪ Molecular modeling
▪ Metallurgy
▪ Manufacturing
▪ Textile layout
➢ How to answer basic questions about line segments efficiently and
accurately?
➢ Whether one segment is clockwise or counterclockwise from another that
shares an endpoint?
➢ Which way we turn when traversing two adjoining line segments, and
whether two line segments intersect?

Dr. Pratyay Kuila, NIT Sikkim 2


▪ A set of points can be represented as {p1, p2, p3, …}, where each
pi = (xi, yi) and xi, yi ∈ ℝ.

▪ Definition: A convex combination of two distinct points p1 = (x1, y1) and


p2 = ( x2, y2) is any point p3 = (x3, y3) such that for some α in the range
0 ≤ α ≤ 1, we have x3 = α x1 + (1 - α) x2 and y3 = α y1 + (1 - α) y2. We also
write that p3 = α p1 + (1- α)p2 . Intuitively, p3 is any point that is on the line
passing through p1 and p2 and is on or between p1 and p2 on the line.
p3 = (x3, y3)
p1 = (x1, y1) p2 = (x2, y2)

▪ Definition: Given two distinct points p1 and p2, the line segment p1p2 is the
set of convex combinations of p1 and p2. We call p1 and p2 the endpoints of
segment p1p2.

Dr. Pratyay Kuila, NIT Sikkim 3


1. Given two directed segments p0p1 and p0p2, is p0p1 clockwise from p0p2
with respect to their common endpoint p0?
2. Given two line segments p1p2 and p2p3, if we traverse p1p2 and then p2p3,
do we make a left turn at point p2?
3. Do line segments p1p2 and p3p4 intersect?

p2 p3
p3 p2

p1 p2 p4

p0 p1 p1

(1) (2) (3)

Dr. Pratyay Kuila, NIT Sikkim 4


Cross Product: We can interpret the cross product p1×p2 as the signed area of
the parallelogram formed by the points (0,0), p1, p2, and p1+p2 = (x1+x2, y1+y2).
x x 
p1  p2 = det  1 2 
 y1 y2 
= x1 y2 − x2 y1
= − p2  p1

▪ If p1×p2 is positive, then p1 is clockwise


from p2 with respect to the origin (0, 0);
if this cross product is negative, then p1
is counterclockwise from p2.

Dr. Pratyay Kuila, NIT Sikkim 5


Dr. Pratyay Kuila, NIT Sikkim 6
➢If p1×p2 is positive, then vector p1 is clockwise from vector p2 with
respect to the origin (0, 0) and that if this cross product is negative,
then p1 is counterclockwise from p2.

Justification: Suppose first that the cross product is positive.


Therefore, x1y2 - x2y1 > 0.
or, (y2/x2) > (y1/x1).
or, tan-1 (y2/x2) > tan-1(y1/x1) //Since, tan-1 is a monotone function.

Thereby, the angle that p2 makes with the positive x axis is greater than
the angle that p1 does. Therefore, vector p1 is clockwise from vector p2
with respect to the origin (0, 0).

Similarly, if the cross product is negative, it can be shown that we


need to go counter clockwise from p2 to get to p1.

Dr. Pratyay Kuila, NIT Sikkim 7


Q. How to determine whether consecutive segments turn left or right?
Ans: A positive cross product indicates a clockwise orientation and a right
turn. A cross product of 0 means that points p0, p1, and p2 are co-linear.
Q. How to determine whether two line segments intersect?
Ans:

Various cases of line segment position.

Dr. Pratyay Kuila, NIT Sikkim 8


Q. How to determine whether two line segments intersect?
Ans: We check whether each segment straddles the line containing the
other. A segment p1p2 straddles a line if point p1 lies on one side of the
line and point p2 lies on the other side. A boundary case arises if p1 or p2
lies directly on the line.

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. (This
condition comes from the boundary case.)

Dr. Pratyay Kuila, NIT Sikkim 9


Q. How to determine whether two line segments intersect? (Cont…)
SEGMENTS-INTERSECT(p1, p2, p3, p4) DIRECTION(pi, pj, pk)
d1 = DIRECTION(p3, p4, p1) Return (pk - pi ) × (pj - pi)
d2 = DIRECTION(p3, p4, p2)
d3 = DIRECTION(p1, p2, p3)
d4 = DIRECTION(p1, p2, p4) ON-SEGMENT(pi, pj, pk)
If ((d1 > 0 and d2 < 0) or (d1 < 0 and d2 > 0)) If min (xi, xj) ≤ xk ≤ max(xi, xj)
and ((d3 > 0 and d4 < 0) or (d3 < 0 and d4 > 0)) and min(yi, yj ) ≤ yk ≤ max(yi, yj)
Return TRUE Return TRUE
ElseIf d1 == 0 and ON-SEGMENT(p3, p4, p1) Else return FALSE
Return TRUE
ElseIf d2 == 0 and ON-SEGMENT(p3, p4, p2)
Return TRUE p3 p2
ElseIf d3 == 0 and ON-SEGMENT(p1, p2, p3)
Return TRUE p4
ElseIf d4 == 0 and ON-SEGMENT(p1, p2, p4)
Return TRUE
p1
Else Return FALSE

Dr. Pratyay Kuila, NIT Sikkim 10


 One simple solution is to check each pair of segment, Si and
Si+1. Therefore, with a set of n segments, total O(n2) checking
can be done. Can it be further improved??
 An algorithm that uses a technique known as “sweeping”.
 It determines only whether or not any intersection exists; it
does not print all the intersections.
 An imaginary vertical sweep line passes through the given
set of geometric objects.
 The algorithm considers all the line-segment endpoints in
left-to-right order and checks for an intersection each time it
encounters an endpoint.
Dr. Pratyay Kuila, NIT Sikkim 11
Assumptions
 There are no vertical segments.

Terminologies
 Consider two segments s1 and s2. We say that these segments 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 the intersection of s1 with the sweep line at x is higher than
the intersection of s2 with the same sweep line, or if s1 and s2
intersect at the sweep line.

Dr. Pratyay Kuila, NIT Sikkim 12


The ordering among line segments at various vertical sweep lines. (a) We
have a ≥ r c, a ≥ t b, b ≥ t c, a ≥ t c, and b ≥ u c. Segment d is comparable
with no other segment shown. (b) When segments e and f intersect, they
reverse their orders: we have e ≥v f and also f ≥ w e. Any sweep line (such
as z) that passes through the shaded region has e and f consecutive in the
ordering given by the relation ≥ z.
Dr. Pratyay Kuila, NIT Sikkim 13
Terminologies
The sweep-line status is a total preorder T, for which we
require the following operations:

• INSERT(T, s): insert segment s into T.


• DELETE(T, s): delete segment s from T.
• ABOVE(T,s): return the segment that is immediately above
the segment s in T.
• BELOW(T,s): return the segment that is immediately below
the segment s in T.

We can perform each of the operations in O(log n) time using


red-black trees.
Dr. Pratyay Kuila, NIT Sikkim 14
Dr. Pratyay Kuila, NIT Sikkim 15
The execution of ANY-SEGMENTS-INTERSECT. Each dashed line is the sweep line
at an event point. Except for the rightmost sweep line, the ordering of segment names
below each sweep line corresponds to the total preorder T at the end of the for loop
processing the corresponding event point. The rightmost sweep line occurs when
processing the right endpoint of segment c; because segments d and b surround c and
intersect each other, the procedure returns TRUE.
Dr. Pratyay Kuila, NIT Sikkim 16
➢ If set S contains n segments, then ANY-SEGMENTS-INTERSECT runs in
time O(n log n).
➢ Line 1 takes O(1) time. Line 2 takes O(n log n) time, using merge sort or
heap-sort.
➢ The for loop of lines 3–11 iterates at most once per event point, and so with
2n event points, the loop iterates at most 2n times.
➢ Each iteration takes O(log n) time, since each red-black-tree operation takes
O(log n) time. Each intersection test takes O(1) time. The total time is thus
O(n log n).

➢ It determines only whether or not any intersection exists; it does not print all
the intersections.

Dr. Pratyay Kuila, NIT Sikkim 17


1. How to determine whether any three points are co-linear?
2. Show how to determine in O(n2log n) time whether any three points in a set
of n points are co-linear.
3. Give an O(nlog n) time algorithm to determine whether an n vertex polygon
is simple.
4. Give an O(nlog n) time algorithm to determine whether two simple
polygons with a total of n vertices intersect.
5. A disk consists of a circle plus its interior and is represented by its center
point and radius. Two disks intersect if they have any point in common.
Give an O(nlog n) time algorithm to determine whether any two disks in a
set of n intersect.
6. Given a set of n line segments containing a total of k intersections, show
how to output all k intersections in O((n+k)log n) time.

Dr. Pratyay Kuila, NIT Sikkim 18


 A polygon P is convex if for every pair of points x and y
in P, the line xy is also in P; otherwise, it is called
concave.

P x y P x y

 The convex hull problem : The convex hull of a set of


planar points is the smallest convex polygon containing
all of the points.

Dr. Pratyay Kuila, NIT Sikkim 19


Dr. Pratyay Kuila, NIT Sikkim 20
Dr. Pratyay Kuila, NIT Sikkim 21
Dr. Pratyay Kuila, NIT Sikkim 22
Dr. Pratyay Kuila, NIT Sikkim 23
Dr. Pratyay Kuila, NIT Sikkim 24
Dr. Pratyay Kuila, NIT Sikkim 25
Dr. Pratyay Kuila, NIT Sikkim 26
Dr. Pratyay Kuila, NIT Sikkim 27
Dr. Pratyay Kuila, NIT Sikkim 28
Dr. Pratyay Kuila, NIT Sikkim 29
Dr. Pratyay Kuila, NIT Sikkim 30
Dr. Pratyay Kuila, NIT Sikkim 31
Dr. Pratyay Kuila, NIT Sikkim 32
Dr. Pratyay Kuila, NIT Sikkim 33
Dr. Pratyay Kuila, NIT Sikkim 34
 Graham’s scan takes O(nlog n) time due to initial sort of
angles.

Dr. Pratyay Kuila, NIT Sikkim 35


References:

1. T. H. Cormen, C. E. Leiserson, R. L. Rivest, Introduction to Algorithms,


Prentice hall.

Dr. Pratyay Kuila, NIT Sikkim 36

You might also like