how to make the conversion from barycentric coordinates to Cartesian coordinates?

后端 未结 1 863
醉话见心
醉话见心 2021-01-22 23:13

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

here is a piece of code come from somewhere else

imp         


        
1条回答
  •  说谎
    说谎 (楼主)
    2021-01-22 23:48

    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
    

    0 讨论(0)
提交回复
热议问题