Graphics Reference
In-Depth Information
} else if (y >= x && y >= z) {
// y is largest, project to the xz plane
nu = TriArea2D(p.x, p.z, b.x, b.z, c.x, c.z);
nv = TriArea2D(p.x, p.z, c.x, c.z, a.x, a.z);
ood = 1.0f / -m.y;
} else {
// z is largest, project to the xy plane
nu = TriArea2D(p.x, p.y, b.x, b.y, c.x, c.y);
nv = TriArea2D(p.x, p.y, c.x, c.y, a.x, a.y);
ood = 1.0f / m.z;
}
u = nu * ood;
v = nv * ood;
w=1.0f-u-v;
}
Barycentric coordinates have many uses. Because they are invariant under projec-
tion, they can be used to map points between different coordinate systems. They can
be used for point-in-triangle testing. Given a vertex-lit triangle, they can also find
the corresponding RGB of a specific point within the triangle, which could be used
to adjust the ambient color of an object at that position on the triangle. For triangle
clipping, they can be used to interpolate any quantity, including colors (Gouraud
shading), normals (Phong shading), and texture coordinates (texture mapping). The
following code illustrates how barycentric coordinates can be used to test containment
of a point P in a triangle ABC .
// Test if point p is contained in triangle (a, b, c)
int TestPointTriangle(Point p, Point a, Point b, Point c)
{
float u, v, w;
Barycentric(a, b, c, p, u, v, w);
return v >= 0.0f && w >= 0.0f && (v + w) <= 1.0f;
}
A generalized form of barycentric coordinates for irregular n -sided convex polygons
is given in [Meyer02]. For n
3, it reduces to the traditional formula for barycentric
coordinates. See also [Floater04].
When the barycentric coordinates of a point P ( P
=
=
a 0 P 0
+
a 1 P 1
+···+
a n P n , a 0
+
+···
=
a 1
a n
1) with respect to the points P 0 , P 1 , ... , P n also satisfies a 0 , a 1 , ... , a n
0,
P is said to be a convex combination of P 0 , P 1 , ... , P n .
 
Search WWH ::




Custom Search