[Math] Point in a spherical triangle test

geometrypolar coordinatesspherical-geometry

Given three latitude/longitude coordinates on a sphere forming a triangle, how do I test if a point p is inside that triangle?

I know latitude and longitude implies Earth and Earth is not perfectly spherical, but in my simplified model it will be. Essentially I'm interested in a point-in-triangle test for spherical geometry, using polar coordinates.

Best Answer

Note that for the point $p$ to lie within the spherical triangle bound by $\{ p_1, p_2, p_3 \}$, the intersection of the ray from the origin to $p$ (basically the line joining $(0,0,0)$ and $p$) with the 3D-plane defined by $\{ p_1, p_2, p_3 \}$ lies inside the planar triangle $\{ p_1, p_2, p_3 \}$. This can be formulated with the unknown quantities $\lambda$, $\alpha$, $\beta$ and $\gamma$, to be determined by solving the linear system of equations: $$ \alpha p_1 + \beta p_2 + \gamma p_3 = \lambda p $$

If $\lambda, \alpha, \beta, \gamma > 0$ and $\alpha + \beta + \gamma = 1$, then the projection of $p$ onto the plane $\{p_1, p_2, p_3\}$ lies in the triangle $\{p_1, p_2, p_3\}$ and therefore the point $p$ itself lies in the spherical triangle. With a little jugglery, this can be implemented in any popular linear algebra package very easily. I would start by solving the linear system: $$ \frac{\alpha}{\lambda} p_1 + \frac{\beta}{\lambda} p_2 + \frac{\gamma}{\lambda} p_3 = p $$ And then determining $\lambda$ by forcing $\alpha + \beta + \gamma = 1$.

Related Question