メインコンテンツまでスキップ

barycentric: Barycentric Coordinate Computation

Mathematical Foundation for Triangle Point Interpolation

Barycentric coordinates provide a coordinate system for expressing points relative to a triangle. For any point in the plane of a triangle, barycentric coordinates (u,v,w)(u, v, w) satisfy the constraint u+v+w=1u + v + w = 1, where each component represents the weight of the corresponding triangle vertex.

The mathematical definition involves solving the linear system: P=uA+vB+wCP = u \cdot A + v \cdot B + w \cdot C

Where PP is the target point and AA, BB, CC are triangle vertices. The computation uses dot products to solve for the weights:

daa=AAdab=ABdenom=daadbbdab2v=dbb(CA)dab(CB)denom\begin{align} \text{daa} &= A \cdot A \\ \text{dab} &= A \cdot B \\ \text{denom} &= \text{daa} \cdot \text{dbb} - \text{dab}^2 \\ v &= \frac{\text{dbb} \cdot (C \cdot A) - \text{dab} \cdot (C \cdot B)}{\text{denom}} \end{align}

Two primary functions handle different coordinate computation scenarios:

barycentric(a, b, c) computes normalized barycentric coordinates directly from three vertices. The computation projects vectors onto a coordinate system defined by the triangle edges.

barycentricFromPosition(a, b, c, pos) determines barycentric coordinates of a specific position relative to the triangle using cross product area ratios:

wi=Area(triangle formed by pos and edge i)Area(triangle)w_i = \frac{\text{Area}(\text{triangle formed by pos and edge i})}{\text{Area}(\text{triangle})}

ライブエディター
const fragment = () => Scope(() => {
      const tri = Triangle({
              a: vec3(-0.5, -0.4, 0),
              b: vec3(0.5, -0.4, 0),
              c: vec3(0, 0.6, 0)
      })
      const pos = vec3(uv, 0)
      const bary = barycentricFromPosition(tri, pos)
      return vec4(bary, 1)
})