1#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
3pub struct Coordinates<T: Copy> {
4 pub x: T,
6 pub y: T,
8}
9
10#[derive(Debug, Copy, Clone)]
12pub struct Coord<T: Copy>(pub T, pub T);
13pub type Coordf = Coordinates<f64>;
17
18impl<T: Copy> Coordinates<T> {
19 pub fn new(x: T, y: T) -> Self {
21 Coordinates { x, y }
22 }
23
24 pub fn tup(&self) -> (T, T) {
26 (self.x, self.y)
27 }
28}
29
30impl<T: Copy> From<[T; 2]> for Coordinates<T> {
33 fn from(array: [T; 2]) -> Self {
34 Self {
35 x: array[0],
36 y: array[1],
37 }
38 }
39}
40
41impl<T: Copy> From<Coordinates<T>> for [T; 2] {
42 fn from(c: Coordinates<T>) -> Self {
43 [c.x, c.y]
44 }
45}
46
47impl<T: Copy> From<(T, T)> for Coordinates<T> {
48 fn from(tuple: (T, T)) -> Self {
49 Self {
50 x: tuple.0,
51 y: tuple.1,
52 }
53 }
54}
55
56impl<T: Copy> From<Coordinates<T>> for (T, T) {
57 fn from(c: Coordinates<T>) -> Self {
58 (c.x, c.y)
59 }
60}
61
62#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
64pub struct Size {
65 pub w: i32,
67 pub h: i32,
69}
70
71impl Size {
72 pub fn new(w: i32, h: i32) -> Self {
74 Size { w, h }
75 }
76
77 pub fn tup(self) -> (i32, i32) {
79 (self.w, self.h)
80 }
81}
82
83impl From<[i32; 2]> for Size {
86 fn from(array: [i32; 2]) -> Self {
87 Self {
88 w: array[0],
89 h: array[1],
90 }
91 }
92}
93
94impl From<Size> for [i32; 2] {
95 fn from(c: Size) -> Self {
96 [c.w, c.h]
97 }
98}
99
100impl From<(i32, i32)> for Size {
101 fn from(tuple: (i32, i32)) -> Self {
102 Self {
103 w: tuple.0,
104 h: tuple.1,
105 }
106 }
107}
108impl From<Size> for (i32, i32) {
109 fn from(c: Size) -> Self {
110 (c.w, c.h)
111 }
112}
113
114#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
116pub struct Cell {
117 pub r: i32,
119 pub c: i32,
121}
122
123impl Cell {
124 pub fn new(r: i32, c: i32) -> Self {
126 Cell { r, c }
127 }
128
129 pub fn tup(self) -> (i32, i32) {
131 (self.r, self.c)
132 }
133}
134
135impl From<[i32; 2]> for Cell {
138 fn from(array: [i32; 2]) -> Self {
139 Cell {
140 r: array[0],
141 c: array[1],
142 }
143 }
144}
145
146impl From<Cell> for [i32; 2] {
147 fn from(c: Cell) -> Self {
148 [c.r, c.c]
149 }
150}
151
152impl From<(i32, i32)> for Cell {
153 fn from(tuple: (i32, i32)) -> Self {
154 Self {
155 r: tuple.0,
156 c: tuple.1,
157 }
158 }
159}
160impl From<Cell> for (i32, i32) {
161 fn from(c: Cell) -> Self {
162 (c.r, c.c)
163 }
164}
165
166use std::ops::{Add, Sub};
167
168#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
170pub struct Rectangle<T: Copy + Add<Output = T> + Sub<Output = T>> {
171 pub x: T,
173 pub y: T,
175 pub w: T,
177 pub h: T,
179}
180
181pub type Rect = Rectangle<i32>;
183
184impl<T: Copy + Add<Output = T> + Sub<Output = T>> Rectangle<T> {
185 pub fn new(x: T, y: T, width: T, height: T) -> Self {
188 Self {
189 x,
190 y,
191 w: width,
192 h: height,
193 }
194 }
195
196 pub fn with_padding(self, pad: T) -> Self {
198 Self {
199 x: self.x + pad,
200 y: self.y + pad,
201 w: self.w - (pad + pad),
202 h: self.h - (pad + pad),
203 }
204 }
205
206 pub fn from_coords(top_left: Coordinates<T>, bottom_right: Coordinates<T>) -> Self {
209 Self {
210 x: top_left.x,
211 y: top_left.y,
212 w: bottom_right.x - top_left.x,
213 h: bottom_right.y - top_left.y,
214 }
215 }
216
217 pub fn top_left(&self) -> Coordinates<T> {
219 Coordinates::new(self.x, self.y)
220 }
221
222 pub fn bottom_right(&self) -> Coordinates<T> {
224 Coordinates::new(self.x + self.w, self.y + self.h)
225 }
226
227 pub fn tup(&self) -> (T, T, T, T) {
229 (self.x, self.y, self.w, self.h)
230 }
231}
232
233impl<T: Copy + Add<Output = T> + Sub<Output = T>> From<[T; 4]> for Rectangle<T> {
236 fn from(array: [T; 4]) -> Self {
237 Self {
238 x: array[0],
239 y: array[1],
240 w: array[2],
241 h: array[3],
242 }
243 }
244}
245
246impl<T: Copy + Add<Output = T> + Sub<Output = T>> From<Rectangle<T>> for [T; 4] {
247 fn from(c: Rectangle<T>) -> Self {
248 [c.x, c.y, c.w, c.h]
249 }
250}
251
252impl<T: Copy + Add<Output = T> + Sub<Output = T>> From<(T, T, T, T)> for Rectangle<T> {
253 fn from(tuple: (T, T, T, T)) -> Self {
254 Self {
255 x: tuple.0,
256 y: tuple.1,
257 w: tuple.2,
258 h: tuple.3,
259 }
260 }
261}
262
263impl<T: Copy + Add<Output = T> + Sub<Output = T>> From<Rectangle<T>> for (T, T, T, T) {
264 fn from(c: Rectangle<T>) -> Self {
265 (c.x, c.y, c.w, c.h)
266 }
267}