package toffee

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file rect.ml

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
type 'a t = { left : 'a; right : 'a; top : 'a; bottom : 'a }

(* Constants *)

let zero = { left = 0.0; right = 0.0; top = 0.0; bottom = 0.0 }
let all value = { left = value; right = value; top = value; bottom = value }

(* Creation *)

let make ~left ~right ~top ~bottom = { left; right; top; bottom }

(* Mapping functions *)

let map f rect =
  {
    left = f rect.left;
    right = f rect.right;
    top = f rect.top;
    bottom = f rect.bottom;
  }

let map2 f r1 r2 =
  {
    left = f r1.left r2.left;
    right = f r1.right r2.right;
    top = f r1.top r2.top;
    bottom = f r1.bottom r2.bottom;
  }

let zip_size size f rect =
  {
    left = f rect.left size.Size.width;
    right = f rect.right size.Size.width;
    top = f rect.top size.height;
    bottom = f rect.bottom size.height;
  }

(* Component extraction *)

let horizontal_components rect = Line.{ start = rect.left; end_ = rect.right }
let vertical_components rect = Line.{ start = rect.top; end_ = rect.bottom }

(* Arithmetic operations *)

let add a b =
  {
    left = a.left +. b.left;
    right = a.right +. b.right;
    top = a.top +. b.top;
    bottom = a.bottom +. b.bottom;
  }

let ( + ) = add

(* Axis sums *)

let horizontal_axis_sum rect = rect.left +. rect.right
let vertical_axis_sum rect = rect.top +. rect.bottom

let sum_axes rect =
  { Size.width = horizontal_axis_sum rect; height = vertical_axis_sum rect }

let grid_axis_sum axis rect =
  match axis with
  | Absolute_axis.Horizontal -> rect.left +. rect.right
  | Absolute_axis.Vertical -> rect.top +. rect.bottom

(* Comparison and string functions *)

let compare cmp a b =
  let cmp_left = cmp a.left b.left in
  if cmp_left <> 0 then cmp_left
  else
    let cmp_right = cmp a.right b.right in
    if cmp_right <> 0 then cmp_right
    else
      let cmp_top = cmp a.top b.top in
      if cmp_top <> 0 then cmp_top else cmp a.bottom b.bottom

let equal eq a b =
  eq a.left b.left && eq a.right b.right && eq a.top b.top
  && eq a.bottom b.bottom

let to_string f rect =
  Printf.sprintf "{ left: %s; right: %s; top: %s; bottom: %s }" (f rect.left)
    (f rect.right) (f rect.top) (f rect.bottom)

let pp f fmt rect =
  Format.fprintf fmt "{ left: %a; right: %a; top: %a; bottom: %a }" f rect.left
    f rect.right f rect.top f rect.bottom