1use std::ops::{Add, Mul, Neg, Sub};
2
3#[derive(Debug, Clone, Copy, PartialEq)]
4pub struct Vector2 {
5 pub x: f32,
6 pub y: f32,
7}
8
9impl Vector2 {
10 pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
11 pub const ONE: Self = Self { x: 1.0, y: 1.0 };
12
13 pub const fn new(x: f32, y: f32) -> Self {
14 Self { x, y }
15 }
16
17 pub fn set(&mut self, x: f32, y: f32) -> &mut Self {
18 self.x = x;
19 self.y = y;
20 self
21 }
22
23 pub fn length(&self) -> f32 {
24 self.length_sq().sqrt()
25 }
26
27 pub fn length_sq(&self) -> f32 {
28 self.x * self.x + self.y * self.y
29 }
30
31 pub fn normalize(&self) -> Self {
32 let len = self.length();
33 if len == 0.0 {
34 Self::ZERO
35 } else {
36 *self * (1.0 / len)
37 }
38 }
39
40 pub fn dot(&self, other: Self) -> f32 {
41 self.x * other.x + self.y * other.y
42 }
43
44 pub fn cross(&self, other: Self) -> f32 {
46 self.x * other.y - self.y * other.x
47 }
48
49 pub fn distance_to(&self, other: Self) -> f32 {
50 (*self - other).length()
51 }
52
53 pub fn distance_to_sq(&self, other: Self) -> f32 {
54 (*self - other).length_sq()
55 }
56
57 pub fn lerp(&self, other: Self, t: f32) -> Self {
58 *self + (other - *self) * t
59 }
60
61 pub fn angle(&self) -> f32 {
62 self.y.atan2(self.x)
63 }
64
65 pub fn rotate_around(&self, center: Self, angle: f32) -> Self {
66 let c = angle.cos();
67 let s = angle.sin();
68 let x = self.x - center.x;
69 let y = self.y - center.y;
70 Self::new(x * c - y * s + center.x, x * s + y * c + center.y)
71 }
72
73 pub fn min(&self, other: Self) -> Self {
74 Self::new(self.x.min(other.x), self.y.min(other.y))
75 }
76
77 pub fn max(&self, other: Self) -> Self {
78 Self::new(self.x.max(other.x), self.y.max(other.y))
79 }
80
81 pub fn clamp(&self, min: Self, max: Self) -> Self {
82 Self::new(self.x.clamp(min.x, max.x), self.y.clamp(min.y, max.y))
83 }
84
85 pub fn to_array(&self) -> [f32; 2] {
86 [self.x, self.y]
87 }
88}
89
90impl Add for Vector2 {
91 type Output = Self;
92 fn add(self, rhs: Self) -> Self {
93 Self::new(self.x + rhs.x, self.y + rhs.y)
94 }
95}
96
97impl Sub for Vector2 {
98 type Output = Self;
99 fn sub(self, rhs: Self) -> Self {
100 Self::new(self.x - rhs.x, self.y - rhs.y)
101 }
102}
103
104impl Mul<f32> for Vector2 {
105 type Output = Self;
106 fn mul(self, rhs: f32) -> Self {
107 Self::new(self.x * rhs, self.y * rhs)
108 }
109}
110
111impl Neg for Vector2 {
112 type Output = Self;
113 fn neg(self) -> Self {
114 Self::new(-self.x, -self.y)
115 }
116}
117
118#[cfg(test)]
119mod tests {
120 use super::*;
121
122 #[test]
123 fn normalize_unit_length() {
124 let v = Vector2::new(3.0, 4.0).normalize();
125 assert!((v.length() - 1.0).abs() < 1e-6);
126 }
127
128 #[test]
129 fn cross_z_component() {
130 let a = Vector2::new(1.0, 0.0);
131 let b = Vector2::new(0.0, 1.0);
132 assert!((a.cross(b) - 1.0).abs() < 1e-6);
133 }
134
135 #[test]
136 fn rotate_around_origin_90() {
137 let p = Vector2::new(1.0, 0.0);
138 let r = p.rotate_around(Vector2::ZERO, std::f32::consts::FRAC_PI_2);
139 assert!((r.x - 0.0).abs() < 1e-6);
140 assert!((r.y - 1.0).abs() < 1e-6);
141 }
142}