-
Notifications
You must be signed in to change notification settings - Fork 1
/
vec2.rs
217 lines (180 loc) · 4.95 KB
/
vec2.rs
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
use std::{fmt::Display, ops::Neg};
use auto_ops::{impl_op_ex, impl_op_ex_commutative};
use crate::Vec3;
#[derive(Debug, Clone, Copy, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub struct Vec2 {
pub x: f32,
pub y: f32,
}
impl Display for Vec2 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { x, y } = self;
write!(f, "({x}, {y})")
}
}
impl Vec2 {
/// The zero vector (0, 0)
pub const ZERO: Self = Self::new(0.0, 0.0);
/// The one vector (1, 1)
pub const ONE: Self = Self::new(1.0, 1.0);
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
/// Returns the square of the vector's length.
///
/// Faster to compute than [`magnitude()`](Self::magnitude())
pub fn sqr_magnitude(&self) -> f32 {
self.x * self.x + self.y * self.y
}
/// Returns the vector's length
pub fn magnitude(&self) -> f32 {
self.sqr_magnitude().sqrt()
}
/// Normalizes `self` in place
pub fn normalize(&mut self) -> &mut Self {
let m = self.magnitude();
self.x /= m;
self.y /= m;
self
}
/// Returns a normalized copy of `self`
#[must_use]
pub fn normalized(&self) -> Self {
*self.clone().normalize()
}
/// Returns the dot product of `self` and `b`
pub fn dot(&self, b: Vec2) -> f32 {
self.x * b.x + self.y * b.y
}
pub fn extend(&self, z: f32) -> Vec3 {
Vec3 {
x: self.x,
y: self.y,
z,
}
}
}
/// Vec2 swizzles
impl Vec2 {
swizzle!(x, x);
swizzle!(x, y);
swizzle!(y, x);
swizzle!(y, y);
swizzle!(x, x, x);
swizzle!(x, x, y);
swizzle!(x, y, x);
swizzle!(x, y, y);
swizzle!(y, x, x);
swizzle!(y, x, y);
swizzle!(y, y, x);
swizzle!(y, y, y);
swizzle!(x, x, x, x);
swizzle!(x, x, x, y);
swizzle!(x, x, y, x);
swizzle!(x, x, y, y);
swizzle!(x, y, x, x);
swizzle!(x, y, x, y);
swizzle!(x, y, y, x);
swizzle!(x, y, y, y);
swizzle!(y, x, x, x);
swizzle!(y, x, x, y);
swizzle!(y, x, y, x);
swizzle!(y, x, y, y);
swizzle!(y, y, x, x);
swizzle!(y, y, x, y);
swizzle!(y, y, y, x);
swizzle!(y, y, y, y);
}
impl_op_ex!(+= |a: &mut Vec2, b: &Vec2| { a.x += b.x; a.y += b.y; });
impl_op_ex!(-= |a: &mut Vec2, b: &Vec2| { a.x -= b.x; a.y -= b.y; });
impl_op_ex!(*= |a: &mut Vec2, b: &Vec2| { a.x *= b.x; a.y *= b.y; });
impl_op_ex!(/= |a: &mut Vec2, b: &Vec2| { a.x /= b.x; a.y /= b.y; });
impl_op_ex!(*= |a: &mut Vec2, b: &f32| { a.x *= b; a.y *= b });
impl_op_ex!(/= |a: &mut Vec2, b: &f32| { a.x /= b; a.y /= b });
impl_op_ex!(+ |a: &Vec2, b: &Vec2| -> Vec2 { Vec2{x: a.x + b.x, y: a.y + b.y} });
impl_op_ex!(-|a: &Vec2, b: &Vec2| -> Vec2 {
Vec2 {
x: a.x - b.x,
y: a.y - b.y,
}
});
impl_op_ex!(*|a: &Vec2, b: &Vec2| -> Vec2 {
Vec2 {
x: a.x * b.x,
y: a.y * b.y,
}
});
impl_op_ex!(/ |a: &Vec2, b: &Vec2| -> Vec2 { Vec2{x: a.x / b.x, y: a.y / b.y} });
impl_op_ex_commutative!(*|a: &Vec2, b: &f32| -> Vec2 {
Vec2 {
x: a.x * b,
y: a.y * b,
}
});
impl_op_ex!(/ |a: &Vec2, b: &f32| -> Vec2 { Vec2{x: a.x / b, y: a.y / b} });
impl_op_ex!(/ |a: &f32, b: &Vec2| -> Vec2 { Vec2{x: a / b.x, y: a / b.y} });
impl Neg for Vec2 {
type Output = Vec2;
fn neg(self) -> Self::Output {
Vec2 {
x: -self.x,
y: -self.y,
}
}
}
impl From<[f32; 2]> for Vec2 {
fn from(d: [f32; 2]) -> Self {
Self { x: d[0], y: d[1] }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn operators() {
let a = Vec2::new(1.0, 2.0);
let b = Vec2::new(3.0, 4.0);
assert_eq!(-a, Vec2 { x: -1.0, y: -2.0 });
assert_eq!(a.sqr_magnitude(), 5.0);
assert_eq!(a.magnitude(), 5.0f32.sqrt());
assert_eq!(a.dot(b), 11.0);
assert_eq!(a + b, Vec2 { x: 4.0, y: 6.0 });
assert_eq!(a - b, Vec2 { x: -2.0, y: -2.0 });
assert_eq!(a * b, Vec2 { x: 3.0, y: 8.0 });
assert_eq!(
a / b,
Vec2 {
x: 1.0 / 3.0,
y: 0.5
}
);
assert_eq!(a * 2.0, Vec2 { x: 2.0, y: 4.0 });
assert_eq!(2.0 * a, Vec2 { x: 2.0, y: 4.0 });
assert_eq!(a / 2.0, Vec2 { x: 0.5, y: 1.0 });
assert_eq!(2.0 / a, Vec2 { x: 2.0, y: 1.0 });
let mut c = a;
assert_eq!(c.normalized(), a / a.magnitude());
c.normalize();
assert_eq!(c, a / a.magnitude());
c = a;
c += b;
assert_eq!(c, a + b);
c = a;
c -= b;
assert_eq!(c, a - b);
c = a;
c *= b;
assert_eq!(c, a * b);
c = a;
c /= b;
assert_eq!(c, a / b);
c = a;
c *= 2.0;
assert_eq!(c, a * 2.0);
c = a;
c /= 2.0;
assert_eq!(c, a / 2.0);
}
}