~jaro/balkon

14b8c850a92ec30889f388361ed95843a46d55c8 — Jaro 1 year, 3 months ago 6138513
Implement unions of lists of rectangles.
1 files changed, 22 insertions(+), 0 deletions(-)

M src/Data/Text/ParagraphLayout/Internal/Rect.hs
M src/Data/Text/ParagraphLayout/Internal/Rect.hs => src/Data/Text/ParagraphLayout/Internal/Rect.hs +22 -0
@@ 5,6 5,8 @@ module Data.Text.ParagraphLayout.Internal.Rect
    , Rect (Rect, x_origin, y_origin, x_size, y_size)
    , height
    , union
    , unionMany
    , unionMany1
    , width
    , x_max
    , x_min


@@ 15,6 17,8 @@ module Data.Text.ParagraphLayout.Internal.Rect
    )
where

import Data.List.NonEmpty (NonEmpty, nonEmpty)

-- | An axis-aligned rectangle on a 2D plane.
data Rect a = Rect
    { x_origin :: a


@@ 101,3 105,21 @@ union bias a b =
        hy = y_max a `max` y_max b
        dx = hx - lx
        dy = hy - ly

-- | `Just` the `union` of all given rectangles, or `Nothing` if none are given.
unionMany :: (Num a, Ord a) => Bias -> [Rect a] -> Maybe (Rect a)
unionMany bias rects = unionMany1 bias <$> nonEmpty rects

-- | The `union` of all given rectangles, where at least one must be given.
--
-- Note that adding a default value to the input list to make it non-empty
-- will probably not do what you want, since the `union` operation has no
-- identity element.
--
-- If you need a default output value for empty inputs, consider using:
--
-- @
-- `Data.Maybe.fromMaybe` yourDefaultValue $ `unionMany` bias rects
-- @
unionMany1 :: (Num a, Ord a) => Bias -> NonEmpty (Rect a) -> Rect a
unionMany1 bias rects = foldr1 (union bias) rects