TermOx
boundary.hpp
1 #ifndef TERMOX_WIDGET_BOUNDARY_HPP
2 #define TERMOX_WIDGET_BOUNDARY_HPP
3 #include <cassert>
4 #include <cmath>
5 
6 namespace ox {
7 
9 template <typename Number_t>
10 struct Boundary {
11  Number_t west = 0;
12  Number_t east = 1;
13  Number_t north = 1;
14  Number_t south = 0;
15 };
16 
18 
19 template <typename Number_t>
20 [[nodiscard]] auto scale(Boundary<Number_t> b, double amount)
22 {
23  assert(amount > 0. && std::isfinite(amount));
24  return {Number_t(b.west * amount), Number_t(b.east * amount),
25  Number_t(b.north * amount), Number_t(b.south * amount)};
26 }
27 
29 
30 template <typename Number_t>
31 [[nodiscard]] auto scroll_west(Boundary<Number_t> b, Number_t amount)
32  -> Boundary<Number_t>
33 {
34  assert(amount >= 0. && std::isfinite(amount));
35  b.west -= amount;
36  b.east -= amount;
37  return b;
38 }
39 
41 
42 template <typename Number_t>
43 [[nodiscard]] auto scroll_east(Boundary<Number_t> b, Number_t amount)
44  -> Boundary<Number_t>
45 {
46  assert(amount >= 0. && std::isfinite(amount));
47  b.west += amount;
48  b.east += amount;
49  return b;
50 }
51 
53 
54 template <typename Number_t>
55 [[nodiscard]] auto scroll_north(Boundary<Number_t> b, Number_t amount)
56  -> Boundary<Number_t>
57 {
58  assert(amount >= 0. && std::isfinite(amount));
59  b.north += amount;
60  b.south += amount;
61  return b;
62 }
63 
65 
66 template <typename Number_t>
67 [[nodiscard]] auto scroll_south(Boundary<Number_t> b, Number_t amount)
68  -> Boundary<Number_t>
69 {
70  assert(amount >= 0. && std::isfinite(amount));
71  b.north -= amount;
72  b.south -= amount;
73  return b;
74 }
75 
77 template <typename Number_t>
78 [[nodiscard]] auto operator-(Boundary<Number_t> a, Boundary<Number_t> b)
79  -> Boundary<Number_t>
80 {
81  return {a.west - b.west, a.east - b.east, a.north - b.north,
82  a.south - b.south};
83 }
84 
86 template <typename Number_t>
87 [[nodiscard]] auto operator+(Boundary<Number_t> a, Boundary<Number_t> b)
88  -> Boundary<Number_t>
89 {
90  return {a.west + b.west, a.east + b.east, a.north + b.north,
91  a.south + b.south};
92 }
93 
94 } // namespace ox
95 #endif // TERMOX_WIDGET_BOUNDARY_HPP
Four point Boundary, edges are inclusive.
Definition: boundary.hpp:10