Sub topic 5
Triangles, Quads and N-gons
A face in a polygon mesh is a flat polygon, and the number of vertices it uses has real consequences. This chapter compares triangles, quads and n-gons, explains why renderers end up with triangles, and shows where each type causes trouble. It assumes you know what a vertex, an edge and a face are, how an indexed face set stores faces as lists of vertex indices, and the basics of the cross product. You should also be able to read short Python programs.
The three face types
Faces are named by their vertex count. The same count also gives the number of edges, because a face is a closed loop.
| Type | Vertices per face | Always planar? | Always convex? | Typical use |
|---|---|---|---|---|
| Triangle | 3 | Yes | Yes | Rendering, games, scanned data |
| Quad | 4 | No | No | Character and surface modeling |
| N-gon | 5 or more | No | No | Flat caps, hard-surface shortcuts |
A planar face has all its vertices in one plane. A convex face has no inward-pointing corner, so a line between any two points inside it stays inside it.
Three points always define a plane, so a triangle is flat by construction. A triangle also cannot be concave. Every property a renderer wants, such as one well-defined normal, a simple interior and a cheap inside-test, holds for triangles with no extra checks. Four or more points can be bent out of a plane, and a polygon with four or more corners can fold inward.
Counting faces by kind
Because faces are plain index tuples, their length tells you their type. This example classifies the faces of a mesh made of quads, a triangle and a pentagon.
from collections import Counter
def face_kinds(faces):
names = {3: "triangle", 4: "quad"}
return Counter(names.get(len(f), "n-gon") for f in faces)
faces = [
(0, 1, 2, 3), # quad
(4, 5, 6, 7), # quad
(0, 1, 5), # triangle
(0, 1, 2, 3, 4), # pentagon
]
print(face_kinds(faces))
# Counter({'quad': 2, 'triangle': 1, 'n-gon': 1})Many tools run a check like this before exporting. A game engine importer may reject anything but triangles. A subdivision modeler may warn when it finds n-gons.
Why renderers work with triangles
Graphics hardware rasterizes triangles. It takes three projected vertices and fills the pixels between them. The reasons are practical:
- The three vertices are always coplanar, so the surface is unambiguous.
- Interpolating values such as depth, color or UV coordinates across the face has exactly one correct answer, using barycentric coordinates (weights of the three corners).
- The face is always convex, so the fill algorithm needs no special cases.
A quad or n-gon is therefore turned into triangles before drawing. That step is called triangulation, and a later chapter covers it in full. This chapter only needs the counting rule.
A polygon with n vertices splits into exactly n − 2 triangles. A triangle gives 1, a quad gives 2, and a hexagon gives 4. A simple way to split a convex face is a fan, which connects the first vertex to every non-adjacent vertex pair.
def fan_triangulate(face):
"""Split a convex face into triangles that share face[0].
Winding order is preserved."""
return [(face[0], face[i], face[i + 1]) for i in range(1, len(face) - 1)]
print(fan_triangulate((4, 5, 6, 7))) # [(4, 5, 6), (4, 6, 7)]
print(fan_triangulate((0, 1, 2, 3, 4))) # [(0, 1, 2), (0, 2, 3), (0, 3, 4)]The triangles keep the original counter-clockwise order, so their normals match the original face's normal. A cube of 6 quads becomes 12 triangles. In a closed all-triangle mesh each edge is shared by exactly two faces, so 3F = 2E. The triangulated cube has 12 faces and 18 edges, which fits.
Why modelers prefer quads
If renderers want triangles, why do artists model in quads? The reasons are about editing and deformation, not drawing.
- Edge loops. In a pure quad mesh, edges continue straight through each vertex that has four edges, forming loops and rings. Tools select, slide, insert or delete a whole loop in one action. Triangles do not form clean loops.
- Subdivision. Catmull-Clark subdivision splits every face into quads, and it behaves best when the base mesh is already mostly quads.
- Deformation. When a character's elbow bends, loops that follow the muscle and joint flow let the surface fold cleanly.
- Readable shape. Four-sided patches are easy to see and edit by hand.
A vertex with a number of edges other than four in a quad mesh is called a pole. Poles with three or five edges are unavoidable on most shapes, since a quad grid cannot wrap a sphere without them. Artists place poles where they do the least damage, such as on a flat area that does not deform.
N-gons: useful and risky
An n-gon is a face with five or more vertices. They are convenient in a few cases. A flat circular cap on a cylinder is a single n-gon instead of a fan of thin triangles. A flat panel with a hole cut in it is easier to select as one face.
They carry risks:
- Concavity. Fan triangulation from the wrong start vertex can produce triangles that overlap or point the wrong way.
- Non-planarity. The face has no single normal, and the triangulation you get changes the visible shape.
- Poor subdivision. Catmull-Clark turns an n-gon into n quads that meet at a pole, which tends to produce pinching.
- Tool differences. Different exporters triangulate n-gons differently, so the same file can shade differently in two programs.
The L-shaped hexagon below is concave at vertex 3. This code checks each possible fan start by computing the signed area of every triangle in the fan. A positive area means the triangle has the same counter-clockwise orientation as the polygon. A negative area means the triangle is flipped.
def signed_area(a, b, c):
"""Positive when a, b, c turn counter-clockwise (2D)."""
return ((b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])) / 2
L = [(0, 0), (2, 0), (2, 1), (1, 1), (1, 2), (0, 2)] # counter-clockwise
def fan_areas(poly, start):
r = poly[start:] + poly[:start]
return [signed_area(r[0], r[i], r[i + 1]) for i in range(1, len(r) - 1)]
for start in range(len(L)):
ok = all(a > 0 for a in fan_areas(L, start))
print(start, ok)
# 0 True
# 1 False
# 2 False
# 3 True
# 4 False
# 5 FalseOnly starts 0 and 3 work. Vertex 3 is the concave corner, and it can see every other vertex. Vertex 0 happens to see all of them as well. A naive fan from vertex 1 would create a triangle that is flipped and covers area outside the shape. General triangulation of concave polygons needs a method such as ear clipping, which is the topic of a later chapter.
Non-planar quads
A quad whose four vertices do not lie in one plane has no single correct surface. The two triangulations, split along one diagonal or the other, give different shapes. This function measures how far the fourth vertex sits from the plane of the first three.
def planarity_error(vertices, face):
"""Distance of the fourth vertex from the plane through the first three."""
p0, p1, p2, p3 = (vertices[i] for i in face[:4])
e1 = tuple(b - a for a, b in zip(p0, p1))
e2 = tuple(b - a for a, b in zip(p1, p2))
n = (
e1[1] * e2[2] - e1[2] * e2[1],
e1[2] * e2[0] - e1[0] * e2[2],
e1[0] * e2[1] - e1[1] * e2[0],
)
length = sum(c * c for c in n) ** 0.5
d = tuple(b - a for a, b in zip(p0, p3))
return abs(sum(nc * dc for nc, dc in zip(n, d))) / length
flat = [(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0)]
bent = [(0, 0, 0), (1, 0, 0), (1, 1, 0.3), (0, 1, 0)]
print(round(planarity_error(flat, (0, 1, 2, 3)), 3)) # 0.0
print(round(planarity_error(bent, (0, 1, 2, 3)), 3)) # 0.287The bent quad is off the plane by about 0.287 units. Real pipelines compare this value to a small tolerance and either warn or triangulate the face explicitly. The cause is usually moving one vertex of a quad while leaving the other three in place.
Choosing a face type
The usual workflow follows this chart. Model in quads and use n-gons sparingly on flat regions. Triangulate at export time, or let the engine do it, so the editing mesh stays clean and the render mesh is safe.
Common errors
- Assuming imports are quads. Scanned and CAD-derived meshes are often all triangles, so loop-based tools behave differently on them.
- Triangulating before modeling is finished. Triangulation discards the loop structure that editing tools rely on.
- Trusting automatic triangulation of non-planar faces. The chosen diagonal changes the shape and the shading.
- Using n-gons on deforming areas. They pinch and shade unevenly when animated.
- Counting faces instead of triangles. Budgets and performance depend on the triangle count.
Exercise
A closed mesh has 300 triangles, 500 quads and 20 hexagons. How many triangles will it contain after triangulation?
Show answer
A triangle gives 1, a quad gives 2 and a hexagon gives 6 − 2 = 4. The total is 300 + 500 × 2 + 20 × 4 = 300 + 1000 + 80 = 1380 triangles.
Exercise
Why does fan triangulation from vertex 1 fail for the L-shaped polygon in this chapter, while a fan from vertex 3 works?
Show answer
Vertex 3 is the concave (reflex) corner, and it can see every other vertex through the polygon's interior, so every triangle of its fan lies inside the shape. From vertex 1, the triangle (1, 3, 4) has a negative signed area, so it is flipped and covers area outside the polygon.
Exercise
A quad has vertices (0, 0, 0), (2, 0, 0), (2, 2, 0) and (0, 2, 1). Is it planar? Say which tool from this chapter you would use and what result to expect.
Show answer
It is not planar. The first three points lie in the plane z = 0, and the fourth is 1 unit above it, so planarity_error would return 1.0. The quad needs to be fixed or split into two triangles.