orbtk_utils/
size.rs

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
use derive_more::{Add, Constructor, From, Sub};
use std::ops::Div;

/// A `Size` specified by width and height.
///
/// # Examples
/// ```rust
/// # use orbtk_utils::Size;
/// let size = Size::new(10., 10.);
/// let other_size = Size::new(5., 7.);
/// let result = size - other_size;
///
/// assert_eq!(result.width(), 5.);
/// assert_eq!(result.height(), 3.);
/// ```
#[derive(Constructor, Add, Sub, Copy, From, Clone, Default, Debug, PartialEq)]
pub struct Size {
    width: f64,
    height: f64,
}

impl Size {
    /// Gets the width of the size.
    pub fn width(&self) -> f64 {
        self.width
    }

    /// Sets the width of the size.
    pub fn set_width(&mut self, width: f64) {
        self.width = width;
    }

    /// Gets the height of the size.
    pub fn height(&self) -> f64 {
        self.height
    }

    /// Sets the height of the size.
    pub fn set_height(&mut self, height: f64) {
        self.height = height;
    }
}

// Operations

impl Div<f64> for Size {
    type Output = Size;

    fn div(mut self, rhs: f64) -> Self::Output {
        self.width /= rhs;
        self.height /= rhs;
        self
    }
}

impl Div<Size> for f64 {
    type Output = Size;

    fn div(self, mut rhs: Size) -> Self::Output {
        rhs.width /= self;
        rhs.height /= self;
        rhs
    }
}

// --- Conversions ---

impl From<f64> for Size {
    fn from(t: f64) -> Self {
        Size::new(t, t)
    }
}

impl From<i32> for Size {
    fn from(t: i32) -> Self {
        Size::new(t as f64, t as f64)
    }
}

impl From<(i32, i32)> for Size {
    fn from(s: (i32, i32)) -> Size {
        Size::from((s.0 as f64, s.1 as f64))
    }
}

// --- Conversions ---

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sub() {
        let expected_result = Size::new(-3., 5.);
        const ERROR_MARGIN: f64 = 0.00001;

        let left_side = Size::new(5., 7.);
        let right_side = Size::new(8., 2.);

        let result = left_side - right_side;

        assert!((result.width() - expected_result.width()).abs() < ERROR_MARGIN);
        assert!((result.height() - expected_result.height()).abs() < ERROR_MARGIN);
    }

    #[test]
    fn test_add() {
        let expected_result = Size::new(13., 9.);
        const ERROR_MARGIN: f64 = 0.00001;

        let left_side = Size::new(5., 7.);
        let right_side = Size::new(8., 2.);

        let result = left_side + right_side;

        assert!((result.width() - expected_result.width()).abs() < ERROR_MARGIN);
        assert!((result.height() - expected_result.height()).abs() < ERROR_MARGIN);
    }
}