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
use std::fmt;

#[derive(Debug, Clone, Copy)]
pub struct Coords {
    x: i32,
    y: i32,
}

impl std::ops::Add for Coords {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
        }
    }
}

impl std::ops::Sub for Coords {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        Self {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
        }
    }
}

impl std::ops::Div<i32> for Coords {
    type Output = Self;

    fn div(self, rhs: i32) -> Self::Output {
        Self {
            x: self.x / rhs,
            y: self.y / rhs,
        }
    }
}

impl std::ops::Mul<i32> for Coords {
    type Output = Self;

    fn mul(self, rhs: i32) -> Self::Output {
        Self {
            x: self.x * rhs,
            y: self.y * rhs,
        }
    }
}

impl std::ops::Mul<f32> for Coords {
    type Output = Self;

    fn mul(self, rhs: f32) -> Self::Output {
        #[allow(clippy::cast_precision_loss)]
        #[allow(clippy::cast_possible_truncation)]
        Self {
            x: (self.x as f32 * rhs) as i32,
            y: (self.y as f32 * rhs) as i32,
        }
    }
}

impl From<(i32, i32)> for Coords {
    fn from((x, y): (i32, i32)) -> Self {
        Self { x, y }
    }
}

impl From<i32> for Coords {
    fn from(length: i32) -> Self {
        Self {
            x: length,
            y: length,
        }
    }
}

impl fmt::Display for Coords {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}x{}", self.x, self.y)
    }
}

pub struct Rect {
    min: Coords,
    max: Coords,
}

impl Rect {
    #[must_use]
    pub fn center(&self) -> Coords {
        (self.min + self.max) / 2
    }
}

impl Rect {
    #[must_use]
    pub fn scale(mut self, factor: f32) -> Self {
        let center = self.center();

        self.min = center + (self.min - center) * factor;
        self.max = center + (self.max - center) * factor;

        self
    }
}

impl<T: Into<Coords>> From<(T, T)> for Rect {
    fn from((min, max): (T, T)) -> Self {
        Self {
            min: min.into(),
            max: max.into(),
        }
    }
}

impl fmt::Display for Rect {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}", self.min, self.max)
    }
}