Barycentric coordinates

From vegard.wiki
Revision as of 13:36, 29 December 2019 by Vegard (talk | contribs)
Jump to navigation Jump to search

Conversion from cartesian

// https://gamedev.stackexchange.com/a/23745
// Compute barycentric coordinates (u, v, w) for
// point p with respect to triangle (a, b, c)
void Barycentric(Point p, Point a, Point b, Point c, float &u, float &v, float &w)
{
    Vector v0 = b - a, v1 = c - a, v2 = p - a;
    float d00 = Dot(v0, v0);
    float d01 = Dot(v0, v1);
    float d11 = Dot(v1, v1);
    float d20 = Dot(v2, v0);
    float d21 = Dot(v2, v1);
    float denom = d00 * d11 - d01 * d01;
    v = (d11 * d20 - d01 * d21) / denom;
    w = (d00 * d21 - d01 * d20) / denom;
    u = 1.0f - v - w;
}

Conversion to cartesian

// https://stackoverflow.com/a/11262425
Vector3d Tri::cartesian(const Vector3d& barycentric) const
{
      return barycentric.x * p0 + barycentric.y * p1 + barycentric.z * p2;
}