{"id":9,"date":"2007-07-25T22:26:54","date_gmt":"2007-07-26T05:26:54","guid":{"rendered":"http:\/\/www.elbeno.com\/haskell_soe_blog\/?p=9"},"modified":"2008-01-07T21:42:33","modified_gmt":"2008-01-08T05:42:33","slug":"exercise-24","status":"publish","type":"post","link":"https:\/\/www.elbeno.com\/haskell_soe_blog\/?p=9","title":{"rendered":"Exercise 2.4"},"content":{"rendered":"<pre lang=\"haskell\">{-\r\nIs a shape convex: compute the cross product of the two vectors\r\nformed by each 3 vertices - a convex shape will have all the\r\ncross products the same sign.\r\n-}\r\n\r\ncrossProduct :: Vertex -> Vertex -> Vertex -> Float\r\ncrossProduct (x1, y1) (x2, y2) (x3, y3) = (x2 - x1) * (y3 - y2)\r\n                                          - (y2 - y1) * (x3 - x2)\r\n\r\nconvex :: Shape -> Bool\r\nconvex (Rectangle a b) = True\r\nconvex (RtTriangle a b) = True\r\nconvex (Ellipse r1 r2) = True\r\nconvex (Polygon [ _ , _ , _ ]) = True\r\nconvex (Polygon (vfirst : vsecond : vthird : vs))\r\n    = let sign = crossProduct vfirst vsecond vthird\r\n      in polyConvex sign vsecond (vthird : vs)\r\n          where polyConvex s v1 (v2 : v3 : vs')\r\n                    = let newSign = crossProduct v1 v2 v3\r\n                      in if newSign * s < 0\r\n                         then False\r\n                         else polyConvex s v2 (v3 : vs')\r\n                polyConvex s vn (vlast : [])\r\n                    = let newSign = crossProduct vn vlast vfirst\r\n                      in if newSign * s < 0\r\n                         then False\r\n                         else polyConvex s vlast []\r\n                polyConvex s vlast []\r\n                    = let newSign = crossProduct vlast vfirst vsecond\r\n                      in newSign * s > 0<\/pre>\n<p>This function is crying out for some higher order help! But it works, and it&#8217;s even straightforward, if inelegant in the end-of-list pattern matching.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>{- Is a shape convex: compute the cross product of the two vectors formed by each 3 vertices &#8211; a convex shape will have all the cross products the same sign. -} crossProduct :: Vertex -> Vertex -> Vertex -> Float crossProduct (x1, y1) (x2, y2) (x3, y3) = (x2 &#8211; x1) * (y3 &#8211; [&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\/9"}],"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=9"}],"version-history":[{"count":0,"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=\/wp\/v2\/posts\/9\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}