# How to make sense of the Barycentric coordinate system from Cartesian coordinate system?

Given a triangle (defined by three points: $p_1$, $p_2$, $p_3$) and a point ($t$) inside a triangle in 3D space. Then, I compute the Barycentric coordinate of point $t$ programmatically like this:

double v0[3], v1[3], v2[3];

vtkMath::Subtract(p1, p2, v0);
vtkMath::Subtract(p3, p2, v1);
vtkMath::Subtract(t, p2, v2);

float d00 = vtkMath::Dot(v0, v0);
float d01 = vtkMath::Dot(v0, v1);
float d11 = vtkMath::Dot(v1, v1);
float denom = d00 * d11 - d01 * d01;

float d20 = vtkMath::Dot(v2, v0);
float d21 = vtkMath::Dot(v2, v1);
u = (d11 * d20 - d01 * d21) / denom;
v = (d00 * d21 - d01 * d20) / denom;
w = 1.0f - u - v;


Now let's say I want to interpolate values (such as vertex normal) that belong to the three point ON the triangle and assign the interpolated value to the point IN the triangle. The question is how do I know which coefficient ($u, v, w$) belongs to which point on the triangle?

• Why are you having to guess? Inherent in the definition of barycentric coordinates is the order of the vertices. – amd Apr 18 '17 at 18:15
• @amd From the above equation can you tell me which coefficient belongs to which vertex? My math is not so good. :) – Bla... Apr 19 '17 at 1:48

You can think of the barycentric co-ordinates of a point p as being the masses that would have to be placed at vertices $p_1, p_2, p_3$ to have an overall centre of mass at p. So the co-ordinates are inversely proportional to the distance from p to each of the points $p_1, p_2, p_3$. Of course, if masses $m_1, m_2, m_3$ produce a centre of mass at p then so will masses $km_1, km_2, km_3$ for any multiple k - so barycentric co-ordinates are by convention normalised so that they add up to 1.
The three points $p_1, p_2, p_3$ have co-ordinates (1,0,0), (0,1,0) and (0,0,1). Any point between $p_1$ and $p_2$ will have co-ordinates (a, 1-a, 0) such that the ratio $\frac{a}{1-a}$ is equal to the ratio of the distance from p to $p_2$ to the distance from p to $p_1$. So the mid-point of the line from $p_1$ to $p_2$ has co-ordinates (1/2, 1/2, 0).
If you draw a line from (a, 1-a, 0) to the third point $p_3$ and let p move along this line, then the co-ordinates of p will change smoothly from (a, 1-a, 0) to (0,0,1) at $p_3$, in such a way that the ratio of the first and second co-ordinates remains constant (since the ratio of distances from $p_2$ and $p_1$ is constant along this line). So the points along the line from (1/2, 1/2, 0) to $p_3$ will have co-ordinates (a, a, 1-2a) where a varies smoothly from 1/2 down to 0, and 1-2a varies smoothly from 0 up to 1.
Alonmg this line, one third of the way to $p_3$, you reach a point with co-ordinates (1/3, 1/3, 1/3). This is the centroid of the triangle - it is the same distance from all three vertices. And because the barycentric co-ordinates of the centroid are all equal, we can see that it must lie at the intersection of the lines from each vertex to the mid-point of the opposite side.
• So how do I know if $u$ relates to $p_1, p_2$ or $p_3$? – Bla... Apr 18 '17 at 13:52
• Set t equal to $p_1$ and see if u=1. If not, try $t=p_2$ and then $t=p_3$. – gandalf61 Apr 18 '17 at 14:02