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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#![warn(missing_copy_implementations)]
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Vector {
pub x: f32,
pub y: f32,
}
pub type Point = Vector;
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Rect {
pub position: Point,
pub size: Vector,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
impl Vector {
pub fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
pub fn distance(self, other: Vector) -> f32 {
let dx = other.x - self.x;
let dy = other.y - self.y;
(dx * dx + dy * dy).sqrt()
}
}
pub fn vector(x: f32, y: f32) -> Vector {
Vector::new(x, y)
}
pub fn point(x: f32, y: f32) -> Point {
Point::new(x, y)
}
impl Color {
pub const BLACK: Self = rgb(0, 0, 0);
pub const WHITE: Self = rgb(255, 255, 255);
pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
Self { r, g, b, a }
}
pub const fn argb(hex: u32) -> Self {
let a = (hex >> 24) & 0xFF;
let r = (hex >> 16) & 0xFF;
let g = (hex >> 8) & 0xFF;
let b = hex & 0xFF;
Self {
r: r as u8,
g: g as u8,
b: b as u8,
a: a as u8,
}
}
pub const fn to_argb(&self) -> u32 {
((self.a as u32) << 24) | ((self.r as u32) << 16) | ((self.g as u32) << 8) | (self.b as u32)
}
pub const fn rgb(hex: u32) -> Self {
Self::argb(hex | 0xFF000000)
}
pub const fn with_alpha(self, a: u8) -> Self {
Self::new(self.r, self.g, self.b, a)
}
}
pub const fn rgb(r: u8, g: u8, b: u8) -> Color {
Color::new(r, g, b, 255)
}
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Color {
Color::new(r, g, b, a)
}
impl Rect {
pub fn new(position: impl Into<Point>, size: impl Into<Vector>) -> Self {
Self {
position: position.into(),
size: size.into(),
}
}
pub fn x(&self) -> f32 {
self.position.x
}
pub fn y(&self) -> f32 {
self.position.y
}
pub fn width(&self) -> f32 {
self.size.x
}
pub fn height(&self) -> f32 {
self.size.y
}
pub fn left(&self) -> f32 {
self.position.x
}
pub fn top(&self) -> f32 {
self.position.y
}
pub fn right(&self) -> f32 {
self.position.x + self.size.x
}
pub fn bottom(&self) -> f32 {
self.position.y + self.size.y
}
pub fn top_left(&self) -> Point {
self.position
}
pub fn top_right(&self) -> Point {
self.position + vector(self.size.x, 0.0)
}
pub fn bottom_left(&self) -> Point {
self.position + vector(0.0, self.size.y)
}
pub fn bottom_right(&self) -> Point {
self.position + self.size
}
pub fn center(&self) -> Point {
self.position + self.size / 2.0
}
pub fn center_x(&self) -> f32 {
self.position.x + self.size.x / 2.0
}
pub fn center_y(&self) -> f32 {
self.position.y + self.size.y / 2.0
}
pub fn sort(self) -> Self {
let left = f32::min(self.left(), self.right());
let right = f32::max(self.left(), self.right());
let top = f32::min(self.top(), self.bottom());
let bottom = f32::max(self.top(), self.bottom());
Self::new(point(left, top), vector(right - left, bottom - top))
}
}
impl Default for Vector {
fn default() -> Self {
vector(0.0, 0.0)
}
}
impl From<(f32, f32)> for Vector {
fn from(tuple: (f32, f32)) -> Self {
vector(tuple.0, tuple.1)
}
}
impl From<[f32; 2]> for Vector {
fn from(array: [f32; 2]) -> Self {
vector(array[0], array[1])
}
}
impl Neg for Vector {
type Output = Self;
fn neg(self) -> Self::Output {
vector(-self.x, -self.y)
}
}
impl<T: Into<Self>> Add<T> for Vector {
type Output = Self;
fn add(self, v: T) -> Self {
let v = v.into();
vector(self.x + v.x, self.y + v.y)
}
}
impl<T: Into<Self>> AddAssign<T> for Vector {
fn add_assign(&mut self, v: T) {
let v = v.into();
self.x += v.x;
self.y += v.y;
}
}
impl<T: Into<Self>> Sub<T> for Vector {
type Output = Self;
fn sub(self, v: T) -> Self {
let v = v.into();
vector(self.x - v.x, self.y - v.y)
}
}
impl<T: Into<Self>> SubAssign<T> for Vector {
fn sub_assign(&mut self, v: T) {
let v = v.into();
self.x -= v.x;
self.y -= v.y;
}
}
impl<T: Into<Self>> Mul<T> for Vector {
type Output = Self;
fn mul(self, v: T) -> Self {
let v = v.into();
vector(self.x * v.x, self.y * v.y)
}
}
impl<T: Into<Self>> MulAssign<T> for Vector {
fn mul_assign(&mut self, v: T) {
let v = v.into();
self.x *= v.x;
self.y *= v.y;
}
}
impl<T: Into<Self>> Div<T> for Vector {
type Output = Self;
fn div(self, v: T) -> Self {
let v = v.into();
vector(self.x / v.x, self.y / v.y)
}
}
impl<T: Into<Self>> DivAssign<T> for Vector {
fn div_assign(&mut self, v: T) {
let v = v.into();
self.x /= v.x;
self.y /= v.y;
}
}
impl Mul<f32> for Vector {
type Output = Self;
fn mul(self, s: f32) -> Self {
vector(self.x * s, self.y * s)
}
}
impl MulAssign<f32> for Vector {
fn mul_assign(&mut self, s: f32) {
self.x *= s;
self.y *= s;
}
}
impl Div<f32> for Vector {
type Output = Self;
fn div(self, s: f32) -> Self {
vector(self.x / s, self.y / s)
}
}
impl DivAssign<f32> for Vector {
fn div_assign(&mut self, s: f32) {
self.x /= s;
self.y /= s;
}
}