diff options
Diffstat (limited to 'ex2')
| -rw-r--r-- | ex2/Main.hs | 4 | ||||
| -rw-r--r-- | ex2/Shape.hs | 49 |
2 files changed, 53 insertions, 0 deletions
diff --git a/ex2/Main.hs b/ex2/Main.hs new file mode 100644 index 0000000..7e32c4a --- /dev/null +++ b/ex2/Main.hs @@ -0,0 +1,4 @@ +import Shape + +main = do + putStrLn $ show $ square 5.0 diff --git a/ex2/Shape.hs b/ex2/Shape.hs new file mode 100644 index 0000000..0f7c0a9 --- /dev/null +++ b/ex2/Shape.hs @@ -0,0 +1,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) |
