Suppose you are given the vertices of a triangle and asked to find the area using the Shoelace formula and Heron's formula. The two methods yield different results.
In the example describe below, there is a discrepancy i.e. difference of 896 between the two methods. What causes this discrepancy in the result?
Does this say anything about the accuracy of the methods in general i.e. is one method more accurate than the other?
My Best Guess: calculating the area using Heron's formula requires us to compute the lengths of each side. This computation introduces an additional uncertainty/error in the result. To avoid this "extra error", we can calculate the area directly from the given information i.e. the vertices. Therefore, the Shoelace method is more accurate when given the vertices.
def shoelace_formula(v):
x1y2 = v[0][0]*v[1][1]
x2y3 = v[1][0]*v[2][1]
x3y1 = v[2][0]*v[0][1]
x2y1 = v[1][0]*v[0][1]
x3y2 = v[2][0]*v[1][1]
x1y3 = v[0][0]*v[2][1]
A = (0.5)*abs(x1y2+x2y3+x3y1-x2y1-x3y2-x1y3)
return A
def magnitudes(v):
mags = []
sides = ((v[0],v[1]),(v[1],v[2]),(v[0],v[2]))
for side in sides:
mag = hypot(side[0][0]-side[1][0],side[0][1]-side[1][1])
# mag = sqrt((side[0][0]-side[1][0])**2+(side[0][1]-side[1][1])**2)
mags.append(mag)
return mags
def herons_formula(a,b,c):
s = (a+b+c)/2.0
A = sqrt(s*(s-a)*(s-b)*(s-c))
return A
vertices = [[9120703, 8956600230], [-88693678, -83026], [67100, 47194000]]
a,b,c = magnitudes(vertices)
Area_heron = herons_formula(a,b,c)
Area_shoelace =shoelace_formula(vertices)
print repr(Area_shoelace), repr(Area_heron), Area_shoelace - Area_heron
#returns 3.9518890053421114e+17 3.9518890053421203e+17 -896.0