Barycentric coordinates: Difference between revisions

From vegard.wiki
Jump to navigation Jump to search
Content added Content deleted
(new page)
 
No edit summary
Line 17: Line 17:
w = (d00 * d21 - d01 * d20) / denom;
w = (d00 * d21 - d01 * d20) / denom;
u = 1.0f - v - w;
u = 1.0f - v - w;
}
</source>

=== Conversion to cartesian ===

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

Revision as of 13:36, 29 December 2019

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;
}