Last active
June 8, 2020 11:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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