Advertisement Jump to content
Sign in to follow this  
tom_mai78101

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!