Barycentric Coordinates are used in applications of Computer Graphics to determine whether a given point is located within a specific polygon on the plane.
The most common system recognized is the Cartesian Coordinate system that expresses a point's location on the plane relative to the origin (at 0,0).
Barycentric Coordinates and Cartesian coordinates both refer to the same points on the same plane, but rather than express a point's location relative to an origin, Barycentric Coordinates express a point's location relative to other points on the plane.
Let's consider a very simple triangle that is sitting on the 2-dimensional (2D) Cartesian plane with vertices P1 (at 0,0), P2 (at 1,0), and P3 (at 1,1)
Suppose we want to locate the coordinates of the point in the center of the triangle with respect to the origin.
We could express this in terms of Cartesian coordinates by finding the average of the x,y values:
Pcenter = ((X1 + X2 + X3) / 3, (Y1 + Y2 + Y3) / 3) = ((0 + 1 + 1) / 3, (0 + 0 + 1) / 3) = (2/3, 1/3)
This tells us "Go 2/3 to the right of the origin and up 1/3 from the origin."
We could also re-write the above calculations as:
= (1/3)X1 + (1/3)X2 + (1/3)X3
(1/3)Y1 + (1/3)Y2 + (1/3)Y3
This gives us a "system" consisting of two equations for the x and y values. To keep it easier to read, let's rewrite the system as a single equation:
Pcenter = (1/3)P1 + (1/3)P2 + (1/3)P3
Note: You may be wondering "What does it mean to multiply a Point by a scalar value (1/3)?" What is assumed is that the Point is a "vector" of 2 values (x,y). So multiplying a point by a scalar is the same as multiplying a matrix by a scalar value. Both the x and y values are multiplied by the scalar.
We have now expressed the location of the center of the triangle as a Barycentric combination of the triangle's vertices.
Using what we have discovered, we can hypothesize the following:
To express the Point P4 as a combination of the vertices of a triangle whose vertices are P1, P2, P3:
P4 = (U)(P1) + (V)(P2) + (W)(P3)
Where the 3-tuple (U,V,W) are the Barycentric Coordinates of P4.
Applying this definition to our previous example of the triangle's center, we see that the Barycentric Coordinates for the point at the center of the triangle are (1/3, 1/3, 1/3)
Another way of looking at it is to think of the Barycentric Coordinates as "weights" applied to the vertices of the triangles where U is the weight of P1, V is the weight of P2, and W is the weight of P3.
Therefore, we say that the sum of the weights must add up to be 1. Why 1? Because 1 implies the ratio 100% (just like .99 implies 99%)
Therefore, if P0 gets 100% of the weight then points P1 and P2 get 0% of the weight.
Likewise, if P1 gets 100% of the weight then points P0 and P2 get 0% of the weight.
Likewise, if P2 gets 100% of the weight then points P0 and P1 get 0% of the weight.
We can use this property to find the Barycentric Coordinates of the triangle's vertices.
P0 = 1(P0) + 0(P1) + 0(P2) so P0's Barycentric Coordinates are: (1,0,0)
P1 = 0(P0) + 1(P1) + 0(P2) so P1's Barycentric Coordinates are: (0,1,0)
P2 = 0(P0) + 0(P1) + 1(P2) so P2's Barycentric Coordinates are: (0,0,1)
OK, great, so we can see the Barycentric Coordinates of the vertices and the centroid (center of the triangle). So how do we calculate the Barycentric Coordinates of an arbitrary point in the plane with respect to some triangle?
Here we go.
We are given the triangle whose vertices are at P1, P2, and P3. We are given a fourth point P4 that may or may not be within the triangle. Find the Barycentric Coordinates of P4.
We start with our definitions:
U(P1) + V(P2) + W(P3) = P4
U + V + W = 1
We know (from above) that the first equation is actually short-hand for 2 equations. So let's go ahead and decompose it back into 2 equations and our resulting system is:
U(X1) + V(X2) + W(X3) = X4
U(Y1) + V(Y2) + W(Y3) = Y4
U + V + W = 1
If you are familiar with Linear Algebra, you will see that what we have here is a system of linear equations with known values X1, X2, X3, X4, Y1, Y2, Y3, Y4 and unknown values U, V, W which just happen to be the Barycentric Coordinates that we are trying to calculate. Thus, we will figure out the Barycentric Coordinates by finding the solution to the system.
The 3 equations that form our linear system:
(U)(X1) + (V)(X2) + (W)(X3) = X4
(U)(Y1) + (V)(Y2) + (W)(Y3) = Y4
U + V + W = 1
Can be re-written in matrix notation (using knowledge of Linear Algebra) as:
---------------------
| X1 | X2 | X3 | X4 |
---------------------
| Y1 | Y2 | Y3 | Y4 |
---------------------
| 1 | 1 | 1 | 1 |
---------------------
To obtain the solution set, we shall use Cramer's Rule which is defined as:
Xi = detAi(b) / detA
We can use this formula to solve for unknowns U, V, and W:
U = detA1(b) / detA
V = detA2(b) / detA
W = detA3(b) / detA
We first solve detA as the determinate of A's coefficient matrix:
----------------
| X1 | X2 | X3 |
----------------
detA = det | Y1 | Y2 | Y3 | = (X1 * Y2) - (X1 * Y3) - (X2 * Y1) + (X2 * Y3) + (X3 * Y1) - (X3 * Y2)
----------------
| 1 | 1 | 1 |
----------------
We then apply Cramer's rule to find detA1(b), detA2(b), and detA3(b):
----------------
| X4 | X2 | X3 |
----------------
detA1(b) = det | Y4 | Y2 | Y3 | = (X4 * Y2) - (X4 * Y3) - (X2 * Y4) + (X2 * Y3) + (X3 * Y4) - (X3 * Y2)
----------------
| 1 | 1 | 1 |
----------------
----------------
| X1 | X4 | X3 |
----------------
detA2(b) = det | Y1 | Y4 | Y3 | = (X1 * Y4) - (X1 * Y3) - (X4 * Y1) + (X4 * Y3) + (X3 * Y1) - (X3 * Y4)
----------------
| 1 | 1 | 1 |
----------------
----------------
| X1 | X2 | X4 |
----------------
detA3(b) = det | Y1 | Y2 | Y4 | = (X1 * Y2) - (X1 * Y4) - (X2 * Y1) + (X2 * Y4) + (X4 * Y1) - (X4 * Y2)
----------------
| 1 | 1 | 1 |
----------------
Finally, the solution set is resolved as:
U = detA1(b) \ detA
= ((X4 * Y2) - (X4 * Y3) - (X2 * Y4) + (X2 * Y3) + (X3 * Y4) - (X3 * Y2)) \
((X1 * Y2) - (X1 * Y3) - (X2 * Y1) + (X2 * Y3) + (X3 * Y1) - (X3 * Y2))
V = detA2(b) \ detA
= ((X1 * Y4) - (X1 * Y3) - (X4 * Y1) + (X4 * Y3) + (X3 * Y1) - (X3 * Y4)) \
((X1 * Y2) - (X1 * Y3) - (X2 * Y1) + (X2 * Y3) + (X3 * Y1) - (X3 * Y2))
W = detA3(b) \ detA
= ((X1 * Y2) - (X1 * Y4) - (X2 * Y1) + (X2 * Y4) + (X4 * Y1) - (X4 * Y2)) \
((X1 * Y2) - (X1 * Y3) - (X2 * Y1) + (X2 * Y3) + (X3 * Y1) - (X3 * Y2))
Note: Given a triangle with vertices at points P1 (at X1, Y1), P2 (at X2, Y2), and P3 (at X3, Y3), we can determine whether or not an arbitrary point P4 (at X4,Y4) is within the triangle bounded by P1, P2, P3 by calculating the Barycentric Coordinates of P4. If all of P4's calculated Barycentric Coordinates are positive then the point must be within the triangle. If any of the coordinates are negative then the point must lie outside the triangle.
Let's try our example from the beginning and see if it really works or not.
Given a triangle whose vertices are given as: P1 (0,0), P2 (1,0), and P3 (1,1).
Find the Barycentric Coordinates of point P4 (2/3, 1/3)
Note: Recall from earlier that the point (2/3, 1/3) should be the center of the triangle, so we should expect to calculate a Barycentric Coordinates of (1/3, 1/3, 1/3).
Let's see what happens.
Ok, we know that:
U = detA1(b) \ detA
V = detA2(b) \ detA
W = detA3(b) \ detA
Let's first calculate the common denominator (detA)
detA = ((X1 * Y2) - (X1 * Y3) - (X2 * Y1) + (X2 * Y3) + (X3 * Y1) - (X3 * Y2))
= ((0 * 0) - (0 * 1) - (1 * 0) + (1 * 1) + (1 * 0) - (1 * 0))
= 1
Solve U
detA1(b) = ((X4 * Y2) - (X4 * Y3) - (X2 * Y4) + (X2 * Y3) + (X3 * Y4) - (X3 * Y2))
= ((2/3 * 0) - (2/3 * 1) - (1 * 1/3) + (1 * 1) + (1 * 1/3) - (1 * 0))
= - 2/3 - 1/3 + 1 + 1/3
= - 2/3 + 1
= 1/3
U = detA1(b) \ detA
=(1/3) / 1
= 1/3 Matches what we expected.
Solve V
detA2(b) = ((X1 * Y4) - (X1 * Y3) - (X4 * Y1) + (X4 * Y3) + (X3 * Y1) - (X3 * Y4))
= ((0 * (1/3)) - (0 * 1) - ((2/3) * 0) + ((2/3) * 1) + (1 * 0) - (1 * (1/3)))
= (2/3) - (1/3)
= 1/3
V = detA2(b) \ detA
=(1/3) / 1
= 1/3 Matches what we expected.
Solve W
detA3(b) = ((X1 * Y2) - (X1 * Y4) - (X2 * Y1) + (X2 * Y4) + (X4 * Y1) - (X4 * Y2))
= ((0 * 0) - (0 * (1/3)) - (1 * 0) + (1 * (1/3)) + ((2/3) * 0) - ((2/3) * 0))
= 1/3
W = detA3(b) \ detA
=(1/3) / 1
= 1/3 Matches what we expected.
Thus, we conclude that the Barcentric Coordinates of P4 are (1/3, 1/3, 1/3).