~jaro/balkon

ref: e18bc15fe1d4b1445d766afea1a1ebf628747ee6 balkon/src/Data/Text/ParagraphLayout/Internal/Rect.hs -rw-r--r-- 2.1 KiB
e18bc15fJaro Ensure tests do not pass with missing files. 1 year, 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
-- | Representation of an axis-aligned rectangle on a 2D plane, with one of its
-- corners being a designated origin point.
module Data.Text.ParagraphLayout.Internal.Rect
    (Rect(Rect, x_origin, y_origin, x_size, y_size)
    ,height
    ,union
    ,width
    ,x_max
    ,x_min
    ,x_terminus
    ,y_max
    ,y_min
    ,y_terminus
    )
where

data Rect a = Rect { x_origin :: a, y_origin :: a, x_size :: a, y_size :: a }
    deriving (Eq, Read, Show)

-- | Absolute difference between the X coordinates of the rectangle's sides.
width :: Num a => Rect a -> a
width r = abs $ x_size r

-- | Absolute difference between the Y coordinates of the rectangle's sides.
height :: Num a => Rect a -> a
height r = abs $ y_size r

-- | X coordinate of the corner opposite of the origin.
x_terminus :: Num a => Rect a -> a
x_terminus r = x_origin r + x_size r

-- | Y coordinate of the corner opposite of the origin.
y_terminus :: Num a => Rect a -> a
y_terminus r = y_origin r + y_size r

-- | The smaller of the two X coordinates of the rectangle's edges.
x_min :: (Num a, Ord a) => Rect a -> a
x_min r = x_origin r `min` x_terminus r

-- | The smaller of the two Y coordinates of the rectangle's edges.
y_min :: (Num a, Ord a) => Rect a -> a
y_min r = y_origin r `min` y_terminus r

-- | The larger of the two X coordinates of the rectangle's edges.
x_max :: (Num a, Ord a) => Rect a -> a
x_max r = x_origin r `max` x_terminus r

-- | The larger of the two Y coordinates of the rectangle's edges.
y_max :: (Num a, Ord a) => Rect a -> a
y_max r = y_origin r `max` y_terminus r

-- | Calculate the smallest rectangle that completely contains the given two
-- rectangles.
--
-- The origin of the resulting rectangle will be the corner with the lowest
-- X coordinate and the highest Y coordinate, regardless of the origin of the
-- input rectangles.
union :: (Num a, Ord a) => Rect a -> Rect a -> Rect a
union a b = Rect x_low y_high dx (-dy) where
    x_low = x_min a `min` x_min b
    y_low = y_min a `min` y_min b
    x_high = x_max a `max` x_max b
    y_high = y_max a `max` y_max b
    dx = x_high - x_low
    dy = y_high - y_low