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 -gon and draw every diagonal from it. You cut the polygon into triangles, and each triangle's angles add up to 180°. So:
The diagonal count works because each of the vertices connects to others (not itself, not its two neighbours), and that counts every diagonal twice.
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 -gon splits into identical isosceles triangles meeting at the centre. The height of each triangle is the apothem , the distance from the centre to the middle of a side. With side and perimeter :
The shoelace formula
For any simple polygon (sides do not cross) with vertices listed in order:
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:
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 . 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:
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 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
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
Exercise 3: Diagonals of a decagon
How many diagonals does a decagon have?
Answer
35
Solution
Exercise 4: Shoelace by hand
Find the area of the quadrilateral with vertices , , , . Is the list counter-clockwise?
Answer
Area 10; yes, counter-clockwise (the signed sum is positive).
Solution
The shape is in fact a square with side , and .
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 the left edge is at , and the strict test x < x_cross () does not count it. The ray then touches the notch's tip , counted twice (once per edge, which is correct for a touch), and crosses the right edge at : three counts, odd, so "inside". For every crossing is at , 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.
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?
- 540°
- 720°
- 900°
- 360°
The shoelace formula without the absolute value returns a negative number. What does that tell you?
- The polygon is concave
- The vertices are listed clockwise
- The polygon has crossing sides
- The calculation overflowed
In ray casting, a ray from a point crosses the polygon's edges 3 times. Where is the point?
- Outside
- On a vertex
- Inside
- It cannot be decided