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
|
use crate::*;
use std::fmt;
#[derive(Copy, Clone, PartialEq)]
pub struct Dimensions<D: Internal> {
pub width: D,
pub height: D,
}
pub trait HasDimensions<D: Internal> {
fn dimensions(&self) -> Dimensions<D>;
fn width(&self) -> D {
self.dimensions().width
}
fn height(&self) -> D {
self.dimensions().height
}
fn area_usize(&self) -> usize {
self.dimensions().width.as_usize() * self.dimensions().height.as_usize()
}
fn area_f64(&self) -> f64 {
self.dimensions().width.as_f64() * self.dimensions().height.as_f64()
}
fn contains_point(&self, point: Point<D>) -> bool {
point.x < self.width() && point.y < self.height()
}
}
impl<D: Internal> Dimensions<D> {
pub const ZERO: Self = Self { width: D::ZERO, height: D::ZERO };
pub const fn new(width: D, height: D) -> Self {
Dimensions { width, height }
}
pub fn is_zero(&self) -> bool {
self.width == D::ZERO && self.height == D::ZERO
}
}
impl<D: Internal> HasDimensions<D> for Dimensions<D> {
fn dimensions(&self) -> Dimensions<D> {
*self
}
}
impl<O: Internal, D: Internal> From<Rect<O, D>> for Dimensions<D> {
fn from(rect: Rect<O, D>) -> Self {
rect.dimensions
}
}
impl<D: Internal> fmt::Debug for Dimensions<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Dimensions {{ w:{:?} h:{:?} }}", self.width, self.height)
}
}
impl<D: Internal> std::ops::Mul<D> for Dimensions<D> {
type Output = Self;
fn mul(mut self, scale: D) -> Self {
self.width *= scale;
self.height *= scale;
return self;
}
}
impl<D: Internal> std::ops::MulAssign<D> for Dimensions<D> {
fn mul_assign(&mut self, scale: D) {
self.width *= scale;
self.height *= scale;
}
}
impl<D: Internal> std::ops::Div<D> for Dimensions<D> {
type Output = Self;
fn div(mut self, scale: D) -> Self {
self.width /= scale;
self.height /= scale;
return self;
}
}
impl<D: Internal> std::ops::DivAssign<D> for Dimensions<D> {
fn div_assign(&mut self, scale: D) {
self.width /= scale;
self.height /= scale;
}
}
|