SOE ex.2.2


{-
  Polar coordinate to Cartesian coordinate:
  (r, theta)  ==>   (r*sin(theta), r*cos(theta))

  regularPolygon of n sides ==> theta = 360 degrees / n

     /|  angle = theta / 2
  r / |  ==> sin (theta/2) = (s/2) / r
   /__|  ==> r = s / (2 * sin(theta/2))
    s/2
-}

regularPolygon :: Int -> Side -> Shape
-- regularPolygon n s = Polygon $ map vertexAtAngle [0, theta .. 360]
regularPolygon n s = Polygon $ map vertexAtAngle (take n [0, theta ..])
  where
  intToFloat n = fromInteger (toInteger n)
  degreeToRadian angle = angle * pi / 180
  theta = 360 / (intToFloat n)
  r = s / (2 * sin (degreeToRadian (theta / 2)))
  vertexAtAngle angle = (r * cos (degreeToRadian angle), r * sin (degreeToRadian angle))

Leave a Reply