Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!

A 2d TriArea method,don't understand why,need help

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 tracegame   Members   -  Reputation: 134


Posted 05 June 2012 - 07:53 AM

hi,again another geometry silly question to ask if i may.

i'm a total math idiot,i read this book named Realtime Collision Detection,and

there is a function called TriArea2D defines below

inline float TriArea2D(float x1, float y1, float x2, float y2, float x3, float y3)
return (x1-x2)*(y2-y3) - (x2-x3)*(y1-y2);

// 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)
// Unnormalized triangle normal
Vector m = Cross(b - a, c - a);
// Nominators and one-over-denominator for u and v ratios
float nu, nv, ood;
// Absolute components for determining projection plane
float x = Abs(m.x), y = Abs(m.y), z = Abs(m.z);

// Compute areas in plane of largest projection
if (x >= y && x >= z) {
// x is largest, project to the yz plane
nu = TriArea2D(p.y, p.z, b.y, b.z, c.y, c.z); // Area of PBC in yz plane
nv = TriArea2D(p.y, p.z, c.y, c.z, a.y, a.z); // Area of PCA in yz plane
ood = 1.0f / m.x; // 1/(2*area of ABC in yz plane)
} 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;

i don't understand why this function can calculate TriArea,i only know if two vector cross product,you get a parallelogram area in 2D case,right?

but why this called TriArea??Do i misread something?

Edited by tracegame, 05 June 2012 - 10:49 PM.


#2 Brother Bob   Moderators   -  Reputation: 9290


Posted 05 June 2012 - 08:23 AM

Unless I'm missing something obvious, the code has to divide the result by two in order to get the area of triangle. As it is, it does indeed calculate the cross product, or equivalently the area of the parallelogram spanned by two edges of the triangle, and not the area of the triangle itself.

#3 DvDmanDT   GDNet+   -  Reputation: 1271


Posted 05 June 2012 - 08:38 AM

What's it used for? Even if it does not compute the actual area of a triangle, it may still be used for comparing triangle areas for sorting or whatever.

#4 tracegame   Members   -  Reputation: 134


Posted 05 June 2012 - 10:50 PM

oops,it's my fault,update the full code,sorry.

and i agree with Bob,it should be divided by two,and i'm NOT good at english,

so maybe i don't fully understand TriArea2D mean,does it also mean some ratio of Area,or something like that?

or maybe the author writes this name for some short case or use or something,anyway i'm confused with this function name and use.

actually i can not visualize the projection area,and how does it work.

Edited by tracegame, 06 June 2012 - 12:28 AM.

#5 Rikonator   Members   -  Reputation: 168


Posted 11 June 2012 - 05:23 AM

I'm at the same point of the same book, and it is pretty confusing. Especially since there is no explanation of the code.

He did explain prior to this that the barycentric coordinates of a point P in triangle ABC are given as,

u = SignedArea(PBC)/SignedArea(ABC),
v = SignedArea(PCA)/SignedArea(ABC), and
w = SignedArea(PAB)/SignedArea(ABC) = 1 - u – v.

He says that instead of computing areas from 3d coordinates of the triangle, we can project vertices onto the yz, zx or xy plane, selecting the plane with largest projected area.

And in the section before Barycentric coordinates, he also says that for a triangle ABC,

d = Cross(B-A, C-A),

where each component of d holds twice the signed area of the projection of ABC onto the yz, zx and xy planes, respectively.

What's happening here is instead of u = SignedArea(PBC)/SignedArea(ProjectionZY(ABC)), it is u = (2*SignedArea(PBC)/(2*SignedArea(ProjectionZY(ABC))). He simply finds the projection with largest projected area, and then finds the Barycentric coordinates using that.

What I don't understand is why he had to multiply the signed area of projection onto the zx plane with -1.

Why does he use ood = 0.01f/-m.y instead of ood = 0.01f/m.y, like the other two planes? There is nothing mentioned related to that in the book.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.