 Can someone explain this "triarea2" function for me?

This topic is 2121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I don't fully understand how it's related to the triangular area of three given 2D Cartesian points on a 2D plane. I do know that given the three points as:

X = (4, 0)

Y = (0, 0)

Z = (0, 3)

The function returns twice the size of the expected triangular area the three points have marked.

Here's the function I found on a blog:

inline float triarea2(const float* a, const float* b, const float* c)
{
const float ax = b - a;
const float ay = b - a;
const float bx = c - a;
const float by = c - a;
return bx*ay - ax*by;
}

Or in Java code:

public static float triarea2(float[] a, float[] b, float[] c)
{
float ax = b - a;
float ay = b - a;
float bx = c - a;
float by = c - a;
return bx * ay - ax * by;
}

Can someone help me explain what this function does, and how one can come up with such math? How do one derive the area from a simple triangle? Thanks.

Edited by tom_mai78101

Share on other sites

The magnitude of the cross product of two vectors is the area of the parallelogram spanned by the two vectors. The area of the triangle is half that area, or equivalently, the area of the parallelogram is twice the area of the triangle. See this as an example: clicky.

The formula in your code is simply the Z-component of the cross product of two vectors in a 2D-plane. Two vectors in the XY-plane has its cross product along the Z-axis, and thus the magnitude of the cross product is simply the magnitude of the Z-component.

• Game Developer Survey We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 9
• 15
• 9
• 11