{"id":8,"date":"2007-07-25T22:05:44","date_gmt":"2007-07-26T05:05:44","guid":{"rendered":"http:\/\/www.elbeno.com\/haskell_soe_blog\/?p=8"},"modified":"2008-01-07T21:41:01","modified_gmt":"2008-01-08T05:41:01","slug":"exercise-23","status":"publish","type":"post","link":"https:\/\/www.elbeno.com\/haskell_soe_blog\/?p=8","title":{"rendered":"Exercise 2.3"},"content":{"rendered":"<p>Relevant definitions:<\/p>\n<pre lang=\"haskell\">area :: Shape -> Float\r\narea (Rectangle s1 s2) = s1 * s2\r\narea (Polygon (v1 : vs) = polyArea vs\r\n      where polyArea (v2 : v3 : vs') = triArea v1 v2 v3\r\n                                       + polyArea (v3 : vs')\r\n            polyArea _ = 0\r\n\r\ntriArea :: Vertex -> Vertex -> Vertex -> Float\r\ntriArea v1 v2 v3 = let a = distBetween v1 v2\r\n                       b = distBetween v2 v3\r\n                       c = distBetween v3 v1\r\n                       s = 0.5 * (a + b + c)\r\n                   in sqrt (s * (s-a) * (s-b) * (s-c))\r\n\r\ndistBetween :: Vertex -> Vertex -> Float\r\ndistBetween (x1, y1) (x2, y2) = sqrt ((x1-x2)^2 + (y1-y2)^2)<\/pre>\n<p>Expanding <tt>area (Polygon [(0,0), (s1,0), (s1,s2), (0,s2)])<\/tt>:<\/p>\n<pre>area (Polygon [(0,0), (s1,0), (s1,s2), (0,s2)])\r\n=> polyArea [(s1,0), (s1,s2), (0,s2)]\r\n=> triArea (0,0) (s1,0) (s1,s2) + polyArea [(s1,s2), (0,s2)]\r\n=> triArea (0,0) (s1,0) (s1,s2) + triArea (0,0) (s1,s2) (0,s2)\r\n                                + polyArea [(0,s2)]\r\n=> triArea (0,0) (s1,0) (s1,s2) + triArea (0,0) (s1,s2) (0,s2) + 0<\/pre>\n<p>Consider <tt>triArea (0,0) (s1,0) (s1,s2)<\/tt>:<\/p>\n<pre>a = distBetween (0,0) (s1,0)\r\n  = sqrt ((0-s1)<sup>2<\/sup> + (0-0)<sup>2<\/sup>)\r\n  = sqrt s1<sup>2<\/sup>\r\n  = s1\r\n\r\nb = distBetween (s1,0) (s1,s2)\r\n  = sqrt ((s1-s1)<sup>2<\/sup> + (0-s2)<sup>2<\/sup>)\r\n  = sqrt s2<sup>2<\/sup>\r\n  = s2\r\n\r\nc = distBetween (s1,s2) (0,0)\r\n  = sqrt ((s1-0)<sup>2<\/sup> + (s2-0)<sup>2<\/sup>)\r\n  = sqrt(s1<sup>2<\/sup> + s2<sup>2<\/sup>)\r\n\r\ns * (s-c)\r\n= 0.5 * (a + b + c) * ((0.5 * (a + b + c)) - c)\r\n= 0.5 * (a + b + c) * 0.5 * (a + b - c)\r\n= 0.25 * (a + b + c) * (a + b - c)\r\n= 0.25 * ((a + b)<sup>2<\/sup> - c<sup>2<\/sup>)\r\n= 0.25 * ((s1 + s2)<sup>2<\/sup> - (s1<sup>2<\/sup> + s2<sup>2<\/sup>))\r\n= 0.25 * (s1<sup>2<\/sup> + 2*s1*s2 + s2<sup>2<\/sup> - s1<sup>2<\/sup> - s2<sup>2<\/sup>)\r\n= 0.25 * 2*s1*s2\r\n= 0.5 * s1 * s2\r\n\r\n(s-a) * (s-b)\r\n= ((0.5 * (a + b + c)) - a) * ((0.5 * (a + b + c)) - b)\r\n= (0.5 * (b + c - a)) * (0.5 * (a + c - b))\r\n= 0.25 * (b + c - a) * (a + c - b)\r\n= 0.25 * (c + (b - a)) * (c - (b - a))\r\n= 0.25 * (c<sup>2<\/sup> - (b-a)<sup>2<\/sup>)\r\n= 0.25 * (s1<sup>2<\/sup> + s2<sup>2<\/sup> - (s2-s1)<sup>2<\/sup>)\r\n= 0.25 * (s1<sup>2<\/sup> + s2<sup>2<\/sup> - (s2<sup>2<\/sup> - 2*s1*s2 + s1<sup>2<\/sup>))\r\n= 0.25 * (s1<sup>2<\/sup> + s2<sup>2<\/sup> - s2<sup>2<\/sup> + 2*s1*s2 - s1<sup>2<\/sup>)\r\n= 0.25 * 2*s1*s2\r\n= 0.5 * s1 * s2\r\n\r\nsqrt (s * (s-a) * (s-b) * (s-c))\r\n= sqrt (0.5 * s1 * s2 * 0.5 * s1 * s2)\r\n= 0.5 * s1 * s2<\/pre>\n<p>Consider <tt>triArea (0,0) (s1,s2) (0,s2)<\/tt>. By inspection:<\/p>\n<pre>a = sqrt(s1<sup>2<\/sup> + s2<sup>2<\/sup>)\r\nb = s1\r\nc = s2<\/pre>\n<p>And as before:<\/p>\n<pre>sqrt (s * (s-a) * (s-b) * (s-c))\r\n= 0.5 * s1 * s2<\/pre>\n<p>Therefore:<\/p>\n<pre>area (Polygon [(0,0), (s1,0), (s1,s2), (0,s2)])\r\n= triArea (0,0) (s1,0) (s1,s2) + triArea (0,0) (s1,s2) (0,s2) + 0\r\n= (0.5 * s1 * s2) + (0.5 * s1 * s2)\r\n= s1 * s2\r\n= area (Rectangle s1 s2)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Relevant definitions: area :: Shape -> Float area (Rectangle s1 s2) = s1 * s2 area (Polygon (v1 : vs) = polyArea vs where polyArea (v2 : v3 : vs&#8217;) = triArea v1 v2 v3 + polyArea (v3 : vs&#8217;) polyArea _ = 0 triArea :: Vertex -> Vertex -> Vertex -> Float triArea v1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=\/wp\/v2\/posts\/8"}],"collection":[{"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8"}],"version-history":[{"count":0,"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=\/wp\/v2\/posts\/8\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}