-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShape.hs
More file actions
45 lines (34 loc) · 1.25 KB
/
Shape.hs
File metadata and controls
45 lines (34 loc) · 1.25 KB
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
-- file: Haskell/Graphic/Show.hs
module Shape ( Shape (Rectangle, Ellipse, RtTriangle, Polygon)
, Radius
, Side
, Vertex
, square
, circle
, distBetween
, area ) 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)
square s = Rectangle s s
circle r = Ellipse r r
area :: Shape ->Float
area (Rectangle s1 s2) = s1*s2
area (RtTriangle s1 s2) = s1*s2/2
area (Ellipse r1 r2) = pi*r1*r2
area (Polygon (v1:vs))
= foldl (\a (v2,v3) -> a + triArea v1 v2 v3) 0.0 list
where list = zip (tail vs) vs
triArea :: Vertex -> Vertex -> Vertex -> Float
triArea v1 v2 v3 = let a = distBetween v1 v2
b = distBetween v2 v3
c = distBetween v3 v1
s = 0.5 * (a+b+c)
in sqrt(s*(s-a)*(s-b)*(s-c))
distBetween :: Vertex -> Vertex -> Float
distBetween (x1, y1) (x2, y2) = sqrt((x1-x2)^2+(y1-y2)^2)