4

per wiki, the conversion from barycentric coordinates to Cartesian coordinates is as follow

enter image description here

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?

enter image description here

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 question is not clear. Are you asking for the purpose of the code in your snippet, or the correct code to use for the conversion from barycentric to Cartesian coordinates, or something else? – Rory Daulton May 27 '19 at 15:04

1 Answers1

4

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
  • thanks for your answer! does a `column` in `every column is a point expressed` mean that (0,1,1/2) is a `column`, and (0,0,sqrt(3)/2) is another `column`? –  May 28 '19 at 00:29
  • @singularli No, I meant that the column of the matrix formed by `a`, `b`, and `c`, i.e., the six points: (0,0,1), (0,1,0), (1,0,0), (1/4,1/4,1/2), (1/4,1/2,1/4), (1/2,1/4,1/4) are the barycentric coordinates of the six points. (0,0), (1,0), and (1/2,sqrt(3)/2) are the 3 points of the triangle. – Gilles-Philippe Paillé May 28 '19 at 00:41
  • thanks for you patience! the column part is clear, still confused about the whole picture. i just updated the question, would you please take point B as an example, elaborate the process of the conversion? –  May 28 '19 at 01:45
  • @singularli I edited the answer to cover the case you mentioned. – Gilles-Philippe Paillé May 28 '19 at 02:23
  • thanks for you explanation! does `... is not expressed with barycentric coordinates` mean (0,0,1) is not the barycentric coordinates of B? –  May 28 '19 at 02:31