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
pub trait Vector: Clone {
    type Scalar: Copy;

    fn length_squared(&self) -> Self::Scalar;

    fn scale(&mut self, factor: Self::Scalar);

    fn sub(&self, other: &Self) -> Self;

    fn add_scaled(&mut self, factor: Self::Scalar, other: &Self);

    fn reset(&mut self);

    fn clip_within(&mut self, min: &Self, max: &Self);

    fn new() -> Self;
}

#[derive(Debug, Copy, Clone)]
pub struct P2d (pub f32, pub f32);

impl Vector for P2d {
    type Scalar = f32;

    fn new() -> Self {
        P2d (0.0, 0.0)
    }

    fn length_squared(&self) -> f32 {
        self.0 * self.0 + self.1 * self.1
    }

    fn reset(&mut self) {
        self.0 = 0.0;
        self.1 = 0.0;
    }

    fn scale(&mut self, factor: f32) {
        self.0 *= factor;
        self.1 *= factor;
    }

    fn sub(&self, other: &Self) -> Self {
        P2d (self.0 - other.0, self.1 - other.1)
    }

    fn add_scaled(&mut self, factor: f32, other: &P2d) {
        self.0 += factor * other.0;
        self.1 += factor * other.1;
    }

    fn clip_within(&mut self, min: &Self, max: &Self) {
        if self.0 < min.0 {
            self.0 = min.0
        } else if self.0 > max.0 {
            self.0 = max.0
        }

        if self.1 < min.1 {
            self.1 = min.1
        } else if self.1 > max.1 {
            self.1 = max.1
        }
    }
}