per wiki, the conversion from barycentric coordinates to Cartesian coordinates is as follow
here is a piece of code come from somewhere else
import numpy as np
import matplotlib.pyplot as plt
# from barycentric coordinates to Cartesian coordinates
a = np.array([0. , 0. , 1. , 0.25, 0.25, 0.5 ])
b = np.array([0. , 1. , 0. , 0.25, 0.5 , 0.25])
c = np.array([1. , 0. , 0. , 0.5 , 0.25, 0.25])
x = 0.5 * ( 2.*b+c ) / ( a+b+c )
y = 0.5*np.sqrt(3) * c / (a+b+c)
plt.scatter(x,y)
plt.show()
it seems that the piece of code is using another formula, if it is, what is the formula?
assume the barycentric coordinates of B is (0,0,1), how to compute its Cartesian coordinates? what lambda_1, lambda_2, lambda_3, x_1, x_2, x_3, y_1, y_2, y_3 are for point B?
Your formula is correct.
Assuming that the three corners of a triangles are encoded as the columns of the matrix t
, here is a simple Python implementation:
import numpy as np
def get_cartesian_from_barycentric(b, t):
return t.dot(b)
b = np.array([0.25,0.3,0.45]) # Barycentric coordinates
t = np.transpose(np.array([[0,0],[1,0],[0,1]])) # Triangle
c = get_cartesian_from_barycentric(b, t)
The formula you found is also calculating Cartesian from barycentric coordinates but uses a predefined regular triangle with the following coordinates:
(x1,y1) = (0,0)
(x2,y2) = (1,0)
(x3,y3) = (1/2,sqrt(3)/2)
In this calculation, the code considers that every column is a point expressed with barycentric coordinates. Thus, it calculates 6 points at once. Furthermore, barycentric coordinates need to be normalized, i.e., lambda1 + lamda2 + lambda3 = 1
. This code does not assume normalization, so it needs to divide by the sum of lambdas to ensure this property. Of course, we can see that the sum is always 1 for all 6 points, but the code could be used for lambdas that do not sum to 1.
In the last example you gave, B is a point of the triangle and is not expressed with barycentric coordinates. P is the point that is expressed with barycentric coordinate relative to the point A, B, and C. Let A = (x1,y1)
, B = (x2,y2)
, and C = (x3,y3)
, and that P has barycentric coordinates (l1,l2,l3)
. Then, the Cartesian coordinates (xp,yp)
of P is
xp = l1*x1 + l2*x2 + l3*x3
yp = l1*y1 + l2*y2 + l3*y3