Can someone explain this "triarea2" function for me?

This topic is 1842 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[0] - a[0];
const float ay = b[1] - a[1];
const float bx = c[0] - a[0];
const float by = c[1] - a[1];
return bx*ay - ax*by;
}



Or in Java code:

	public static float triarea2(float[] a, float[] b, float[] c)
{
float ax = b[0] - a[0];
float ay = b[1] - a[1];
float bx = c[0] - a[0];
float by = c[1] - a[1];
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.

Share on other sites

Ah! That explains it. Thanks again.

Share on other sites

Yes, I was. And then I forgo it, due to me not knowing how to write the code and implement it into mine.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 9
• 15
• 14
• Forum Statistics

• Total Topics
634070
• Total Posts
3015332
×