From d017ccd84899d85fffe7d840b7513e8bf286b2e3 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sun, 16 Jun 2024 23:27:38 +0200 Subject: [PATCH] Implement combined functions --- Exercises/exercise-6/Shapes.hs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Exercises/exercise-6/Shapes.hs b/Exercises/exercise-6/Shapes.hs index c4583ff..1f2e996 100644 --- a/Exercises/exercise-6/Shapes.hs +++ b/Exercises/exercise-6/Shapes.hs @@ -41,13 +41,13 @@ unitSq = Shape $ \(x, y) -> -} translate :: Vector -> Shape -> Shape translate (dx, dy) s = Shape $ \(x, y) -> - error "Fixme" + inside s (x + dx, y + dy) {- Inverting Inverting a shape i.e. switching outside vs inside -} negate :: Shape -> Shape -negate s = error "Fixme" +negate s = Shape $ \ (x, y) -> not (inside s (x, y)) {- General combinator Combining two shapes with a parametric boolean function @@ -66,19 +66,19 @@ combineBool f s1 s2 = Shape $ \p -> f (f1 p) (f2 p) All points that are in both shapes -} intersect :: Shape -> Shape -> Shape -intersect = error "Fixme" +intersect s1 s2 = Shape $ \ (x, y) -> all (\ s -> inside s (x, y)) [s1, s2] {- All points that in at least one of the shapes -} merge :: Shape -> Shape -> Shape -merge = error "Fixme" +merge s1 s2 = Shape $ \ (x, y) -> any (\ s -> inside s (x, y)) [s1, s2] {- - All points in the first shape that are not in the secnond shape + All points in the first shape that are not in the second shape -} minus :: Shape -> Shape -> Shape -minus = error "Fixme" +minus s1 s2 = Shape $ \ (x, y) -> inside s1 (x, y) && not (inside s1 (x, y)) {- Matrix transformations @@ -170,4 +170,4 @@ iShape = flipX $ merge (stretchY 2 unitSq) (translate (0, 5) unitDisc) -disc50 = stretch 50 unitDisc \ No newline at end of file +disc50 = stretch 50 unitDisc