Interpolating Across a Square and a Triangle
To blend values across a square, lerp along two opposite edges and then lerp between the results: that's bilinear interpolation, and each corner's weight is the area of the rectangle across from it. In a triangle, every point is a weighted average of the three corners, and its weights, the barycentric coordinates, are areas too.
What the symbols mean
- Across
How far across the cell: 0 at the left edge, 1 at the right.
- Up
How far up the cell: 0 at the bottom edge, 1 at the top.
- Corner value
The value stored at corner (0, 0). Likewise f₁₀ at (1, 0), f₀₁ at (0, 1) and f₁₁ at (1, 1).
What the symbols mean
- Weight of A
How much corner A counts at P.
- Weight of B
How much corner B counts at P.
- Weight of C
How much corner C counts at P.
Explanation
Bilinear interpolation is simply lerp done twice to fill in a square. You know a value at the four corners of a grid cell (the heights of four terrain points, the colors of four neighboring pixels in a texture) and you want a sensible value anywhere inside. Lerp across the bottom edge, lerp across the top edge, then lerp between those two answers going up. The name bi-linear means linear in each direction separately: hold fixed and it's a lerp in ; hold fixed and it's a lerp in . The four-term formula looks busy, but each term is just a corner value times its weight, like for , and the four weights add up to . The triangle version, barycentric coordinates, is the same idea with three corners instead of four.
Two lerps, then one. With and the position inside the cell (each from to ),
Multiply it out and you get the four-term formula. Notice it's symmetric in and , so you could just as well lerp up the left and right edges first and then across; the answer is the same. To find and from a world position, use inverse lerp on each axis of the cell.
The weights are areas. Look at the weight of , . It's the area of the rectangle between the point and the opposite corner . Every corner works this way: its weight is the area of the rectangle diagonally across from it. That makes sense: the closer the point gets to a corner, the bigger the rectangle across from that corner, and the more that corner counts. The four rectangles tile the whole unit cell, so their areas add to . There's an animation further down that shows the four rectangles changing as the point moves.
It isn't a plane. Collect the terms:
Unless the four corners happen to lie on one plane, that term is there, and the surface is a gently twisted sheet (a piece of a saddle, a hyperbolic paraboloid). It's straight along every line parallel to an edge, since those are lerps, but curved along the diagonals: on the line the value is quadratic in . There's a 3D animation further down that builds the sheet out of straight lerps.
Barycentric coordinates in a triangle. 3D models are made of triangles, and graphics cards shade each one by blending what's stored at its three corners: colors, lighting directions, texture positions. Any point in the plane of a triangle can be written as
and are its barycentric coordinates. Barycenter means center of mass: is the balance point when masses , and sit on the corners. Once you have the weights, any value stored at the corners blends the same way, . On a segment this is plain lerp: has the two weights and .
Computing the weights. Each weight is an area ratio, the triangle across from its corner over the whole triangle: , and so on around. Here's why. is everywhere on the edge and at , and it changes linearly, so it measures how far is from the line as a fraction of how far is. The triangles and share the base , and 's height is times 's, so its area is times as big. To compute areas, use the 2D cross product (the -part of the cross product): for and , , and the triangle has signed area , positive when go counterclockwise. The 2 in the ½ cancels in every ratio.
// 2D cross product: (ax, ay) x (bx, by) = ax*by - ay*bx
const cross = (ax, ay, bx, by) => ax * by - ay * bx;
function barycentric(P, A, B, C) {
const whole = cross(B.x - A.x, B.y - A.y, C.x - A.x, C.y - A.y);
const alpha = cross(B.x - P.x, B.y - P.y, C.x - P.x, C.y - P.y) / whole;
const beta = cross(C.x - P.x, C.y - P.y, A.x - P.x, A.y - P.y) / whole;
return [alpha, beta, 1 - alpha - beta];
}
The inside test. Signed areas keep track of which side of an edge is on. When crosses the edge , the triangle flips over, its signed area turns negative, and so does . So is inside the triangle exactly when all three weights are . That one test decides which pixels a triangle covers when it's drawn, which triangle the mouse clicked on, and which triangle of a walkable floor a character is standing on.
A common mistake. Weighting corners by distance, giving closer corners more say with something like . It sounds reasonable, but it doesn't blend linearly: halfway along the edge , corner should have no say at all, yet its distance there is finite, so a distance-based weight still gives it some. Areas get it right: the triangle across from is flat on that edge, so .
Another one. Thinking bilinear interpolation is flat. The center of a cell is always the plain average of its four corners, but the values along a diagonal follow a parabola, not a line, and a cell split into two triangles (as physics engines and many terrain systems do) gives a different surface from the bilinear one. The challenge problem measures the difference.
Both ideas are lerps underneath, like everything since Lerp: Blending Between Two Values, and they connect to the matrices of computer graphics: a transformation moves the triangle's corners, and barycentric weights carry everything else along. Next, lerps of lerps make Bézier curves.
See it
Worked examples
A terrain heightmap stores heights every m. One cell has corner heights m (southwest corner), m (southeast), m (northwest) and m (northeast). A character stands m east and m north of the southwest corner. How high is the ground under them?
- 1
Turn the position into cell coordinates with inverse lerp: of the way east, and of the way north.
A triangle has corners , and , with brightness , and stored at them. Find the barycentric coordinates of , decide whether is inside, and find its blended brightness.
- 1
Start with the whole triangle. . That's twice the area, so . It's positive, so , , run counterclockwise.
Practice
Type your answer and check it. Solve them all to complete the lesson.
A unit cell has corner values , , and . Find the bilinear value , where the first coordinate is and the second is .
A heightmap cell covers from to m and from to m, with ground heights m at , m at , m at and m at . How high is the ground, in meters, at ?
A unit cell has corner values , , and . Write the bilinear interpolant , with across and up, multiplied out and simplified.
Find the barycentric coordinates of in the triangle , , .
For the triangle , , and the point , find , the barycentric weight of . Is inside the triangle?
One triangle of a terrain mesh has corners at height m, at m and at m. A character stands at . Using barycentric interpolation, how high is the ground there, in meters?
Find the barycentric coordinates of in the triangle , , .
The hardest one here. Get it, and you've got this lesson.
A terrain cell on the unit square has corner heights , , and . The physics engine splits the cell into two triangles along the diagonal from to and interpolates each with barycentric coordinates, while the renderer draws the bilinear surface. At the point , how much higher is the physics surface than the drawn one?
Where it's used
Every textured 3D game samples its textures bilinearly, or trilinearly (bilinear on two sizes of the texture and a lerp between them), millions of times per frame, which is why a texture seen up close looks soft rather than blocky. Terrain engines read heights from heightmaps with bilinear lookups, and weather and ocean models interpolate their gridded data the same way. Barycentric coordinates are how graphics cards spread colors, lighting directions and texture positions across each triangle (with a correction for perspective), how ray tracers report exactly where a ray hit a triangle, and how games work out which triangle of a walkable floor a character stands on.