Checking that a point is in a spherical polygon

linear algebraspherical-geometry

I am trying to find an algorithm to check if a point $p$ on the surface of a sphere lies within a spherical polygon with vertices at $v_1, v_2, \dots, v_J$. Any polygons I am considering will be "small", so the smaller of the two potential polygons on the surface of the sphere. I also expect my polygons to be spherical squares and hexagons, that is nothing too irregular. My points are arranged so that any polygon will be CCW.

Right now, I am thinking of using the following. I have not yet found a counter-example to suggest it shouldn't work, but given how hard it has been to find such an algorithm I am wondering if I am missing something as this seems very straighforward.

If $\hat{\mathbf{n}}_{j,p}$ is unit vector normal to the great circle connecting $v_j$ and $p$, then the interior angle for the points $v_j$$p$$v_{j+1}$ will be $$\alpha_j = \arccos(\hat{\mathbf{n}}_{j,p}\cdot\hat{\mathbf{n}}_{p,j+1})$$
and the point is in the polygon iff
$$\sum_{j=1}^J\alpha_j=2\pi.$$

Can anyone suggest a reason why this would not work? If one checked that angles were CW or CCW, is there any reason it could not be extended for more complex polygons with more of a "Pacman" shape? (Apologies for poor vernacular!)

Best Answer

Using the suggestions in Will's comments, if a $J$-sided polygon is convex and CCW, then $\hat{\mathbf{n}}_j$ points into the polygon and so for a point $\mathbf{p}$ if $\hat{\mathbf{n}}_j\cdot\mathbf{p}>0\,\forall j\in J$ then the point is in the polygon. Should the $\hat{\mathbf{n}}_j\cdot\mathbf{p}=0$ for any side $j$ then the point lies on that line.

Related Question