Advertisement
DexterAndre

Barycentric Coordinates

Mar 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. static void barycentric(const Vector3& p, const Vector3& a, const Vector3& b, const Vector3& c, Vector3& out)
  2. {
  3.     // Using code from: https://gamedev.stackexchange.com/questions/23743/whats-the-most-efficient-way-to-find-barycentric-coordinates
  4.  
  5.     Vector3 v0 = b - a;
  6.     Vector3 v1 = c - a;
  7.     Vector3 v2 = p - a;
  8.     float d00 = v0 * v0;
  9.     float d01 = v0 * v1;
  10.     float d11 = v1 * v1;
  11.     float d20 = v2 * v0;
  12.     float d21 = v2 * v1;
  13.     float denominator = d00 * d11 - d01 * d01;
  14.     float v = (d11 * d20 - d01 * d21) / denominator;
  15.     float w = (d00 * d21 - d01 * d20) / denominator;
  16.     float u = 1.0f - v - w;
  17.     out = Vector3(u, v, w);
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement