TExcelTAD

Polygons

A polygon is a closed shape made of straight segments joined end to end. Triangles, squares, road signs, country borders on a map and the faces of a 3D model are all polygons. This lesson covers their names, angle sums and diagonals, then two algorithms every graphics programmer uses: the shoelace area formula and the point-in-polygon test.

Vocabulary

The segments are sides (or edges), the corners are vertices, and a segment joining two non-neighbouring vertices is a diagonal.

Sides Name Sides Name
3 triangle 7 heptagon
4 quadrilateral 8 octagon
5 pentagon 9 nonagon
6 hexagon 10 decagon
  • Convex: every interior angle is less than 180°, and any segment between two points inside stays inside.
  • Concave: at least one interior angle is greater than 180° (a "dent").
  • Regular: all sides equal and all angles equal, like a stop sign (a regular octagon).

Angle sums and diagonals

Pick one vertex of a convex nn-gon and draw every diagonal from it. You cut the polygon into n−2n - 2 triangles, and each triangle's angles add up to 180°. So:

sum of interior angles=(n−2)⋅180∘each angle, regular n-gon=(n−2)⋅180∘/nsum of exterior angles=360∘(one full turn walking around the outline)each exterior, regular=360∘/nnumber of diagonals=n(n−3)/2\begin{aligned} \text{sum of interior angles} &= (n - 2) \cdot 180^\circ \\ \text{each angle, regular } n\text{-gon} &= (n - 2) \cdot 180^\circ / n \\ \text{sum of exterior angles} &= 360^\circ && \text{(one full turn walking around the outline)} \\ \text{each exterior, regular} &= 360^\circ / n \\ \text{number of diagonals} &= n(n - 3) / 2 \end{aligned}

The diagonal count works because each of the nn vertices connects to n−3n - 3 others (not itself, not its two neighbours), and that counts every diagonal twice.

python · try it yourself
for n in [3, 4, 5, 6, 8, 12]:
    total = (n - 2) * 180
    print(f"n={n:2}  sum={total:5}°  each={total / n:6.1f}°  exterior={360 / n:5.1f}°  diagonals={n * (n - 3) // 2}")
n= 3  sum=  180°  each=  60.0°  exterior=120.0°  diagonals=0
n= 4  sum=  360°  each=  90.0°  exterior= 90.0°  diagonals=2
n= 5  sum=  540°  each= 108.0°  exterior= 72.0°  diagonals=5
n= 6  sum=  720°  each= 120.0°  exterior= 60.0°  diagonals=9
n= 8  sum= 1080°  each= 135.0°  exterior= 45.0°  diagonals=20
n=12  sum= 1800°  each= 150.0°  exterior= 30.0°  diagonals=54

Area of a regular polygon

A regular nn-gon splits into nn identical isosceles triangles meeting at the centre. The height of each triangle is the apothem aa, the distance from the centre to the middle of a side. With side ss and perimeter P=n⋅sP = n \cdot s:

A=12⋅P⋅a,a=s2tan⁡(180∘/n)A = \tfrac{1}{2} \cdot P \cdot a, \qquad a = \frac{s}{2\tan(180^\circ/n)}

The shoelace formula

For any simple polygon (sides do not cross) with vertices (x1,y1)…(xn,yn)(x_1, y_1) \ldots (x_n, y_n) listed in order:

A=12⋅∣(x1y2−x2y1)+(x2y3−x3y2)+…+(xny1−x1yn)∣A = \tfrac{1}{2} \cdot \left\lvert (x_1 y_2 - x_2 y_1) + (x_2 y_3 - x_3 y_2) + \ldots + (x_n y_1 - x_1 y_n) \right\rvert

Without the absolute value the result is the signed area: positive when the vertices go counter-clockwise, negative when clockwise. Graphics code uses that sign to tell front faces from back faces and to know which way a path winds. Here the formula is checked against the regular-polygon formula on a hexagon of side 2:

python · try it yourself
import math

def signed_area(pts):
    s = 0.0
    for i in range(len(pts)):
        x1, y1 = pts[i]
        x2, y2 = pts[(i + 1) % len(pts)]
        s += x1 * y2 - x2 * y1
    return s / 2

n, side = 6, 2
r = side / (2 * math.sin(math.pi / n))            # centre-to-vertex distance
hexagon = [(r * math.cos(2 * math.pi * k / n), r * math.sin(2 * math.pi * k / n)) for k in range(n)]
apothem = side / (2 * math.tan(math.pi / n))
print("shoelace:      ", round(signed_area(hexagon), 6))
print("½·P·a:         ", round(0.5 * n * side * apothem, 6))
print("clockwise list:", round(signed_area(hexagon[::-1]), 6))
shoelace:       10.392305
½·P·a:          10.392305
clockwise list: -10.392305

Both give 63≈10.3926\sqrt{3} \approx 10.392. Reversing the vertex order flips the sign but not the size.

Point in polygon

Is the mouse cursor inside this map region? The ray-casting test sends a ray from the point to the right and counts how many edges it crosses. An odd count means inside, even means outside. It works for concave shapes too:

python · try it yourself
import matplotlib.pyplot as plt

def inside(p, poly):
    x, y = p
    result = False
    for i in range(len(poly)):
        x1, y1 = poly[i]
        x2, y2 = poly[(i + 1) % len(poly)]
        if (y1 > y) != (y2 > y):                         # edge straddles the ray's height
            x_cross = x1 + (y - y1) * (x2 - x1) / (y2 - y1)
            if x < x_cross:
                result = not result
    return result

