SOE ex.8.1


import Shape

fiveCircles' = circlesRegion `Intersect` rectangleRegion
  where
  ps = [0, 2 ..] ++ [-2, -4 ..]
  circleShape = Shape (circle 1)
  circleShapes = map (flip Translate circleShape) [(x,y) | x <- ps, y <- ps]
  circlesRegion = foldl Union Empty circleShapes
  rectangleRegion = Translate (4, 0) (Shape $ rectangle 10 2)

circlesRegion' = foldl Union Empty (map circleR1At circleCenters)
  where
  spiral = concat $ map (replicate 2) [1..]
  directions = cycle [(2, 0), (0, 2), (-2, 0), (0, -2)]
  circleCenters = scanl (\(x,y) (dx,dy) -> (x+dx, y+dy)) (0,0) $
                  concat $ zipWith replicate spiral directions
  circleR1At v = Translate v (Shape (circle 1))

{-
fiveCircles' is theory correct but practically impossible to achieve.
All cycles in 2D plan (for axe negative infinite to positive infinite)
are almost impossible to test "Intersect". 
(Especially the way we implement the "intersect")

circlesRegion' is just for fun, it defines all cycles from origin and spiral to infinite.
-}

data Region
  = Shape Shape
  | Translate Vector Region
  | Scale Vector Region
  | Complement Region
  | Region `Union` Region
  | Region `Intersect` Region
  | Empty
  deriving Show

type Vector = (Float, Float)

infixr 5 `Union`
infixr 6 `Intersect`

Leave a Reply