How to calculate area of triangle given the length of sides?

To apply any formula to calculate area of a triangle, one must know the value of at least one angle of the triangle. However, there is one formula for calculating the area of a triangle without measuring any of its angles.

In this article, we will use Heron’s formula, named after the Hero of Alexandria, a Greek mathematician. It takes three lengths (a,b, c) of a triangle and semi-perimeter s. In order to apply this formula, it is mandatory to know the lengths of all three sides of the triangle.

Formula

A = √s (s - a) (s - b) (s - c)

where, a, b and c are the lengths of sides of the triangle where s is half of the perimeter given by s=(a+b+c)/2.

Mathematical illustration

calculate area of triangle using python
Heron’s Formula to calculate area of triangle

Explanation

While coding, using Heron’s formula is not going to be a challenging task for you if you can compute the square root using a custom or built-in function. Now, let’s have a python program to evaluate the area of a triangle by using the length of the sides.

def calculateArea(a, b, c):
  	 s= (a+b+c)/2  #s is the semi perimeter
  	 area= math.sqrt(s*(s-a)*(s-b)*(s-c))
  	 return area
 
print(calculateArea (4, 6, 8))
  • Line 1: Function definition to calculate the area of a triangle using given sides i.e. a, b, and c.
  • Line 2: Calculating the semi perimeter of the triangle by calculating the sum of sides & dividing it by 2.
  • Line 3: Computing the area by calculating the square root of the product of s(s-a)(s-b)(s-c).
  • Line 4: Returning the area computed by calculateArea().
  • Line 6: Invoking calculateArea() method with 4, 6, and 8 values for a, b, and c respectively to calculate the area of a triangle. 

Output

calculate area of triangle using python

Stay in the Loop

Get the daily email from Algoideas that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...