Skip to main content
GameDev.net gamedev.net
🔒 Locked

Barycentric Coords

Started by glSmurf Oct 18, 2005 at 4:05 PM 4 replies 1.4k views
Original Post
glSmurf
glSmurf
How do I compute the barycentric coords for a point on a triangle?
Zakwayda
Zakwayda
Quote:
How do I compute the barycentric coords for a point on a triangle?
Google will probably tell you. However, you'll need to specify (to us at least) whether the triangle is in 2d or 3d.

You probably already know that the barycentric coordinates can be viewed as weights for the vertices, like this:

p = b1v1+b2v2+b3v3

And that the values of bi sum to 1. In 2d this leads to a system of 3 equations in 3 unknowns, which is solvable as long as the triangle is not degenerate. The solution can also be expressed in terms of the ratio between the areas of the triangle and of the three sub-triangles formed from the query point and a triangle edge. These ratios can be determined using the perp-dot product, and are necessarily the same as the solutions to the linear system.

In 3d the linear system is 4 equations in 3 unknowns and is over-determined. A common solution is to project the triangle into the plane with which it is most aligned and use the 2d solution. There are also ways of determining the solution directly, such as using cross products or quadratic minimization.
glSmurf
glSmurf
Maybe I just had bad luck on my google search, but all the information I found about the subject was the equation used to get point P already knowing the barycentrics. I posted this question just before going to bed, and hoped that my problem would be solved while I was asleep =)

The triangle is in 3D ..but the reason for why I didn't specify that is because an answer for a triangle in 2D would help me alot too.

Ofcourse I will keep searching on google now that I'm awake again, but if you know of any good links that could help me don't hesitate to write =)
Gorwooken
Gorwooken
TI'm not sure if theres a better way to do it but when i needed to calculate barycentric coords i decided to write a function for row reduction for my matrix class, since all we're looking for is a solution to a system of linear equations.

W1A + W2B + W3C = D

so i have 3 points of the triangle, A, B, C and the point that i want to represent, D.

I setup the matrix like this:

| Ax Bx Cx | Dx |
| Ay By Cy | Dy |
| Az Bz Cz | Dz |
| 1 1 1 | 1 |

The last row there is the formula W1 + W2 + W2 = 1.

Now I have 3 weights that I want to solve for, the coeff for W1 are the left most column, W2 the one to the right of that ect.

All I do now is put this matrix into row reduced echelon form and i've got:

| 1 0 0 | W1 |
| 0 1 0 | W2 |
| 0 0 1 | W3 |
glSmurf
glSmurf
thanks =)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.