Skip to content

Instantly share code, notes, and snippets.

@marmakoide
Last active June 8, 2020 11:11
Show Gist options
  • Save marmakoide/a381afe30f5d4e1dbcb8792e4d50b4c8 to your computer and use it in GitHub Desktop.
Save marmakoide/a381afe30f5d4e1dbcb8792e4d50b4c8 to your computer and use it in GitHub Desktop.
Returns a triangle's surface area from the length of its 3 sides, in a numerically stable fashion
'''
Compute a triangle's area from its edges length with Heron's formula
Numerically stable
'''
def triangle_area_from_lengths(a, b, c):
# Sort input
c, b, a = sorted((a, b, c))
# Compute Heron's formula
ret = (a + (b + c)) * (c - (a - b)) * (c + (a - b)) * (a + (b - c))
return .25 * ret ** .5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment