• Create Account

# Can someone explain this "triarea2" function for me?

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.

4 replies to this topic

### #1tom_mai78101  Members   -  Reputation: 477

Like
0Likes
Like

Posted 16 December 2013 - 07:07 AM

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, 16 December 2013 - 07:08 AM.

### #2Brother Bob  Moderators   -  Reputation: 7135

Like
3Likes
Like

Posted 16 December 2013 - 07:22 AM

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.

### #3tom_mai78101  Members   -  Reputation: 477

Like
0Likes
Like

Posted 16 December 2013 - 07:58 AM

Ah! That explains it. Thanks again.

### #4Angus Hollands  Members   -  Reputation: 553

Like
0Likes
Like

Posted 01 January 2014 - 03:32 PM

Out of interest, are you using this algorithm?

http://digestingduck.blogspot.co.uk/2010/03/simple-stupid-funnel-algorithm.html

### #5tom_mai78101  Members   -  Reputation: 477

Like
0Likes
Like

Posted 01 January 2014 - 06:35 PM

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

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.

PARTNERS