[Math] Compute weight of a point on a 3D triangle

3dgeometry

Let's say I have a 3D triangle $ABC$ with $x$, a random point on it, I know the coordinates of each one of the points.
Each of $A$, $B$ and $C$ have a "weight" which is a decimal value between 0 and 1 (actually the color value of the point on a black and white gradient). What I want to do is to find the precise weight of $x$, knowing that the weight interpolation between the points is linear.

What formula should I use?

ABC triangle with x point on it

Best Answer

Elaborating on my comment: This webpage describes a good method to interpolate and find the barycentric coordinates of a point inside a triangle, which can easily be used to calculate the 'weight' or color of the point as asked by the OP. The method involves first representing the triangle as a triplet of vectors $(\mathbf{a} = (a_x, a_y), \mathbf{b} = (b_x, b_y), \mathbf{c} = (c_x, c_y))$ and the point as another vector $\mathbf{p} = (p_x, p_y)$. (The linked webpage uses 3-dimensional vectors but the method is generalizable.)

Once the vectors are obtained, we can calculate the areas of the triangles formed by joining the vertices of the triangle to the point inside it. The area of the whole triangle is $$\Delta = \frac{1}{2} \cdot ||\mathbf{a} - \mathbf{b}|| \cdot ||\mathbf{a} - \mathbf{c}||$$ (which is half the magnitude of the cross product of the two vectors). The areas of the triangles opposite vertices $\mathbf{a}$, $\mathbf{b}$, and $\mathbf{c}$, respectively, are $$ \Delta_a = \frac{1}{2} \cdot ||\mathbf{p} - \mathbf{b}|| \cdot ||\mathbf{p} - \mathbf{c}|| \\ \Delta_b = \frac{1}{2} \cdot ||\mathbf{p} - \mathbf{a}|| \cdot ||\mathbf{p} - \mathbf{c}|| \\ \Delta_c = \frac{1}{2} \cdot ||\mathbf{p} - \mathbf{a}|| \cdot ||\mathbf{p} - \mathbf{b}|| $$

Now, we calculate the 'interpolation factors' of the point $\mathbf{p}$ as $k_a = \frac{\Delta_a}{\Delta}$, $k_b = \frac{\Delta_b}{\Delta}$, and $k_c = \frac{\Delta_c}{\Delta}$. The barycentric coordinates of $\mathbf{p}$ are finally given by $$\mathbf{p} = \mathbf{a}k_a + \mathbf{b}k_b + \mathbf{c}k_c$$ (In the linked page, they use 'UV' coordinates, but since we are using 2 dimensions throughout, our 'UV' coordiantes are the same as our 'XY[Z]' coordinates.)

If $w_a$, $w_b$, $w_c$ denote the weights of, or colors of, or amount of cheese contained in, the points $\mathbf{a}$, $\mathbf{b}$, and $\mathbf{c}$, respectively, then $w_p = w_ak_a + w_bk_b + w_ck_c$ is the corresponding quantity for $\mathbf{p}$, assuming the 'linear' interpolation.

Extra: It would be interesting if this technique could be generalized to any polygon, using the areas opposite the vertices or something similar. I don't know much about this, but perhaps someone more knowledgeable can comment.

Edit: The answer to the above is very simple, just use a triangle within the polygon that contains the point. I overlooked that :)