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
use std::ops; use serde::{Deserialize, Serialize}; use crate::vector; use crate::vector::*; use crate::{matrix, vector3}; #[derive(PartialEq, Debug, Serialize, Deserialize)] pub struct Vector2 { pub x: f32, pub y: f32, } impl vector::Rotate for Vector2 { fn rotate(&self, _rotation_matrix: matrix::Matrix) -> Self { panic!("TODO") } } impl vector::InnerProduct for Vector2 { fn inner_product(&self, _rhs: &Self) -> f32 { self.x * _rhs.x + self.y * _rhs.y } } impl vector::OuterProduct for Vector2 { fn outer_product(&self, _rhs: &Self) -> vector3::Vector3 { let self_vec3 = vector3::Vector3 { x: self.x, y: self.y, z: 0.0, }; let rhs_vec3 = vector3::Vector3 { x: _rhs.x, y: _rhs.y, z: 0.0, }; self_vec3.outer_product(&rhs_vec3) } } impl vector::Scale for Vector2 { fn scale(&self, multiplier: f32) -> Self { Self { x: self.x * multiplier, y: self.y * multiplier, } } } impl vector::Negate for Vector2 { fn negate(&self) -> Self { self.scale(-1.0) } } impl vector::Length for Vector2 { fn get_squared_length(&self) -> f32 { self.x * self.x + self.y * self.y } fn get_length(&self) -> f32 { self.get_squared_length().sqrt() } fn normalize(&self) -> Self { self.scale(self.get_length().recip()) } } impl ops::Add<Vector2> for Vector2 { type Output = Vector2; fn add(self, _rhs: Vector2) -> Vector2 { Vector2 { x: self.x + _rhs.x, y: self.y + _rhs.y, } } } impl ops::Sub<Vector2> for Vector2 { type Output = Vector2; fn sub(self, _rhs: Vector2) -> Vector2 { Vector2 { x: self.x - _rhs.x, y: self.y - _rhs.y, } } }