From 071eb8997fd92bae23a6beea649715b864507fb8 Mon Sep 17 00:00:00 2001 From: Markus Mayer Date: Wed, 17 Jul 2024 17:06:29 +0200 Subject: [PATCH] Add scalar division for vectors --- src/vector/vector2d.rs | 25 +++++++++++++++++++++++++ src/vector/vector3d.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/vector/vector2d.rs b/src/vector/vector2d.rs index 0611379..0848305 100644 --- a/src/vector/vector2d.rs +++ b/src/vector/vector2d.rs @@ -1,6 +1,7 @@ //! 2-dimensional vector use super::{Component, Vector, Vector3d}; +use core::ops::{Div, DivAssign}; use core::{ iter::FromIterator, ops::{Add, AddAssign, Index, Mul, MulAssign, Sub, SubAssign}, @@ -251,6 +252,30 @@ where } } +impl Div for Vector2d +where + C: Component, +{ + type Output = Self; + + fn div(self, rhs: C) -> Self { + Self { + x: self.x / rhs, + y: self.y / rhs, + } + } +} + +impl DivAssign for Vector2d +where + C: Component, +{ + fn div_assign(&mut self, rhs: C) { + self.x = self.x / rhs; + self.y = self.y / rhs; + } +} + impl From for F32x2 { fn from(vector: I8x2) -> F32x2 { Self { diff --git a/src/vector/vector3d.rs b/src/vector/vector3d.rs index 09151f0..fe5afd7 100644 --- a/src/vector/vector3d.rs +++ b/src/vector/vector3d.rs @@ -2,6 +2,7 @@ use super::{Component, Vector, Vector2d}; use crate::F32; +use core::ops::{Div, DivAssign}; use core::{ iter::FromIterator, ops::{Add, AddAssign, Index, Mul, MulAssign, Sub, SubAssign}, @@ -268,6 +269,32 @@ where } } +impl Div for Vector3d +where + C: Component, +{ + type Output = Self; + + fn div(self, rhs: C) -> Self { + Self { + x: self.x / rhs, + y: self.y / rhs, + z: self.z / rhs, + } + } +} + +impl DivAssign for Vector3d +where + C: Component, +{ + fn div_assign(&mut self, rhs: C) { + self.x = self.x / rhs; + self.y = self.y / rhs; + self.z = self.z / rhs; + } +} + impl From for F32x3 { fn from(vector: I8x3) -> F32x3 { Self {