Kamila Frye

2022-10-21

Finding the counterclockwise nearest point in $\mathcal{O}\left(\mathrm{log}\left(n\right)\right)$ time
Given a point q, direction w and point set P with convex hull $\left({v}_{1},\dots ,{v}_{n}\right)$ known, give an algorithm to find the point in P that (q,w) touches first when rotated counterclockwise in $\mathcal{O}\left(\mathrm{log}\left(n\right)\right)$ time.
REMARK: We can assume that $\left({v}_{1},\dots ,{v}_{n}\right)$ is sorted counterclockwise.
My idea would be the following: Since $\left({v}_{1},\dots ,{v}_{n}\right)$ is sorted we can modify the approach of Binary Search to get the required $\mathcal{O}\left(\mathrm{log}\left(n\right)\right)$ bound. I suppose we have to test the angles between $\left(q,w,{v}_{i}\right)$ repeatedly, but I do not see how to formulate this clearly.

Do you have a similar question?

faux0101d

Expert

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\left(i\right):=\left({v}_{i,y}-{q}_{y}\right){w}_{x}-\left({v}_{i,x}-{q}_{x}\right){w}_{y}=\left({v}_{i}-q\right)\cdot {w}^{\perp }$ where ${w}^{\perp }=\left(-{w}_{y},{w}_{x}\right)$ 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\left(i\right)<0$ and one j with $F\left(j\right)\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\left({v}_{i}\right)$. 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\left(i\right)F\left(k\right)$, i.e., the maximum among these three values is at the middle index. To facilitate distance calculations, I shall however assume that$i. 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\left\{k-l,j-i\right\}$ if $l or $max\left\{k-j,l-i\right\}$ 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.

Still Have Questions?

Free Math Solver