Skip to content

Commit

Permalink
Add scalar division for vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsided committed Jul 17, 2024
1 parent 2e01dcc commit 071eb89
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/vector/vector2d.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -251,6 +252,30 @@ where
}
}

impl<C> Div<C> for Vector2d<C>
where
C: Component,
{
type Output = Self;

fn div(self, rhs: C) -> Self {
Self {
x: self.x / rhs,
y: self.y / rhs,
}
}
}

impl<C> DivAssign<C> for Vector2d<C>
where
C: Component,
{
fn div_assign(&mut self, rhs: C) {
self.x = self.x / rhs;
self.y = self.y / rhs;
}
}

impl From<I8x2> for F32x2 {
fn from(vector: I8x2) -> F32x2 {
Self {
Expand Down
27 changes: 27 additions & 0 deletions src/vector/vector3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -268,6 +269,32 @@ where
}
}

impl<C> Div<C> for Vector3d<C>
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<C> DivAssign<C> for Vector3d<C>
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<I8x3> for F32x3 {
fn from(vector: I8x3) -> F32x3 {
Self {
Expand Down

0 comments on commit 071eb89

Please sign in to comment.