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

Pages: 1-

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:03

where

data Shape = Rectangle Side Side
           | Ellipse Radius Radius
           | RtTriangle Side Side
           | Polygon [Vertex]
    deriving Show
   
type Radius = Float
type Side   = Float
type Vertex = (Float, Float)

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?

Name: Anonymous 2007-10-24 21:51

>>3

the code pasted isn't mine, i'm saying i don't understand why it works

Name: Anonymous 2007-10-24 22:05

oh
it works for what I've just told you.
The only thing I haven't told you yet is where the radius formula comes from.
The circle is splitten like a pizza, so now you have n triangles.
Each triangle has one angle you already know, which is the angle of the triangle that "touches" the center.
Let's call this angle 'a'.
a = 2 pi / n
But the triangle has two other angles, b and c. Also,
b = c
And you also know that, for any triangle
a + b + c = pi
So, since b and c are equal, you can say

2*b = pi - a
b = (pi - a) / 2

but the sin theorem, says that for any triangle

sin a / length side x = sin b / length side y = sin c / length side z

where x, y, and z are the sides opposite to the angles a, b, and c.

You know a, and you know b, and you know the length of the side opposite to the angle 'a', which is the side of the triangle. You also know that the length of the side opposite to the angle 'b' is also the radius.
So:

sin a / s = sin b / radius
radius = sin b / (sin a / s)
radius = (sin b / sin a) * s
but writing what we know, and calling 'a' as 'angleinc'

radius =  s * (sin ((pi - a) / 2) / sin angleinc)

which is pretty much the same formula as the code

Name: Anonymous 2007-10-24 22:50

ah, ok. thanks a lot

Name: Anonymous 2007-10-25 2:29

Name: Anonymous 2007-10-25 13:14

...........           ..........
...........           ..........
...........           ..........

Name: Anonymous 2009-02-25 6:56

Msgtab.

Name: Anonymous 2009-03-06 7:48

The initials are coincidental.

Name: Anonymous 2011-01-31 21:27

<-- check em dubz

Name: Anonymous 2011-02-04 18:04


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