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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use std::{fmt, ops};

use zng_var::{animation::Transitionable, impl_from_and_into_var};

use super::{
    impl_length_comp_conversions, Dip, DipVector, Factor, Factor2d, FactorPercent, Layout1d, LayoutMask, Length, LengthUnits, Point, Px,
    PxVector, Size, Transform,
};

/// 2D vector in [`Length`] units.
#[derive(Clone, Default, PartialEq, serde::Serialize, serde::Deserialize, Transitionable)]
pub struct Vector {
    /// *x* displacement in length units.
    pub x: Length,
    /// *y* displacement in length units.
    pub y: Length,
}
impl fmt::Debug for Vector {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if f.alternate() {
            f.debug_struct("Vector").field("x", &self.x).field("y", &self.y).finish()
        } else {
            write!(f, "({:?}, {:?})", self.x, self.y)
        }
    }
}
impl fmt::Display for Vector {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(p) = f.precision() {
            write!(f, "({:.p$}, {:.p$})", self.x, self.y, p = p)
        } else {
            write!(f, "({}, {})", self.x, self.y)
        }
    }
}
impl Vector {
    /// New x, y from any [`Length`] unit.
    pub fn new<X: Into<Length>, Y: Into<Length>>(x: X, y: Y) -> Self {
        Vector { x: x.into(), y: y.into() }
    }

    /// New x, y from single value of any [`Length`] unit.
    pub fn splat(xy: impl Into<Length>) -> Self {
        let xy = xy.into();
        Vector { x: xy.clone(), y: xy }
    }

    /// ***x:*** [`Length::zero`], ***y:*** [`Length::zero`].
    pub fn zero() -> Self {
        Self::new(Length::zero(), Length::zero())
    }

    /// `(1, 1)`.
    pub fn one() -> Self {
        Self::new(1, 1)
    }

    /// `(1.px(), 1.px())`.
    pub fn one_px() -> Self {
        Self::new(1.px(), 1.px())
    }

    /// Swap `x` and `y`.
    pub fn yx(self) -> Self {
        Vector { y: self.x, x: self.y }
    }

    /// Returns `(x, y)`.
    pub fn as_tuple(self) -> (Length, Length) {
        (self.x, self.y)
    }

    /// Returns `[x, y]`.
    pub fn as_array(self) -> [Length; 2] {
        [self.x, self.y]
    }

    /// Returns a vector that computes the absolute layout vector of `self`.
    pub fn abs(&self) -> Vector {
        Vector {
            x: self.x.abs(),
            y: self.y.abs(),
        }
    }

    /// Returns `true` if all values are [`Length::Default`].
    pub fn is_default(&self) -> bool {
        self.x.is_default() && self.y.is_default()
    }

    /// Replaces [`Length::Default`] values with `overwrite` values.
    pub fn replace_default(&mut self, overwrite: &Vector) {
        self.x.replace_default(&overwrite.x);
        self.y.replace_default(&overwrite.y);
    }

    /// Cast to [`Point`].
    pub fn as_point(self) -> Point {
        Point { x: self.x, y: self.y }
    }

    /// Cast to [`Size`].
    pub fn as_size(self) -> Size {
        Size {
            width: self.x,
            height: self.y,
        }
    }

    /// Create a translate transform from `self`.
    pub fn into_transform(self) -> Transform {
        Transform::new_translate(self.x, self.y)
    }
}
impl super::Layout2d for Vector {
    type Px = PxVector;

    fn layout_dft(&self, default: Self::Px) -> Self::Px {
        PxVector::new(self.x.layout_dft_x(default.x), self.y.layout_dft_y(default.y))
    }

