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
//! A horizontal and vertical rectangular dimension with no specified location.

use std::cmp::Ordering;

use serde::{Deserialize, Serialize};

use crate::dir::Dir;
use crate::point::Point;
use crate::rect::Rect;

/// A horizontal and vertical rectangular dimension with no specified location.
#[derive(
    Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, Serialize, Deserialize,
)]
pub struct Dims {
    /// The width dimension.
    w: i64,
    /// The height dimension.
    h: i64,
}

impl Dims {
    /// Creates a new [`Dims`] from a width and height.
    pub fn new(w: i64, h: i64) -> Self {
        Self { w, h }
    }
    /// Creates a new [`Dims`] with width and height equal to `value`.
    ///
    /// # Example
    ///
    /// ```
    /// # use geometry::prelude::*;
    /// assert_eq!(Dims::square(100), Dims::new(100, 100));
    /// ```
    pub fn square(value: i64) -> Self {
        Self { w: value, h: value }
    }

    /// Returns the dimension in the specified direction.
    ///
    /// # Example
    ///
    /// ```
    /// # use geometry::prelude::*;
    /// let dims = Dims::new(100, 200);
    /// assert_eq!(dims.dim(Dir::Vert), 200);
    /// assert_eq!(dims.dim(Dir::Horiz), 100);
    /// ```
    pub fn dim(&self, dir: Dir) -> i64 {
        match dir {
            Dir::Vert => self.h,
            Dir::Horiz => self.w,
        }
    }

    /// Returns the direction of the longer dimension.
    ///
    /// If the width and height are equal, returns [`Dir::Horiz`].
    ///
    /// # Example
    ///
    /// ```
    /// # use geometry::prelude::*;
    /// let dims = Dims::new(100, 200);
    /// assert_eq!(dims.longer_dir(), Dir::Vert);
    /// let dims = Dims::new(200, 100);
    /// assert_eq!(dims.longer_dir(), Dir::Horiz);
    /// let dims = Dims::new(100, 100);
    /// assert_eq!(dims.longer_dir(), Dir::Horiz);
    /// ```
    pub fn longer_dir(&self) -> Dir {
        if self.w >= self.h {
            Dir::Horiz
        } else {
            Dir::Vert
        }
    }

    /// Returns the direction of the longer dimension.
    ///
    /// If the width and height are equal, returns [`None`].
    /// Otherwise, returns a `Some` variant containing the longer direction.
    pub fn longer_dir_strict(&self) -> Option<Dir> {
        match self.w.cmp(&self.h) {
            Ordering::Greater => Some(Dir::Horiz),
            Ordering::Equal => None,
            Ordering::Less => Some(Dir::Vert),
        }
    }

    /// Returns a new [`Dims`] object with the horizontal and vertical dimensions flipped.
    pub fn transpose(self) -> Self {
        Self {
            w: self.h,
            h: self.w,
        }
    }

    /// Returns the width (i.e. the horizontal dimension).
    #[inline]
    pub fn width(&self) -> i64 {
        self.w
    }

    /// Returns the height (i.e. the vertical dimension).
    #[inline]
    pub fn height(&self) -> i64 {
        self.h
    }

    /// Returns the width (i.e. the horizontal dimension).
    ///
    /// A shorthand for [`Dims::width`].
    #[inline]
    pub fn w(&self) -> i64 {
        self.width()
    }

    /// Returns the height (i.e. the vertical dimension).
    ///
    /// A shorthand for [`Dims::height`].
    #[inline]
    pub fn h(&self) -> i64 {
        self.height()
    }

    /// Converts this dimension object into a [`Rect`].
    ///
    /// See [`Rect::from_dims`] for more information.
    #[inline]
    pub fn into_rect(self) -> Rect {
        Rect::from_dims(self)
    }

    /// Converts this dimension object into a [`Point`] with coordinates `(self.w(), self.h())`.
    #[inline]
    pub fn into_point(self) -> Point {
        Point::new(self.w(), self.h())
    }
}

impl std::ops::Add<Dims> for Dims {
    type Output = Self;
    fn add(self, rhs: Dims) -> Self::Output {
        Self {
            w: self.w + rhs.w,
            h: self.h + rhs.h,
        }
    }
}

impl std::ops::Sub<Dims> for Dims {
    type Output = Self;
    fn sub(self, rhs: Dims) -> Self::Output {
        Self {
            w: self.w - rhs.w,
            h: self.h - rhs.h,
        }
    }
}

impl std::ops::Mul<i64> for Dims {
    type Output = Self;
    fn mul(self, rhs: i64) -> Self::Output {
        Self {
            w: self.w * rhs,
            h: self.h * rhs,
        }
    }
}

impl std::ops::Mul<(usize, usize)> for Dims {
    type Output = Self;
    fn mul(self, rhs: (usize, usize)) -> Self::Output {
        Self {
            w: self.w * rhs.0 as i64,
            h: self.h * rhs.1 as i64,
        }
    }
}

impl std::ops::AddAssign<Dims> for Dims {
    fn add_assign(&mut self, rhs: Dims) {
        self.w += rhs.w;
        self.h += rhs.h;
    }
}

impl std::ops::SubAssign<Dims> for Dims {
    fn sub_assign(&mut self, rhs: Dims) {
        self.w -= rhs.w;
        self.h -= rhs.h;
    }
}

impl std::ops::MulAssign<i64> for Dims {
    fn mul_assign(&mut self, rhs: i64) {
        self.w *= rhs;
        self.h *= rhs;
    }
}

impl From<Rect> for Dims {
    /// Obtains [`Dims`] from the given [`Rect`] using [`Rect::dims`].
    #[inline]
    fn from(value: Rect) -> Self {
        value.dims()
    }
}

impl From<Point> for Dims {
    /// Create a new dimension object from a point.
    ///
    /// The width field of the resulting [`Dims`] will be the point's x-coordinate.
    /// The height field of the resulting [`Dims`] will be the point's y-coordinate.
    #[inline]
    fn from(value: Point) -> Self {
        Self::new(value.x, value.y)
    }
}