Kamila Frye

Answered

2022-10-21

Finding the counterclockwise nearest point in $\mathcal{O}(\mathrm{log}(n))$ time

Given a point q, direction w and point set P with convex hull $({v}_{1},\dots ,{v}_{n})$ known, give an algorithm to find the point in P that (q,w) touches first when rotated counterclockwise in $\mathcal{O}(\mathrm{log}(n))$ time.

REMARK: We can assume that $({v}_{1},\dots ,{v}_{n})$ is sorted counterclockwise.

My idea would be the following: Since $({v}_{1},\dots ,{v}_{n})$ is sorted we can modify the approach of Binary Search to get the required $\mathcal{O}(\mathrm{log}(n))$ bound. I suppose we have to test the angles between $(q,w,{v}_{i})$ repeatedly, but I do not see how to formulate this clearly.

Answer & Explanation

faux0101d

Expert

2022-10-22Added 21 answers

Step 1

To check the orientation of ${v}_{i}-q$ vs. w, we can use the cross product (after viewing our ${\mathbb{R}}^{2}$ as subspace of ${\mathbb{R}}^{3}$). This amounts to checking the sign of the expression

$F(i):=({v}_{i,y}-{q}_{y}){w}_{x}-({v}_{i,x}-{q}_{x}){w}_{y}=({v}_{i}-q)\cdot {w}^{\perp}$ where ${w}^{\perp}=(-{w}_{y},{w}_{x})$ is w rotated left by 90°.

All ${v}_{i}$ for which F(i) is positive are on the same side ("ahead") of the line given by point q and direction w, all ${v}_{i}$ with negative sign are on the other side ("behind"), and all with result 0 are on the line itself.

Step 2

If q is inside the convex hull of P, we are looking for a change from negative to positive (or zero) sign along the cyclically ordered vertex sequence. Binary search is fine as soon as we have obtained one i with $F(i)<0$ and one j with $F(j)\ge 0$. Picking a random index will give us one of the signs, but it is not that trivial to find an index with the other sign. To do so, we may want to look for a maximizer or minimizer of $F({v}_{i})$. We may use that F switches between increasing and decreasing only once. So start with $i=1$, $j\approx \frac{1}{2}n$, $k\approx \frac{2}{3}n$. If differnet signs occur at these, we are already done (up to the final binary search). So assume that F(i), F(j), F(k) are all negative and we want to look for a maximizer (or similarly: all positive and minimizer). By perhaps using cyclic rearrangement of i, j, k, we may assume that $F(i)<F(j)>F(k)$, i.e., the maximum among these three values is at the middle index. To facilitate distance calculations, I shall however assume that$i<j<k$. Now we know that the maximizer is between i and k. We can pick $l\ne j$ between i and k and thereby reduce our interval of length $k-i$ to an interval of length at most $max\{k-l,j-i\}$ if $l<j$ or $max\{k-j,l-i\}$ if $l>j$ by picking those three consecutive out of four indices that have the maximum in the middle again. The essential trick is to pick l smartly so that the interval length decreases fast enough. Picking l such that it split [i,k] roughly by the Golden Ratio ensures this.

Most Popular Questions