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
//! Points, sizes and rectangles. Every unit is a logical pixel; the renderer
//! multiplies by the output's scale.
/// A point in logical pixels, measured from the window's top-left corner.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Point {
/// Distance from the left edge.
pub x: f32,
/// Distance from the top edge.
pub y: f32,
}
impl Point {
/// A point at `x`, `y`.
pub const fn new(x: f32, y: f32) -> Point {
Point { x, y }
}
}
/// A width and height in logical pixels.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Size {
/// The width.
pub width: f32,
/// The height.
pub height: f32,
}
impl Size {
/// A size of `width` by `height`.
pub const fn new(width: f32, height: f32) -> Size {
Size { width, height }
}
}
/// A rectangle in logical pixels: its top-left corner and its size.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Rect {
/// The left edge.
pub x: f32,
/// The top edge.
pub y: f32,
/// The width.
pub width: f32,
/// The height.
pub height: f32,
}
impl Rect {
/// A rectangle with its top-left corner at `x`, `y`.
pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Rect {
Rect {
x,
y,
width,
height,
}
}
/// The right edge.
pub fn right(&self) -> f32 {
self.x + self.width
}
/// The bottom edge.
pub fn bottom(&self) -> f32 {
self.y + self.height
}
/// Whether the rectangle covers no area.
pub fn is_empty(&self) -> bool {
self.width <= 0.0 || self.height <= 0.0
}
/// Whether `point` lies inside. The left and top edges count as inside,
/// the right and bottom edges don't, so two rectangles that touch never
/// both contain a point.
pub fn contains(&self, point: Point) -> bool {
point.x >= self.x && point.x < self.right() && point.y >= self.y && point.y < self.bottom()
}
/// The smallest rectangle that covers both. An empty rectangle adds
/// nothing.
pub fn union(&self, other: Rect) -> Rect {
if other.is_empty() {
return *self;
}
if self.is_empty() {
return other;
}
let x = self.x.min(other.x);
let y = self.y.min(other.y);
Rect::new(
x,
y,
self.right().max(other.right()) - x,
self.bottom().max(other.bottom()) - y,
)
}
/// The area both cover, or an empty rectangle if they don't overlap.
pub fn intersect(&self, other: Rect) -> Rect {
let x = self.x.max(other.x);
let y = self.y.max(other.y);
let right = self.right().min(other.right());
let bottom = self.bottom().min(other.bottom());
if right > x && bottom > y {
Rect::new(x, y, right - x, bottom - y)
} else {
Rect::default()
}
}
}