    fn affect_mask(&self) -> LayoutMask {
        self.x.affect_mask() | self.y.affect_mask()
    }
}
impl_length_comp_conversions! {
    fn from(x: X, y: Y) -> Vector {
        Vector::new(x, y)
    }
}
impl_from_and_into_var! {
    fn from(p: PxVector) -> Vector {
        Vector::new(p.x, p.y)
    }
    fn from(p: DipVector) -> Vector {
        Vector::new(p.x, p.y)
    }
    fn from(p: Point) -> Vector {
        p.as_vector()
    }
    fn from(s: Size) -> Vector {
        s.as_vector()
    }

    /// Use the length for x and y.
    fn from(length: Length) -> Vector {
        Vector::splat(length)
    }

    /// Conversion to [`Length::Factor`] then to vector.
    fn from(percent: FactorPercent) -> Vector {
        Length::from(percent).into()
    }

    /// Conversion to [`Length::Factor`] then to vector.
    fn from(norm: Factor) -> Vector {
        Length::from(norm).into()
    }

    /// Conversion to [`Length::Dip`] then to vector.
    fn from(f: f32) -> Vector {
        Length::from(f).into()
    }

    /// Conversion to [`Length::Dip`] then to vector.
    fn from(i: i32) -> Vector {
        Length::from(i).into()
    }

    /// Conversion to [`Length::Px`] then to vector.
    fn from(l: Px) -> Vector {
        Length::from(l).into()
    }

    /// Conversion to [`Length::Dip`] then to vector.
    fn from(l: Dip) -> Vector {
        Length::from(l).into()
    }
}
impl ops::Add for Vector {
    type Output = Self;

    fn add(mut self, rhs: Self) -> Self {
        self += rhs;
        self
    }
}
impl<'a, 'b> ops::Add<&'a Vector> for &'b Vector {
    type Output = Vector;

    fn add(self, rhs: &'a Vector) -> Self::Output {
        self.clone() + rhs.clone()
    }
}
impl ops::AddAssign for Vector {
    fn add_assign(&mut self, rhs: Self) {
        self.x += rhs.x;
        self.y += rhs.y;
    }
}
impl ops::Sub for Vector {
    type Output = Self;

    fn sub(mut self, rhs: Self) -> Self {
        self -= rhs;
        self
    }
}
impl<'a, 'b> ops::Sub<&'a Vector> for &'b Vector {
    type Output = Vector;

    fn sub(self, rhs: &'a Vector) -> Self::Output {
        self.clone() - rhs.clone()
    }
}
impl ops::SubAssign for Vector {
    fn sub_assign(&mut self, rhs: Self) {
        self.x -= rhs.x;
        self.y -= rhs.y;
    }
}
impl<S: Into<Factor2d>> ops::Mul<S> for Vector {
    type Output = Self;

    fn mul(mut self, rhs: S) -> Self {
        self *= rhs;
        self
    }
}
impl<'a, S: Into<Factor2d>> ops::Mul<S> for &'a Vector {
    type Output = Vector;

    fn mul(self, rhs: S) -> Self::Output {
        self.clone() * rhs
    }
}
impl<S: Into<Factor2d>> ops::MulAssign<S> for Vector {
    fn mul_assign(&mut self, rhs: S) {
        let rhs = rhs.into();

        self.x *= rhs.x;
        self.y *= rhs.y;
    }
}
impl<S: Into<Factor2d>> ops::Div<S> for Vector {
    type Output = Self;

    fn div(mut self, rhs: S) -> Self {
        self /= rhs;
        self
    }
}
impl<'a, S: Into<Factor2d>> ops::Div<S> for &'a Vector {
    type Output = Vector;

    fn div(self, rhs: S) -> Self::Output {
        self.clone() / rhs
    }
}
impl<S: Into<Factor2d>> ops::DivAssign<S> for Vector {
    fn div_assign(&mut self, rhs: S) {
        let rhs = rhs.into();
        self.x /= rhs.x;
        self.y /= rhs.y;
    }
}
impl ops::Neg for Vector {
    type Output = Self;

    fn neg(self) -> Self {
        Vector { x: -self.x, y: -self.y }
    }
}
impl<'a> ops::Neg for &'a Vector {
    type Output = Vector;

    fn neg(self) -> Self::Output {
        -self.clone()
    }
}