Sub topic 5
Triangles, Quads and N-gons
Assume you know basic 3D coordinates, vectors, cross products, and Python lists. This chapter focuses on polygon types and the conversion from polygons to triangles for rendering.
Triangles: the reliable primitive
A triangle is a face with exactly three vertices. Three non-collinear points always define one plane, so a triangle is planar by definition. Its surface is unambiguous, and its edges fully determine its shape.
Graphics hardware ultimately rasterizes triangles. A rasterizer is the part of a renderer that determines which screen pixels a primitive covers. Triangles work well because the renderer can interpolate values such as depth, color, normals, and texture coordinates across their surface.
A triangle is usually stored as three vertex indices:
triangle = (4, 7, 2)The numbers refer to entries in a shared vertex list. The order also matters. It determines the face's winding and therefore its front-facing direction.
Quads: convenient modeling surfaces
A quad is a face with four vertices. Quads are useful when modeling surfaces that have a grid-like structure, such as a character's torso or a subdivided plane. They often make edge loops easier to select and edit.
A quad is not always perfectly planar. Four arbitrary points can form a twisted surface. Even a planar quad must eventually be divided into two triangles:
3---------2
|\ |
| \ |
| \ |
| \ |
0---------1The diagonal from vertex 0 to vertex 2 is one possible split. The other diagonal, from vertex 1 to vertex 3, may produce different triangles. On a flat, convex quad, both choices usually look equivalent. On a twisted quad, they produce different geometry.
A renderer may choose the diagonal itself, or an exporter may triangulate the quad before writing the model. This means a quad that looks acceptable in a modeling tool can show a visible crease after export if its two possible triangulations produce different normals.
N-gons: flexible but conditional
An n-gon is a polygon with more than four vertices. A pentagon and a twelve-sided polygon are both n-gons. N-gons are convenient for quickly describing a broad surface, but they impose more conditions on triangulation.
For predictable results, an n-gon should usually be:
- Planar, so all vertices lie on one plane.
- Convex, so every diagonal stays inside the polygon.
- Simply connected, with no holes or self-intersections.
- Consistently wound, so its front side is clear.
A concave polygon has an inward angle greater than 180 degrees. Some diagonals of a concave polygon leave the polygon's interior. A renderer cannot use those diagonals as valid triangles.
How triangulation works
Triangulation divides a polygon into triangles without adding gaps or overlaps. A convex polygon with n vertices always produces n - 2 triangles. A quad produces two triangles, and a pentagon produces three.
The simplest method for a convex polygon is a triangle fan. Keep the first vertex fixed, then connect it to each pair of consecutive vertices:
def triangulate_fan(vertex_indices):
"""Triangulate a convex polygon using its first vertex."""
if len(vertex_indices) < 3:
raise ValueError("A face needs at least three vertices")
first = vertex_indices[0]
return [
(first, vertex_indices[i], vertex_indices[i + 1])
for i in range(1, len(vertex_indices) - 1)
]
print(triangulate_fan([0, 1, 2, 3, 4]))
# [(0, 1, 2), (0, 2, 3), (0, 3, 4)]This method is safe for convex, planar polygons. It is not generally safe for concave polygons because a fan diagonal can pass outside the face. Concave polygons need a method such as ear clipping, which repeatedly removes triangles whose interior lies entirely inside the polygon.
Choosing a polygon type
| Polygon type | Strength | Common risk |
|---|---|---|
| Triangle | Always planar and renderable | More edges to edit |
| Quad | Efficient for structured modeling | Can be twisted or triangulated inconsistently |
| N-gon | Fast for describing large regions | Concavity, poor triangulation, and shading artifacts |
For modeling, quads can provide useful control. For interchange and rendering, explicit triangles provide the most predictable result. N-gons are best used when the downstream tools can triangulate them consistently.
Exercise
How many triangles does a convex polygon with seven vertices produce, and why?
Show answer
It produces five triangles because a convex polygon with n vertices produces n - 2 triangles.