shape = [(0, 0), (6, 0), (6, 4), (3, 2), (0, 4)]         # a rectangle with a notch on top
tests = [(1, 1), (3, 3), (5, 3), (7, 1)]
plt.figure(figsize=(6, 4))
plt.fill(*zip(*shape), alpha=0.3)
for p in tests:
    ok = inside(p, shape)
    plt.plot(*p, "o", color="tab:green" if ok else "tab:red")
    print(p, "inside" if ok else "outside")
plt.axis("equal")
plt.title("Ray casting: green = inside")
(1, 1) inside
(3, 3) outside
(5, 3) inside
(7, 1) outside

The point (3,3)(3, 3) sits in the notch, so its ray crosses two edges and is outside. The condition (y1 > y) != (y2 > y) skips horizontal edges and avoids counting a shared vertex twice.

In software

  • Hit testing: clickable map regions, SVG shapes and game zones use point-in-polygon.
  • Area: GIS software computes land parcel areas with shoelace; image tools measure selections.
  • Meshes: 3D models are polygons (usually triangles), and the winding sign decides which side faces the camera. See Collision Geometry and Convex Hulls.

Exercises

Exercise 1: Octagon angles

Find the interior angle sum of an octagon and each interior angle of a regular octagon.

Answer

1080° and 135°.

Solution
sum=(8−2)⋅180∘=1080∘each=1080∘/8=135∘\begin{aligned} \text{sum} &= (8 - 2) \cdot 180^\circ = 1080^\circ \\ \text{each} &= 1080^\circ / 8 = 135^\circ \end{aligned}

Exercise 2: How many sides?

Each exterior angle of a regular polygon is 24°. How many sides does it have, and what is each interior angle?

Answer

15 sides; each interior angle is 156°.

Solution
exterior angles add to 360∘:n=360∘/24∘=15interior=180∘−24∘=156∘(check: 13⋅180/15=156)\begin{aligned} &\text{exterior angles add to } 360^\circ{:}\quad n = 360^\circ / 24^\circ = 15 \\ &\text{interior} = 180^\circ - 24^\circ = 156^\circ \quad (\text{check: } 13 \cdot 180 / 15 = 156) \end{aligned}

Exercise 3: Diagonals of a decagon

How many diagonals does a decagon have?

Answer

35

Solution
n(n−3)/2=10⋅7/2=35n(n - 3)/2 = 10 \cdot 7 / 2 = 35

Exercise 4: Shoelace by hand

Find the area of the quadrilateral with vertices (1,0)(1, 0), (4,1)(4, 1), (3,4)(3, 4), (0,3)(0, 3). Is the list counter-clockwise?

Answer

Area 10; yes, counter-clockwise (the signed sum is positive).

Solution
x1y2−x2y1=1⋅1−4⋅0=1x2y3−x3y2=4⋅4−3⋅1=13x3y4−x4y3=3⋅3−0⋅4=9x4y1−x1y4=0⋅0−1⋅3=−3sum=20>0 (counter-clockwise),A=20/2=10\begin{aligned} x_1 y_2 - x_2 y_1 &= 1 \cdot 1 - 4 \cdot 0 = 1 \\ x_2 y_3 - x_3 y_2 &= 4 \cdot 4 - 3 \cdot 1 = 13 \\ x_3 y_4 - x_4 y_3 &= 3 \cdot 3 - 0 \cdot 4 = 9 \\ x_4 y_1 - x_1 y_4 &= 0 \cdot 0 - 1 \cdot 3 = -3 \\ \text{sum} &= 20 > 0 \ (\text{counter-clockwise}), \qquad A = 20 / 2 = 10 \end{aligned}

The shape is in fact a square with side 32+12=10\sqrt{3^2 + 1^2} = \sqrt{10}, and (10)2=10(\sqrt{10})^2 = 10.

Exercise 5: Points on the edge

Run inside((6, 2), shape) and inside((0, 2), shape) with the notched shape above. Both points lie on the boundary. What do you get, and why might a game not care?

Answer

False and True: boundary points are not handled consistently.

Solution

For (0,2)(0, 2) the left edge is at x=0x = 0, and the strict test x < x_cross (0<00 < 0) does not count it. The ray then touches the notch's tip (3,2)(3, 2), counted twice (once per edge, which is correct for a touch), and crosses the right edge at x=6x = 6: three counts, odd, so "inside". For (6,2)(6, 2) every crossing is at x≤6x \le 6, so nothing is counted and it reports "outside". Points exactly on an edge are rare with floating-point coordinates, so games accept either answer; CAD software adds an explicit "on the edge" check.

python · try it yourself
def inside(p, poly):
    x, y = p
    result = False
    for i in range(len(poly)):
        x1, y1 = poly[i]
        x2, y2 = poly[(i + 1) % len(poly)]
        if (y1 > y) != (y2 > y):
            if x < x1 + (y - y1) * (x2 - x1) / (y2 - y1):
                result = not result
    return result

shape = [(0, 0), (6, 0), (6, 4), (3, 2), (0, 4)]
print(inside((6, 2), shape), inside((0, 2), shape))
False True

Test yourself

What is the sum of the interior angles of a hexagon?

  1. 540°
  2. 720°
  3. 900°
  4. 360°

The shoelace formula without the absolute value returns a negative number. What does that tell you?

  1. The polygon is concave
  2. The vertices are listed clockwise
  3. The polygon has crossing sides
  4. The calculation overflowed

In ray casting, a ray from a point crosses the polygon's edges 3 times. Where is the point?

  1. Outside
  2. On a vertex
  3. Inside
  4. It cannot be decided