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
|
use crate::*;
use std::fmt;
use std::ops::{Add, Sub, AddAssign, SubAssign};
#[derive(Clone, Copy, PartialEq)]
pub struct Rect<O: Internal, D: Internal> {
pub origin: Point<O>,
pub dimensions: Dimensions<D>,
}
pub trait HasRect<O: Internal, D: Internal>: HasPosition<O> + HasDimensions<D> {
fn rect(&self) -> Rect<O, D>;
}
impl<O: Internal, D: Internal> Rect<O, D> {
pub const ZERO: Rect<O, D> = Rect::construct(Point::<O>::ZERO, Dimensions::<D>::ZERO);
pub const fn new(origin_x: O, origin_y: O, width: D, height: D) -> Self {
let origin = Point::new(origin_x, origin_y);
let dimensions = Dimensions::new(width, height);
Self { origin, dimensions }
}
pub const fn construct(origin: Point<O>, dimensions: Dimensions<D>) -> Self {
Self { origin, dimensions }
}
}
impl<O: Internal, D: Internal> HasDimensions<D> for Rect<O, D> {
fn dimensions(&self) -> Dimensions<D> { self.dimensions }
}
impl<O: Internal, D: Internal> HasPosition<O> for Rect<O, D> {
fn position(&self) -> Point<O> { self.origin }
}
impl<O: Internal, D: Internal, T: HasDimensions<D> + HasPosition<O>> HasRect<O, D> for T {
fn rect(&self) -> Rect<O, D> {
Rect::construct(self.position(), self.dimensions())
}
}
impl<O:Internal, D: Internal> Add<Point<O>> for Rect<O, D> {
type Output = Rect<O,D>;
fn add(self, point: Point<O>) -> Self::Output {
Rect::construct(self.origin + point, self.dimensions)
}
}
impl<O: Internal, D: Internal> Sub<Point<O>> for Rect<O, D> {
type Output = Rect<O,D>;
fn sub(self, point: Point<O>) -> Self::Output {
Rect::construct(self.origin - point, self.dimensions)
}
}
impl<O: Internal, D: Internal> AddAssign<Point<O>> for Rect<O, D> {
fn add_assign(&mut self, point: Point<O>) {
self.origin += point
}
}
impl<O: Internal, D: Internal> SubAssign<Point<O>> for Rect<O, D> {
fn sub_assign(&mut self, point: Point<O>) {
self.origin -= point
}
}
impl<O: Internal, D: Internal> fmt::Debug for Rect<O, D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Rect {{ x:{:?} y:{:?} w:{:?} h:{:?} }}",
self.origin.x, self.origin.y,
self.dimensions.width, self.dimensions.height,
)
}
}
// -----------------------------------------------------------------------------
// Macro-generated code
// PartialOrd versions of std::cmp::min/max
fn min<T: std::cmp::PartialOrd>(a: T, b: T) -> T {
match a < b { true => a, false => b }
}
fn max<T: std::cmp::PartialOrd>(a: T, b: T) -> T {
match a > b { true => a, false => b }
}
macro_rules! impl_for_types {
($O:ty, $D:ty) => {
impl Rect<$O, $D> {
pub const fn from_dimensions(dimensions: Dimensions<$D>) -> Self {
Self {
origin: Point::<$O>::ZERO,
dimensions,
}
}
pub fn from_points(point1: Point<$O>, point2: Point<$O>) -> Self {
let (left, right) = match point1.x < point2.x {
true => (point1.x, point2.x),
false => (point2.x, point1.x),
};
let (top, bottom) = match point1.y < point2.y {
true => (point1.y, point2.y),
false => (point2.y, point1.y),
};
let width = (right - left) as $D;
let height = (bottom - top) as $D;
Self {
origin: Point::new(left, top),
dimensions: Dimensions::new(width, height),
}
}
/// Returns Rect(0,0,0,0) where no intersection exists.
pub fn intersect(&self, other: Rect<$O, $D>) -> Self {
let left = max(self.origin.x, other.origin.x);
let top = max(self.origin.y, other.origin.y);
let right_self = self.origin.x + (self.width() as $O);
let right_other = other.origin.x + (other.width() as $O);
let bottom_self = self.origin.y + (self.height() as $O);
let bottom_other = other.origin.y + (other.height() as $O);
let right = min(right_self, right_other);
let bottom = min(bottom_self, bottom_other);
if right <= left || bottom <= top {
Rect::<$O, $D>::ZERO
} else {
let width = (right - left) as $D;
let height = (bottom - top) as $D;
Rect::construct(Point::new(left, top), Dimensions::new(width, height))
}
}
pub fn contains_point(&self, point: Point<$O>) -> bool {
let left = self.origin.x;
let top = self.origin.y;
let right = left + (self.width() as $O);
let bottom = top + (self.height() as $O);
point.x >= left && point.x < right && point.y >= top && point.y < bottom
}
pub fn include_point(&mut self, _point: Point<$O>) {
todo!()
}
/// Return the inclusive left x-coordinate.
pub fn left(&self) -> $O {
self.origin.x
}
/// Return the inclusive top y-coordinate.
pub fn top(&self) -> $O {
self.origin.y
}
/// Return the exclusive right x-coordinate.
pub fn right(&self) -> $O {
self.left() + self.width() as $O
}
/// Return the exclusive bottom y-coordinate.
pub fn bottom(&self) -> $O {
self.top() + self.height() as $O
}
}
impl From<Dimensions<$D>> for Rect<$O, $D> {
fn from(dimensions: Dimensions<$D>) -> Rect<$O, $D> {
Rect::<$O, $D>::from_dimensions(dimensions)
}
}
};
}
impl_for_types!(u8, u8);
impl_for_types!(u16, u16);
impl_for_types!(u32, u32);
impl_for_types!(u64, u64);
impl_for_types!(u128, u128);
impl_for_types!(usize, usize);
impl_for_types!(i8, u8);
impl_for_types!(i16, u16);
impl_for_types!(i32, u32);
impl_for_types!(i64, u64);
impl_for_types!(i128, u128);
impl_for_types!(isize, usize);
impl_for_types!(f32, f32);
impl_for_types!(f64, f64);
|