diff options
| author | Carlos Sosa <gnusosa@gnusosa.net> | 2020-03-20 09:39:05 -0700 |
|---|---|---|
| committer | Carlos Sosa <gnusosa@gnusosa.net> | 2020-03-20 09:39:05 -0700 |
| commit | f3e001044829f4864360df87a69695cb962e5d36 (patch) | |
| tree | a702a0c3ea6a0533747710cdbc8f5152c8a000c4 /ex2/Shape.hs | |
| parent | 525cf5908bebd1a97f803810b8ae318bffbd6fe4 (diff) | |
Exercise trapezoids areas sum to calculate area of polygon
Diffstat (limited to 'ex2/Shape.hs')
| -rw-r--r-- | ex2/Shape.hs | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/ex2/Shape.hs b/ex2/Shape.hs index 97916c0..d1f9e2f 100644 --- a/ex2/Shape.hs +++ b/ex2/Shape.hs @@ -24,11 +24,18 @@ 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)) = polyArea vs - where - polyArea :: [Vertex] -> Float - polyArea (v2 : v3 : vs') = triArea v1 v2 v3 + polyArea (v3 : vs') - polyArea _ = 0 +area (Polygon vs) + | length vs <= 2 = 0 + | otherwise = polyArea (vs ++ [head vs]) + where polyArea :: [Vertex] -> Float + polyArea (v1: v2: vs') = trapezoidArea v1 v2 + polyArea (v2:vs') + polyArea _ = 0 + +trapezoidArea :: Vertex -> Vertex -> Float +trapezoidArea v1 v2 = + let h = fst v2 - fst v1 + aplusb = snd v2 + snd v1 + in (aplusb / 2) * h triArea :: Vertex -> Vertex -> Vertex -> Float triArea v1 v2 v3 = |
