Exercise 2.3


Relevant definitions:

area :: Shape -> Float
area (Rectangle s1 s2) = s1 * s2
area (Polygon (v1 : vs) = polyArea vs
      where polyArea (v2 : v3 : vs') = triArea v1 v2 v3
                                       + polyArea (v3 : vs')
            polyArea _ = 0

triArea :: Vertex -> Vertex -> Vertex -> Float
triArea v1 v2 v3 = let a = distBetween v1 v2
                       b = distBetween v2 v3
                       c = distBetween v3 v1
                       s = 0.5 * (a + b + c)
                   in sqrt (s * (s-a) * (s-b) * (s-c))

distBetween :: Vertex -> Vertex -> Float
distBetween (x1, y1) (x2, y2) = sqrt ((x1-x2)^2 + (y1-y2)^2)

Expanding area (Polygon [(0,0), (s1,0), (s1,s2), (0,s2)]):

area (Polygon [(0,0), (s1,0), (s1,s2), (0,s2)])
=> polyArea [(s1,0), (s1,s2), (0,s2)]
=> triArea (0,0) (s1,0) (s1,s2) + polyArea [(s1,s2), (0,s2)]
=> triArea (0,0) (s1,0) (s1,s2) + triArea (0,0) (s1,s2) (0,s2)
                                + polyArea [(0,s2)]
=> triArea (0,0) (s1,0) (s1,s2) + triArea (0,0) (s1,s2) (0,s2) + 0

Consider triArea (0,0) (s1,0) (s1,s2):

a = distBetween (0,0) (s1,0)
  = sqrt ((0-s1)2 + (0-0)2)
  = sqrt s12
  = s1

b = distBetween (s1,0) (s1,s2)
  = sqrt ((s1-s1)2 + (0-s2)2)
  = sqrt s22
  = s2

c = distBetween (s1,s2) (0,0)
  = sqrt ((s1-0)2 + (s2-0)2)
  = sqrt(s12 + s22)

s * (s-c)
= 0.5 * (a + b + c) * ((0.5 * (a + b + c)) - c)
= 0.5 * (a + b + c) * 0.5 * (a + b - c)
= 0.25 * (a + b + c) * (a + b - c)
= 0.25 * ((a + b)2 - c2)
= 0.25 * ((s1 + s2)2 - (s12 + s22))
= 0.25 * (s12 + 2*s1*s2 + s22 - s12 - s22)
= 0.25 * 2*s1*s2
= 0.5 * s1 * s2

(s-a) * (s-b)
= ((0.5 * (a + b + c)) - a) * ((0.5 * (a + b + c)) - b)
= (0.5 * (b + c - a)) * (0.5 * (a + c - b))
= 0.25 * (b + c - a) * (a + c - b)
= 0.25 * (c + (b - a)) * (c - (b - a))
= 0.25 * (c2 - (b-a)2)
= 0.25 * (s12 + s22 - (s2-s1)2)
= 0.25 * (s12 + s22 - (s22 - 2*s1*s2 + s12))
= 0.25 * (s12 + s22 - s22 + 2*s1*s2 - s12)
= 0.25 * 2*s1*s2
= 0.5 * s1 * s2

sqrt (s * (s-a) * (s-b) * (s-c))
= sqrt (0.5 * s1 * s2 * 0.5 * s1 * s2)
= 0.5 * s1 * s2

Consider triArea (0,0) (s1,s2) (0,s2). By inspection:

a = sqrt(s12 + s22)
b = s1
c = s2

And as before:

sqrt (s * (s-a) * (s-b) * (s-c))
= 0.5 * s1 * s2

Therefore:

area (Polygon [(0,0), (s1,0), (s1,s2), (0,s2)])
= triArea (0,0) (s1,0) (s1,s2) + triArea (0,0) (s1,s2) (0,s2) + 0
= (0.5 * s1 * s2) + (0.5 * s1 * s2)
= s1 * s2
= area (Rectangle s1 s2)

One response to “Exercise 2.3”

Leave a Reply

Your email address will not be published. Required fields are marked *