Calculating the area of a triangle

Hi,
I’m working on a Blender exporter utility and need to calculate the area of a triangle. My trig maths is not very strong. :slight_smile:

I have played with a number of algorithms but the results are inconsistent. i.e. sometimes they are correct, sometimes close and sometimes way off. I’m using the 2 metre test cube and a UVsphere as test objects. I have another utility that gives me the correct answer but I don’t have the source for tht.

The mesh is converted to triangles as part of the export process and I have a linked list of the triangles to work with.

As part of the problem I need to calculate the length of each side of the triangle. This is easy when a triangle is perpendicular and follows the classic text book examples but these triangles exist in a 3 dimensional world and are seldom perpendicular.

I found this equation that should provide the length of any side of a triangle: Sqrt ((X1-x2)[SUP]2[/SUP] + (Y1-y2)[SUP]2[/SUP] + (Z1 - Z2)[SUP]2[/SUP]).

Can someone confirm this is correct? I am using it but think it may be the cause of my inconsistent results.

Once I have the lengths I can calculate angles, etc leading to the area calculation.

Or, if someone has a simple solution for calculating the area of a (Blender) triangle, I’d be pleased to hear it.

TIA
Paul

Use vectors,

A triangle is three points in space A, B and C, taking any one point, A in this case half the magnitude of the cross product of the vectors AB and AC gives the area. Three points is the minimum definition of a plane as long as they don’t fall on the same point or line.


A = Vector((0, 0, 0))
B = Vector((0, 1, 0))
C = Vector((1, 0, 0))

AB = B - A
AC = C - A

area = AC.cross(AB).length / 2

print(area)
# 0.5

Also note bmesh has methods for the area of faces, rather than calculating.

from the console, triangulated default Cube.


>>> bm = bmesh.from_edit_mesh(bpy.context.object.data)
>>> bm.faces[0].calc_area()
1.9999996423721313

Thank you for the quick response. I’ll take that away and play with it.
Cheers

The ideas post by batFINGER helped me solve this problem. The target code wasn’t python but the technique was the same. Many thanks.