use std::{
fmt::Debug,
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign},
};
use crate::linear_algebra::matrix::Matrix;
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Vector2 {
pub x: f32,
pub y: f32,
}
impl Vector2 {
pub fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
pub fn to_matrix(&self) -> Matrix<2, 1> {
Matrix::new([[self.x], [self.y]])
}
pub fn dot(&self, rhs: Self) -> f32 {
self.x * rhs.x + self.y * rhs.y
}
}
impl Add for Vector2 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
x: self.x + rhs.x,
y: self.y + rhs.y,
}
}
}
impl Add<f32> for Vector2 {
type Output = Self;
fn add(self, rhs: f32) -> Self::Output {
Self {
x: self.x + rhs,
y: self.y + rhs,
}
}
}
impl AddAssign for Vector2 {
fn add_assign(&mut self, rhs: Self) {
self.x += rhs.x;
self.y += rhs.y;
}
}
impl AddAssign<f32> for Vector2 {
fn add_assign(&mut self, rhs: f32) {
self.x += rhs;
self.y += rhs;
}
}
impl Sub for Vector2 {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self {
x: self.x - rhs.x,
y: self.y - rhs.y,
}
}
}
impl Sub<f32> for Vector2 {
type Output = Self;
fn sub(self, rhs: f32) -> Self::Output {
Self {
x: self.x - rhs,
y: self.y - rhs,
}
}
}
impl SubAssign for Vector2 {
fn sub_assign(&mut self, rhs: Self) {
self.x -= rhs.x;
self.y -= rhs.y;
}
}
impl SubAssign<f32> for Vector2 {
fn sub_assign(&mut self, rhs: f32) {
self.x -= rhs;
self.y -= rhs;
}
}
impl Mul<f32> for Vector2 {
type Output = Self;
fn mul(self, rhs: f32) -> Self::Output {
Self {
x: self.x * rhs,
y: self.y * rhs,
}
}
}
impl MulAssign<f32> for Vector2 {
fn mul_assign(&mut self, rhs: f32) {
self.x *= rhs;
self.y *= rhs;
}
}
impl Div<f32> for Vector2 {
type Output = Self;
fn div(self, rhs: f32) -> Self::Output {
Self {
x: self.x / rhs,
y: self.y / rhs,
}
}
}
impl DivAssign<f32> for Vector2 {
fn div_assign(&mut self, rhs: f32) {
self.x /= rhs;
self.y /= rhs;
}
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Vector3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
impl Vector3 {
pub fn new(x: f32, y: f32, z: f32) -> Self {
Self { x, y, z }
}
pub fn to_matrix(&self) -> Matrix<3, 1> {
Matrix::new([[self.x], [self.y], [self.z]])
}
pub fn dot(&self, rhs: Self) -> f32 {
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
}
pub fn cross(&self, rhs: Self) -> Self {
Self {
x: self.y * rhs.z - self.z * rhs.y,
y: self.z * rhs.x - self.x * rhs.z,
z: self.x * rhs.y - self.y * rhs.x,
}
}
}
impl Add for Vector3 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
x: self.x + rhs.x,
y: self.y + rhs.y,
z: self.z + rhs.z,
}
}
}
impl Add<f32> for Vector3 {
type Output = Self;
fn add(self, rhs: f32) -> Self::Output {
Self {
x: self.x + rhs,
y: self.y + rhs,
z: self.z + rhs,
}
}
}
impl AddAssign for Vector3 {
fn add_assign(&mut self, rhs: Self) {
self.x += rhs.x;
self.y += rhs.y;
self.z += rhs.z;
}
}
impl AddAssign<f32> for Vector3 {
fn add_assign(&mut self, rhs: f32) {
self.x += rhs;
self.y += rhs;
self.z += rhs;
}
}
impl Sub for Vector3 {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self {
x: self.x - rhs.x,
y: self.y - rhs.y,
z: self.z - rhs.z,
}
}
}
impl Sub<f32> for Vector3 {
type Output = Self;
fn sub(self, rhs: f32) -> Self::Output {
Self {
x: self.x - rhs,
y: self.y - rhs,
z: self.z - rhs,
}
}
}
impl SubAssign for Vector3 {
fn sub_assign(&mut self, rhs: Self) {
self.x -= rhs.x;
self.y -= rhs.y;
self.z -= rhs.z;
}
}
impl SubAssign<f32> for Vector3 {
fn sub_assign(&mut self, rhs: f32) {
self.x -= rhs;
self.y -= rhs;
self.z -= rhs;
}
}
impl Mul<f32> for Vector3 {
type Output = Self;
fn mul(self, rhs: f32) -> Self::Output {
Self {
x: self.x * rhs,
y: self.y * rhs,
z: self.z * rhs,
}
}
}
impl MulAssign<f32> for Vector3 {
fn mul_assign(&mut self, rhs: f32) {
self.x *= rhs;
self.y *= rhs;
self.z *= rhs;
}
}
impl Div<f32> for Vector3 {
type Output = Self;
fn div(self, rhs: f32) -> Self::Output {
Self {
x: self.x / rhs,
y: self.y / rhs,
z: self.z / rhs,
}
}
}
impl DivAssign<f32> for Vector3 {
fn div_assign(&mut self, rhs: f32) {
self.x /= rhs;
self.y /= rhs;
self.z /= rhs;
}
}