Points Inside Triangles and Quadrilaterals

Consider the problem of determining whether a point lies inside relatively simple polygons, such as triangles or quadrilaterals. The general polygon case is more complex and is left to the reader (and can optionally be reduced to the triangle or quadrilateral case).

For triangles and quadrilaterals, the approaches presented here are very efficient when a single polygon must be tested against a large number of points, since they allow preprocessing. The basic idea is to transform the polygon's coordinate space into a space in which the test is easier to perform.

For example, a parallelogram defined by two generating vectors can always be transformed into a unit square $(0,0)-(1,1)$ by an affine transformation $\mathbf{p} \mapsto \left( X, Y \right)$. A point $\mathbf {p}$ lies inside the parallelogram if $0<X(\mathbf{p})<1$ and $0<Y(\mathbf{p})<1$. The same transformation also applies to triangles formed by the same generating vectors, but with the test $0<X(\mathbf{p})<1$ and $0<Y(\mathbf{p})<1-X(\mathbf{p})$. Testing whether a point lies inside the parallelogram requires only four multiplications and six additions. Creating the affine transformation is more costly, but when many tests are performed, this fixed cost is negligible.

To transform general quadrilaterals into a unit square $(0,0)-(1,1)$, a homographic transformation must be used (see Section 1.11). Although computing the transformation has a high initial cost, checking whether a point lies inside the geometric shape is relatively simple:

\begin{displaymath}
\begin{array}{l}
0<h_0 p_x + h_1 p_y + h_2 < h_6 p_x + h_7 p...
...3 p_x + h_4 p_y + h_5 < h_6 p_x + h_7 p_y + h_8 \\
\end{array}\end{displaymath} (1.127)

it requires only six multiplications, six additions, and four comparisons.

Paolo medici
2026-10-19