Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

regular polygons

Name: Anonymous 2007-10-24 21:01

Help me /prog/, i'm stuck on exercise 2.2 of the haskell school of expressions.

Define a function regularPolygon :: Int -> Side -> Shape such that regularPolygon n s is a regular polygon with n sides, each of length s.

I've found a couple answers, but don't get them.

regularPolygon :: Int -> Side -> Shape
regularPolygon n s
    = let angleinc = pi * 2 / fromIntegral n
          radius = s * sin ((pi - angleinc) / 2) / sin angleinc
          regularVerts 0 _ = []
          regularVerts n angle = (radius * cos angle, radius * sin angle)
                                 : regularVerts (n-1) (angle + angleinc)
      in Polygon (regularVerts n 0)

Name: Anonymous 2007-10-24 21:47

are you aware there are many many ways to get a regular polygon with n sides?
anyway just think of a circle centered at 0, divide 2*pi with the number of sides, so now you got the vertexs, but you have to muliply them for some factor to get the desired side length.
but you've got a triangle, you know the sides and one angle, so you can calculate the other side length.
But it seems like you've already figured out this, so what is getting you stuck?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List