Skip to content

Instantly share code, notes, and snippets.

@hertzsprung
Last active June 21, 2017 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hertzsprung/86c97e71ae2a86150bbec45c5e35c264 to your computer and use it in GitHub Desktop.
Save hertzsprung/86c97e71ae2a86150bbec45c5e35c264 to your computer and use it in GitHub Desktop.
Compare d.S and Heron's formula for face area
#!/usr/bin/env python3
import math
import numpy as np
import numpy.linalg as la
def herons_area(v1, v2, v3):
a = la.norm(v2 - v1)
b = la.norm(v3 - v2)
c = la.norm(v1 - v3)
p = 0.5*(a+b+c)
return math.sqrt(p*(p-a)*(p-b)*(p-c))
x1 = 1
y1 = 1
x2 = 3
y2 = 0.5
x3 = x1 # assume this is always true!
y3 = 6
v0 = np.array([0, 0])
v1 = np.array([x1, y1])
v2 = np.array([x2, y2])
v3 = np.array([x3, y3])
area1 = herons_area(v0, v1, v3)
area2 = herons_area(v1, v2, v3)
print("area", area1+area2)
d = v2
S = np.array([y3 - y1, 0]) # should really use cross product
print("0.5*S.d", 0.5*np.dot(S,d))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment