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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/// Defines a pair of `x, y` coordinates
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Coordinates<T: Copy> {
    /// Horizontal X coordinate
    pub x: T,
    /// Vertical Y coordinate
    pub y: T,
}

/// `i32` Coordinates
#[derive(Debug, Copy, Clone)]
pub struct Coord<T: Copy>(pub T, pub T);
// pub type Coord = Coordinates<i32>; // TODO for 2.0

/// `f64` Coordinates
pub type Coordf = Coordinates<f64>;

impl<T: Copy> Coordinates<T> {
    /// Returns a new pair of `x, y` coordinates
    pub fn new(x: T, y: T) -> Self {
        Coordinates { x, y }
    }

    /// Returns a tuple of the values
    pub fn tup(&self) -> (T, T) {
        (self.x, self.y)
    }
}

// Conversions From/Into array and tuple

impl<T: Copy> From<[T; 2]> for Coordinates<T> {
    fn from(array: [T; 2]) -> Self {
        Self {
            x: array[0],
            y: array[1],
        }
    }
}

impl<T: Copy> From<Coordinates<T>> for [T; 2] {
    fn from(c: Coordinates<T>) -> Self {
        [c.x, c.y]
    }
}

impl<T: Copy> From<(T, T)> for Coordinates<T> {
    fn from(tuple: (T, T)) -> Self {
        Self {
            x: tuple.0,
            y: tuple.1,
        }
    }
}

impl<T: Copy> From<Coordinates<T>> for (T, T) {
    fn from(c: Coordinates<T>) -> Self {
        (c.x, c.y)
    }
}

/// Defines a pair of `w, h` (width, height) values representing size
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Size {
    /// Width
    pub w: i32,
    /// Height
    pub h: i32,
}

impl Size {
    /// Returns a new pair of `w, h` (width, height) values
    pub fn new(w: i32, h: i32) -> Self {
        Size { w, h }
    }

    /// Returns a tuple of the values
    pub fn tup(&self) -> (i32, i32) {
        (self.w, self.h)
    }
}

// Conversions From/Into array and tuple

impl From<[i32; 2]> for Size {
    fn from(array: [i32; 2]) -> Self {
        Self {
            w: array[0],
            h: array[1],
        }
    }
}

impl From<Size> for [i32; 2] {
    fn from(c: Size) -> Self {
        [c.w, c.h]
    }
}

impl From<(i32, i32)> for Size {
    fn from(tuple: (i32, i32)) -> Self {
        Self {
            w: tuple.0,
            h: tuple.1,
        }
    }
}
impl From<Size> for (i32, i32) {
    fn from(c: Size) -> Self {
        (c.w, c.h)
    }
}

/// Defines a pair of `r, c` (row, column) representing a Cell
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Cell {
    /// Horizontal X coordinate
    pub r: i32,
    /// Vertical Y coordinate
    pub c: i32,
}

impl Cell {
    /// Returns a new pair of `r, c` (row, column) cell
    pub fn new(r: i32, c: i32) -> Self {
        Cell { r, c }
    }

    /// Returns a tuple of the values
    pub fn tup(&self) -> (i32, i32) {
        (self.r, self.c)
    }
}

// Conversions From/Into array and tuple

impl From<[i32; 2]> for Cell {
    fn from(array: [i32; 2]) -> Self {
        Cell {
            r: array[0],
            c: array[1],
        }
    }
}

impl From<Cell> for [i32; 2] {
    fn from(c: Cell) -> Self {
        [c.r, c.c]
    }
}

impl From<(i32, i32)> for Cell {
    fn from(tuple: (i32, i32)) -> Self {
        Self {
            r: tuple.0,
            c: tuple.1,
        }
    }
}
impl From<Cell> for (i32, i32) {
    fn from(c: Cell) -> Self {
        (c.r, c.c)
    }
}

use std::ops::{Add, Sub};

/// Defines a rectangular bounding box
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Rectangle<T: Copy + Add<Output = T> + Sub<Output = T>> {
    /// Leftmost corner
    pub x: T,
    /// Topmost corner
    pub y: T,
    /// Width
    pub w: T,
    /// Height
    pub h: T,
}

/// `i32` Coordinates
pub type Rect = Rectangle<i32>;

impl<T: Copy + Add<Output = T> + Sub<Output = T>> Rectangle<T> {
    /// Returns a new `Rectangle` from its top-left corner position,
    /// and the length of its sides.
    pub fn new(x: T, y: T, width: T, height: T) -> Self {
        Self {
            x,
            y,
            w: width,
            h: height,
        }
    }

    /// Sets the Rectangle to be padded
    pub fn with_padding(self, pad: T) -> Self {
        Self {
            x: self.x + pad,
            y: self.y + pad,
            w: self.w - (pad + pad),
            h: self.h - (pad + pad),
        }
    }

    /// Returns a new `Rectangle` from the position of its `top_left`
    /// and `bottom_right` corners.
    pub fn from_coords(top_left: Coordinates<T>, bottom_right: Coordinates<T>) -> Self {
        Self {
            x: top_left.x,
            y: top_left.y,
            w: bottom_right.x - top_left.x,
            h: bottom_right.y - top_left.y,
        }
    }

    /// Returns the coordinates of the top-left corner
    pub fn top_left(&self) -> Coordinates<T> {
        Coordinates::new(self.x, self.y)
    }

    /// Returns the coordinates of the bottom-right corner
    pub fn bottom_right(&self) -> Coordinates<T> {
        Coordinates::new(self.x + self.w, self.y + self.h)
    }

    /// Returns a tuple of the values
    pub fn tup(&self) -> (T, T, T, T) {
        (self.x, self.y, self.w, self.h)
    }
}

// Conversions From/Into array and tuple

impl<T: Copy + Add<Output = T> + Sub<Output = T>> From<[T; 4]> for Rectangle<T> {
    fn from(array: [T; 4]) -> Self {
        Self {
            x: array[0],
            y: array[1],
            w: array[2],
            h: array[3],
        }
    }
}

impl<T: Copy + Add<Output = T> + Sub<Output = T>> From<Rectangle<T>> for [T; 4] {
    fn from(c: Rectangle<T>) -> Self {
        [c.x, c.y, c.w, c.h]
    }
}

impl<T: Copy + Add<Output = T> + Sub<Output = T>> From<(T, T, T, T)> for Rectangle<T> {
    fn from(tuple: (T, T, T, T)) -> Self {
        Self {
            x: tuple.0,
            y: tuple.1,
            w: tuple.2,
            h: tuple.3,
        }
    }
}

impl<T: Copy + Add<Output = T> + Sub<Output = T>> From<Rectangle<T>> for (T, T, T, T) {
    fn from(c: Rectangle<T>) -> Self {
        (c.x, c.y, c.w, c.h)
    }
}