Convert cartesian coordinates to baricentric coordinates

i have a (regular) triangle populated with points. is it possible to convert the cartesian coordinates of the points into baricentric coordinates relativ to the trinagle?


cartesian2baricentric.gh (17.3 KB)

It is an odd omission that GH includes a native component to convert from barycentric to cartesian but not the other way around.
However, you can get the MeshParameter value of a point relative to a mesh, which includes the barycentric coordinate, so one way without scripting or plugins is to make a single mesh triangle and evaluate the closest point on that, then parse the values.
cartesian_to_barycentric.gh (14.7 KB)
You could also use this function:

double[] Barycentric(Point3d p, Point3d a, Point3d b, Point3d c)
  {
    Vector3d v0 = b - a, v1 = c - a, v2 = p - a;
    double d00 = v0 * v0;
    double d01 = v0 * v1;
    double d11 = v1 * v1;
    double d20 = v2 * v0;
    double d21 = v2 * v1;
    double denom = d00 * d11 - d01 * d01;
    double u,v,w;
    v = (d11 * d20 - d01 * d21) / denom;
    w = (d00 * d21 - d01 * d20) / denom;
    u = 1.0 - v - w;
    return new double[3]{u, v, w};
  }

(from the first answer here)

thanks a lot, david!
for the explanation, examples and link …