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
use kurbo::{Affine, Vec2};
use std::ops::{
    Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign,
};

/// Your typical 2D vector.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Vector {
    pub x: f64,
    pub y: f64,
}

impl Vector {
    /// Create a new [`Vector`].
    ///
    /// # Panics
    ///
    /// This will panic if `x` or `y` aren't finite.
    pub fn new(x: f64, y: f64) -> Self {
        assert!(x.is_finite(), "Can't create a vector with {}", x);
        assert!(y.is_finite(), "Can't create a vector with {}", y);

        Vector::new_unchecked(x, y)
    }

    pub const fn new_unchecked(x: f64, y: f64) -> Self { Vector { x, y } }

    pub fn from_r_theta(radius: f64, angle: f64) -> Self {
        Vector::new(radius * angle.cos(), radius * angle.sin())
    }

    pub const fn zero() -> Vector { Vector::new_unchecked(0.0, 0.0) }

    pub const fn x_axis() -> Vector { Vector::new_unchecked(1.0, 0.0) }

    pub const fn y_axis() -> Vector { Vector::new_unchecked(0.0, 1.0) }

    pub fn length(self) -> f64 { self.x.hypot(self.y) }

    pub fn angle(self) -> f64 { f64::atan2(self.y, self.x) }

    pub fn unit_vector(self) -> Vector {
        let magnitude = self.length();
        if magnitude == 0.0 {
            Vector::zero()
        } else {
            self / magnitude
        }
    }

    pub fn orientation(
        first: Vector,
        second: Vector,
        third: Vector,
    ) -> Orientation {
        let value = (second.y - first.y) * (third.x - second.x)
            - (second.x - first.x) * (third.y - second.y);

        if value > 0.0 {
            Orientation::Clockwise
        } else if value < 0.0 {
            Orientation::Anticlockwise
        } else {
            Orientation::Collinear
        }
    }

    pub fn dot(left: Vector, right: Vector) -> f64 {
        left.x * right.x + left.y * right.y
    }

    pub fn cross(left: Vector, right: Vector) -> f64 {
        left.x * right.y - right.x * left.y
    }

    pub fn lerp(start: Vector, end: Vector, progress: f64) -> Vector {
        start + (end - start) * progress
    }

    pub fn centre_of_three_points(
        first: Vector,
        second: Vector,
        third: Vector,
    ) -> Option<Vector> {
        let temp = Vector::dot(second, second);
        let bc = (Vector::dot(first, first) - temp) / 2.0;
        let cd = (temp - third.x * third.x - third.y * third.y) / 2.0;
        let determinant = (first.x - second.x) * (second.y - third.y)
            - (second.x - third.x) * (first.y - second.y);

        if determinant == 0.0 {
            // the points are collinear
            return None;
        }

        let x = (bc * (second.y - third.y) - cd * (first.y - second.y))
            / determinant;
        let y = ((first.x - second.x) * cd - (second.x - third.x) * bc)
            / determinant;

        Some(Vector::new(x, y))
    }

    pub fn rotated(self, angle: f64) -> Vector {
        Vector::from_r_theta(self.length(), self.angle() + angle)
    }
}

impl Add for Vector {
    type Output = Vector;

    fn add(self, other: Vector) -> Vector {
        Vector::new(self.x + other.x, self.y + other.y)
    }
}

impl AddAssign for Vector {
    fn add_assign(&mut self, other: Vector) { *self = *self + other; }
}

impl Sub for Vector {
    type Output = Vector;

    fn sub(self, other: Vector) -> Vector {
        Vector::new(self.x - other.x, self.y - other.y)
    }
}

impl SubAssign for Vector {
    fn sub_assign(&mut self, other: Vector) { *self = *self - other; }
}

impl Mul<f64> for Vector {
    type Output = Vector;

    fn mul(self, other: f64) -> Vector {
        Vector::new(self.x * other, self.y * other)
    }
}

impl Mul<Vector> for f64 {
    type Output = Vector;

    fn mul(self, other: Vector) -> Vector { other * self }
}

impl MulAssign<f64> for Vector {
    fn mul_assign(&mut self, other: f64) { *self = *self * other; }
}

impl Mul<Vector> for Affine {
    type Output = Vector;

    fn mul(self, other: Vector) -> Vector {
        use kurbo::Point;

        let temporary = Point::new(other.x, other.y);
        let Point { x, y } = self * temporary;
        Vector::new(x, y)
    }
}

impl MulAssign<Affine> for Vector {
    fn mul_assign(&mut self, other: Affine) { *self = other.mul(*self); }
}

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

    fn div(self, other: f64) -> Vector {
        assert!(other.is_normal(), "Unable to divide by {}", other);
        Vector::new_unchecked(self.x / other, self.y / other)
    }
}

impl DivAssign<f64> for Vector {
    fn div_assign(&mut self, other: f64) { *self = *self / other; }
}

impl From<Vector> for Vec2 {
    fn from(v: Vector) -> Self {
        Vec2::new(v.x, v.y)
    }
}

/// How something may be oriented.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Orientation {
    Clockwise,
    Anticlockwise,
    Collinear,
}

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

    #[test]
    fn add_two_vectors() {
        let left = Vector::new(1.0, 2.0);
        let right = Vector::new(-20.0, 2.5);
        let expected = Vector::new(-19.0, 4.5);

        let got = left + right;

        assert_eq!(got, expected);
    }

    #[test]
    fn subtract_two_vectors() {
        let left = Vector::new(1.0, 2.0);
        let right = Vector::new(-20.0, 2.5);
        let expected = Vector::new(1.0 - -20.0, 2.0 - 2.5);

        let got = left - right;

        assert_eq!(got, expected);
    }

    #[test]
    fn multiply_by_two() {
        let left = Vector::new(-20.0, 2.5);
        let expected = Vector::new(-20.0 * 2.0, 2.5 * 2.0);

        let got = left * 2.0;

        assert_eq!(got, expected);
    }

    #[test]
    fn divide_by_two() {
        let left = Vector::new(-20.0, 2.5);
        let expected = Vector::new(-20.0 / 2.0, 2.5 / 2.0);

        let got = left / 2.0;

        assert_eq!(got, expected);
    }

    #[test]
    #[should_panic(expected = "divide by 0")]
    fn divide_by_zero() {
        let left = Vector::new(-20.0, 2.5);

        let _ = left / 0.0;
    }

    #[test]
    fn find_centre_of_three_points() {
        let a = Vector::new(1.0, 0.0);
        let b = Vector::new(-1.0, 0.0);
        let c = Vector::new(0.0, 1.0);

        let centre = Vector::centre_of_three_points(a, b, c).unwrap();

        assert_eq!(centre, Vector::zero());
    }

    #[test]
    fn find_a_quarter_of_the_way_between_points() {
        let start = Vector::new(0.0, 0.0);
        let end = Vector::new(40.0, 8.0);
        let expected = Vector::new(10.0, 2.0);

        let got = Vector::lerp(start, end, 0.25);

        assert_eq!(got, expected);
    }
}