Skip to content

Instantly share code, notes, and snippets.

@xyfeng
Created February 23, 2017 22:39
Show Gist options
  • Save xyfeng/2247e6367e7843ed6593823b2bb3c820 to your computer and use it in GitHub Desktop.
Save xyfeng/2247e6367e7843ed6593823b2bb3c820 to your computer and use it in GitHub Desktop.
calculate barycentric coordinates in Processing
// calcuate barycentric coordinates (u,v,w) for point p with respect to triangle (a,b,c)
// use 3d PVector to store u,v,w
// https://en.wikipedia.org/wiki/Barycentric_coordinate_system
PVector getBaryCentric(PVector p, PVector a, PVector b, PVector c) {
PVector v0 = PVector.sub(b, a);
PVector v1 = PVector.sub(c, a);
PVector v2 = PVector.sub(p, a);
float den = v0.x * v1.y - v1.x * v0.y;
float v = (v2.x * v1.y - v1.x * v2.y) / den;
float w = (v0.x * v2.y - v2.x * v0.y) / den;
float u = 1.0f - v - w;
return new PVector(u, v, w);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment