triangle_area.py

Created by james-wysocki

Created on February 11, 2026

873 Bytes

Three different formulas (traditional, sine, heron’s) for calculating the area of a triangle.


# Triangle Area formulas
# Three different formulas depending on given information
# J Wysocki, 11 Feb 2026

from numpy import sqrt, sin, pi

def heron(a,b,c):
  """
  Using Heron's formula to calculate area of a triangle
  parameters: side lengths of a triangle (a, b, c)
  return: area of a triangle
  """
  semi = (a + b + c) / 2
  return sqrt(semi * (semi - a) * (semi - b) * (semi - c))
  
def area(base, height):
  """
  Use traditional formula to calculate area of a triangle
  parameters: base and height of a triangle
  return: area of a triangle
  """
  return base * height / 2

def sine_area(side1, angle, side2):
  """
  Using the sine area formula to calculate area of a triangle
  parameters: two sides and included angle
  return: area of a triangle
  """
  radian_angle = angle * pi / 180
  return side1 * side2 * sin(radian_angle) / 2

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.