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
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};

use crate::math::Mat;

#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Vec {
    pub x: f32,
    pub y: f32,
}

impl Vec {
    pub const fn new(x: f32, y: f32) -> Self {
        Self { x, y }
    }

    pub fn transform_dir(self, mat: &Mat) -> Self {
        Self {
            x: self.x * mat.scale_x + self.y * mat.shear_y,
            y: self.x * mat.shear_x + self.y * mat.scale_y,
        }
    }

    pub fn length(self) -> f32 {
        (self.x * self.x + self.y * self.y).sqrt()
    }

    pub fn distance(self, other: Self) -> f32 {
        (self - other).length()
    }

    pub fn normalize(self) -> Self {
        let length = self.length();

        if length > 0.0 {
            self * length.recip()
        } else {
            self
        }
    }

    pub fn dot(self, other: Self) -> f32 {
        self.x * other.x + self.y * other.y
    }

    pub fn lerp(self, other: Self, ratio: f32) -> Self {
        self + (other - self) * ratio
    }
}

impl Eq for Vec {}

impl Add for Vec {
    type Output = Self;

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

impl AddAssign for Vec {
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

impl Sub for Vec {
    type Output = Self;

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

impl SubAssign for Vec {
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

impl Mul for Vec {
    type Output = Self;

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

impl Mul<f32> for Vec {
    type Output = Self;

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

impl Div for Vec {
    type Output = Self;

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

impl Neg for Vec {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Self::new(-self.x, -self.y)
    }
}