Last active
August 29, 2015 14:15
-
-
Save wootfish/6d8a9ac708d391e7b8ca to your computer and use it in GitHub Desktop.
Heron's Formula in Python -- Naive
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
Python 2.7.8 (default, Oct 18 2014, 12:50:18) | |
[GCC 4.9.1] on linux2 | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> def distance(p1, p2): | |
... # this just uses the Pythagorean Theorem | |
... x = (p1[0] - p2[0]) ** 2 | |
... y = (p1[1] - p2[1]) ** 2 | |
... return (x + y) ** 0.5 | |
... | |
>>> def herons(a, b, c): | |
... # this uses Heron's Formula to get the area of a triangle with | |
... # side lengths of a, b, and c | |
... # https://en.wikipedia.org/wiki/Heron%27s_formula | |
... s = (a + b + c) / 2. | |
... return (s*(s-a)*(s-b)*(s-c))**0.5 | |
... | |
>>> p1, p2, p3 = ((3, 4), (7, 10), (11, 161)) | |
>>> # side lengths: | |
... s1, s2, s3 = (distance(p1, p2), distance(p1, p3), distance(p2, p3)) | |
>>> | |
>>> herons(s1, s2, s3) | |
289.99999999999864 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment