1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
module Shape
( Shape
, area
, square
, circle
)
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)
area :: Shape -> Float
area (Rectangle s1 s2) = s1 * s2
area (RtTriangle s1 s2) = s1 * s2 / 2
square :: Side -> Shape
square s = Rectangle s s
circle :: Radius -> Shape
circle r = Ellipse r r
rectangle :: Side -> Side -> Shape
rectangle s1 s2 = Polygon [(x, y), (-x, y), (-x, -y), (x, -y)]
where
x = s1 / 2
y = s2 / 2
rtTriangle :: Side -> Side -> Shape
rtTriangle s1 s2 = Polygon [(0, 0), (s1, 0), (0, s2)]
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)
|