Describe a data structure for storing a convex polygon $P$ which allows us to find out in $O(\log n)$ time whether a query point is contained in $P$. The data structure should be constructed in linear time.
Data structure:
Structure: array
Construction:
Query for a point $p$ (assume clockwise orientation):
False
True
, if it is a left turn, return False
We are given two convex polygons $P, Q$. Describe a linear time algorithm which computes $\operatorname{CH}(P \cup Q)$. You may assume that $P$ and $Q$ are disjoint.
This also works if the polygons are not disjoint!
We are given a convex polygon $P$ as an array of vertices $p[1, \dots ,n]$.
def highestPoint(p):
n = len(p)
def rising(i):
return p[(i+1) % n].y > p[i].y
i, j = 0, n-1
if rising(j) and not rising(i):
return p[i]
while j - i > 1:
h = (i+j) // 2
if rising(i):
if not rising(h) or p[h].y < p[i].y:
j = h
else:
i = h
else:
if rising(h) or p[h].y < p[i].y:
i = h
else:
j = h
if p[i].y > p[j].y:
return p[i]
else:
return p[j]