Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to calculate triangle area from 3 vertices

Is this the correct way to calculate the area of a triangle given the 3 triangle points/vertices? The vertices will never be negative values.

def triangle_area(tri):
    x1, y1, x2, y2, x3, y3 = tri[0][0], tri[0][1], tri[1][0], tri[1][1], tri[2][0], tri[2][1]
    return 0.5 * (((x2-x1)*(y3-y1))-((x3-x1)*(y2-y1)))
like image 791
sazr Avatar asked Oct 25 '25 09:10

sazr


1 Answers

It is necessary to add abs to this formula to avoid negative area value (sign depends on orientation, not on positive/negative coordinates)

Yes, this formula is correct and it implements the best approach if you have vertices coordinates. It is based on cross product properties.

def triangle_area(tri):
    x1, y1, x2, y2, x3, y3 = tri[0][0], tri[0][1], tri[1][0], tri[1][1], tri[2][0], tri[2][1]
    return abs(0.5 * (((x2-x1)*(y3-y1))-((x3-x1)*(y2-y1))))
like image 123
MBo Avatar answered Oct 27 '25 22:10

MBo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!