Skip to main content

iced_core/
vector.rs

1/// A 2D vector.
2#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
3pub struct Vector<T = f32> {
4    /// The X component of the [`Vector`]
5    pub x: T,
6
7    /// The Y component of the [`Vector`]
8    pub y: T,
9}
10
11impl<T> Vector<T> {
12    /// Creates a new [`Vector`] with the given components.
13    pub const fn new(x: T, y: T) -> Self {
14        Self { x, y }
15    }
16}
17
18impl Vector {
19    /// The zero [`Vector`].
20    pub const ZERO: Self = Self::new(0.0, 0.0);
21
22    /// Rounds the [`Vector`].
23    pub fn round(self) -> Self {
24        Self {
25            x: self.x.round(),
26            y: self.y.round(),
27        }
28    }
29
30    /// Computes the logical [`Vector`] that ends up rounded
31    /// after the given `scale_factor` is applied to it.
32    pub fn hint(self, scale_factor: f32) -> Self {
33        (self * scale_factor).round() / scale_factor
34    }
35}
36
37impl<T> std::ops::Neg for Vector<T>
38where
39    T: std::ops::Neg<Output = T>,
40{
41    type Output = Self;
42
43    fn neg(self) -> Self::Output {
44        Self::new(-self.x, -self.y)
45    }
46}
47
48impl<T> std::ops::Add for Vector<T>
49where
50    T: std::ops::Add<Output = T>,
51{
52    type Output = Self;
53
54    fn add(self, b: Self) -> Self {
55        Self::new(self.x + b.x, self.y + b.y)
56    }
57}
58
59impl<T> std::ops::AddAssign for Vector<T>
60where
61    T: std::ops::AddAssign,
62{
63    fn add_assign(&mut self, b: Self) {
64        self.x += b.x;
65        self.y += b.y;
66    }
67}
68
69impl<T> std::ops::Sub for Vector<T>
70where
71    T: std::ops::Sub<Output = T>,
72{
73    type Output = Self;
74
75    fn sub(self, b: Self) -> Self {
76        Self::new(self.x - b.x, self.y - b.y)
77    }
78}
79
80impl<T> std::ops::SubAssign for Vector<T>
81where
82    T: std::ops::SubAssign,
83{
84    fn sub_assign(&mut self, b: Self) {
85        self.x -= b.x;
86        self.y -= b.y;
87    }
88}
89
90impl<T> std::ops::Mul<T> for Vector<T>
91where
92    T: std::ops::Mul<Output = T> + Copy,
93{
94    type Output = Self;
95
96    fn mul(self, scale: T) -> Self {
97        Self::new(self.x * scale, self.y * scale)
98    }
99}
100
101impl<T> std::ops::MulAssign<T> for Vector<T>
102where
103    T: std::ops::MulAssign + Copy,
104{
105    fn mul_assign(&mut self, scale: T) {
106        self.x *= scale;
107        self.y *= scale;
108    }
109}
110
111impl<T> std::ops::Div<T> for Vector<T>
112where
113    T: std::ops::Div<Output = T> + Copy,
114{
115    type Output = Self;
116
117    fn div(self, scale: T) -> Self {
118        Self::new(self.x / scale, self.y / scale)
119    }
120}
121
122impl<T> std::ops::DivAssign<T> for Vector<T>
123where
124    T: std::ops::DivAssign + Copy,
125{
126    fn div_assign(&mut self, scale: T) {
127        self.x /= scale;
128        self.y /= scale;
129    }
130}
131
132impl<T> From<[T; 2]> for Vector<T> {
133    fn from([x, y]: [T; 2]) -> Self {
134        Self::new(x, y)
135    }
136}
137
138impl<T> From<Vector<T>> for [T; 2]
139where
140    T: Copy,
141{
142    fn from(other: Vector<T>) -> Self {
143        [other.x, other.y]
144    }
145}