1#![allow(dead_code)]
2
3struct Vector2Ref<'a> {
4 x: &'a mut f64,
5 y: &'a mut f64,
6}
7
8impl<'a> Vector2Ref<'a> {
9 fn new(x: &'a mut f64, y: &'a mut f64) -> Self {
10 Vector2Ref { x, y }
11 }
12
13 fn dot(&self, other: &Vector2Ref) -> f64 {
14 *self.x * *other.x + *self.y * *other.y
15 }
16
17 fn cross(&self, other: &Vector2Ref) -> f64 {
18 *self.x * *other.y - *other.x * *self.y
19 }
20
21 fn add_assign(&mut self, other: &Vector2Ref) {
22 *self.x += *other.x;
23 *self.y += *other.y;
24 }
25
26 fn sub_assign(&mut self, other: &Vector2Ref) {
27 *self.x -= *other.x;
28 *self.y -= *other.y;
29 }
30
31 fn mul_assign(&mut self, alpha: f64) {
32 *self.x *= alpha;
33 *self.y *= alpha;
34 }
35
36 fn div_assign(&mut self, alpha: f64) {
37 *self.x /= alpha;
38 *self.y /= alpha;
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45
46 #[test]
47 fn test_vector2() {
48 let mut x = 1.0;
49 let mut y = 2.0;
50
51 let mut v = Vector2Ref::new(&mut x, &mut y);
52 v.mul_assign(2.0);
53 assert_eq!(*v.x, 2.0);
54 assert_eq!(*v.y, 4.0);
55
56 let mut v2 = Vector2Ref::new(&mut x, &mut y);
57 v2.mul_assign(2.0);
58 assert_eq!(*v2.y, 8.0);
59 }
60}