Initializing 3D Canvas...

Barycentric Coordinates

2 min read1 page

Barycentric coordinates represent any point $P$ within a triangle $ABC$ as a weighted sum of its vertices: $P = u A + v B + w C$, where $u + v + w = 1$.

Sub-Triangle Area Ratio:

1. Coordinate $u$: Ratio of area($PBC$) to total area($ABC$). 2. Coordinate $v$: Ratio of area($PCA$) to total area($ABC$). 3. Coordinate $w$: Ratio of area($PAB$) to total area($ABC$).
python
1def compute_barycentric(p, a, b, c):
2 v0, v1, v2 = b - a, c - a, p - a
3 d00 = dot(v0, v0)
4 d01 = dot(v0, v1)
5 d11 = dot(v1, v1)
6 d20 = dot(v2, v0)
7 d21 = dot(v2, v1)
8
9 denom = d00 * d11 - d01 * d01
10 v = (d11 * d20 - d01 * d21) / denom
11 w = (d00 * d21 - d01 * d20) / denom
12 u = 1.0 - v - w
13 return u, v, w
1 min read1 page

Barycentric coordinates are the basis of fragment shading. By treating $u$, $v$, and $w$ as weights, we linearly interpolate attributes (colors, UV coordinates, normals) across the triangle face.

Attribute blending:

1. Partition of Unity: Because $u + v + w = 1$, any convex combination is guaranteed to lie within the bounds of the vertex attributes. 2. Vertex Blending: Color at $P$ is $u \cdot Color(A) + v \cdot Color(B) + w \cdot Color(C)$.
python
1# Interpolating vertex attributes (e.g. colors, normals) using weights
2def interpolate_attributes(u, v, w, attr_a, attr_b, attr_c):
3 # Linear combination
4 return u * attr_a + v * attr_b + w * attr_c
Point P Position X
0.00