top of page
Search
cedarcantab

Test point inside Triangle


A typical application of barycentric coordinates is to parameterize triangles (or

the planes of the triangles). Consider a triangle ABC specified by three noncollinear

points A, B, and C. Any point P in the plane of the points can then be uniquely

expressed as P = uA + vB + wC for some constants u, v, and w, where u + v + w = 1.

The triplet (u, v, w) corresponds to the barycentric coordinates of the point. For the triangle ABC, the barycentric coordinates of the vertices A, B, and C are (1, 0, 0),

(0, 1, 0), and (0, 0, 1), respectively. In general, a point with barycentric coordinates

(u, v, w) is inside (or on) the triangle if and only if 0 ≤ u, v, w ≤ 1, or alternatively

if and only if 0 ≤ v ≤ 1, 0 ≤ w ≤ 1, and v + w ≤ 1. That barycentric coordinates

actually parameterize the plane follows from P = uA + vB + wC really just being a

reformulation of P = A + v(B − A) + w(C − A), with v and w arbitrary, as

P = A + v(B − A) + w(C − A) = (1 − v − w)A + vB + wC.


In the latter formulation, the two independent direction vectors AB and AC form a coordinate system with origin A, allowing any point P in the plane to be parameterized in terms of v and w alone. Clearly, barycentric coordinates is a redundant representation in that the third component can be expressed in terms of the first two. It is kept for reasons of symmetry. To solve for the barycentric coordinates, the expression P = A + v(B − A) + w(C − A) — or equivalently v(B − A) + w(C − A) = P − A — can be written as v v0 + w v1 = v2, where v0 = B − A, v1 = C − A, and v2 = P − A. Now, a 2 × 2 system of linear equations can be formed by taking the dot product of both sides with both v0 and v1:



Because the dot product is a linear operator, these expressions are equivalent to


This system is easily solved with Cramer’s rule.


// Compute barycentric coordinates (u, v, w) for

// point p with respect to triangle (a, b, c)
void Barycentric(Point a, Point b, Point c, Point p, 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;
}



Let us express Q as a weighted sum of the triangle vertices. These new barycentric coordinates let us represent any point in the plane in

terms of (u,v,w).



If we combine the previous equations, we can compute the barycentric

coordinates of Q using linear algebra.



So how do we compute the area of a triangle?

Recall that the area of a parallelogram is found from the cross product of two

of its edges.

In 2D the cross product is just a a scalar.

The area of triangle ABC is half the area of the associated parallelogram.

Notice the area can be negative, depending on the winding order of ABC.


We have the interior region when all the coordinates are positive.


If u is negative, then Q is outside edge BC.


If v is negative, then Q is outside edge CA.




The barycentric regions are not exclusive.

Two barycentric coordinates can be negative at the same time.

In this case the query point is outside of two edges simultaneously.

In general this does not indicate that Q is in a vertex region.

In this slide we see that Q is outside BC and CA, yet is closest to BC.


1 view0 comments

Recent Posts

See All

p2 naive broadphase

var Broadphase = require('../collision/Broadphase'); module.exports = NaiveBroadphase; /** * Naive broadphase implementation. Does N^2...

sopiro motor constranit

import { Matrix2, Vector2 } from "./math.js"; import { RigidBody } from "./rigidbody.js"; import { Settings } from "./settings.js";...

Comments


記事: Blog2_Post
bottom of page