Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing operators for Vector2<T> #106

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions src/dimen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ impl<T: std::ops::Neg<Output = T> + Copy> Vector2<T>
{
Vector2::new(self.y, -self.x)
}

/// Rotates the vector by 180 degrees. Equivalent to negating the vector.
#[inline]
#[must_use]
pub fn rotate_180_degrees(&self) -> Vector2<T>
{
-self
}
}

impl<T: num_traits::AsPrimitive<f32>> Vector2<T>
Expand Down Expand Up @@ -281,6 +289,30 @@ impl<T: Copy + std::ops::Sub<Output = T>, R: Into<Vector2<T>>> std::ops::Sub<R>
}
}

impl<T: Copy + std::ops::Neg<Output = T>> std::ops::Neg for Vector2<T>
{
type Output = Vector2<T>;

#[inline]
#[must_use]
fn neg(self) -> Self::Output
{
Vector2::new(-self.x, -self.y)
}
}

impl<T: Copy + std::ops::Neg<Output = T>> std::ops::Neg for &Vector2<T>
{
type Output = Vector2<T>;

#[inline]
#[must_use]
fn neg(self) -> Self::Output
{
Vector2::new(-self.x, -self.y)
}
}

impl<T: Copy + std::ops::AddAssign, R: Into<Vector2<T>>> std::ops::AddAssign<R>
for Vector2<T>
{
Expand Down Expand Up @@ -393,6 +425,87 @@ impl<T: Copy + std::ops::Mul<Output = T>> std::ops::Mul<T> for Vector2<T>
}
}

// Unlike other operators, we can not implement the `scalar by vector`
// multiplication operator generically, as that would require implementing
// a foreign trait for a completely foreign type. For instance, someone
// might want to impl Mul<Vector<TheirType>> for TheirType.

// That said, putting a scalar on the left side of the multiplication
// is so common (standard in textbooks) that we implement that for the
// concrete vectors of this module.

impl std::ops::Mul<Vector2<f32>> for f32
{
type Output = Vector2<f32>;

#[inline]
#[must_use]
fn mul(self, rhs: Vector2<f32>) -> Self::Output
{
Vector2::new(self * rhs.x, self * rhs.y)
}
}

impl std::ops::Mul<Vector2<&f32>> for &f32
{
type Output = Vector2<f32>;

#[inline]
#[must_use]
fn mul(self, rhs: Vector2<&f32>) -> Self::Output
{
Vector2::new(self * rhs.x, self * rhs.y)
}
}

impl std::ops::Mul<Vector2<i32>> for i32
{
type Output = Vector2<i32>;

#[inline]
#[must_use]
fn mul(self, rhs: Vector2<i32>) -> Self::Output
{
Vector2::new(self * rhs.x, self * rhs.y)
}
}

impl std::ops::Mul<Vector2<&i32>> for &i32
{
type Output = Vector2<i32>;

#[inline]
#[must_use]
fn mul(self, rhs: Vector2<&i32>) -> Self::Output
{
Vector2::new(self * rhs.x, self * rhs.y)
}
}

impl std::ops::Mul<Vector2<u32>> for u32
{
type Output = Vector2<u32>;

#[inline]
#[must_use]
fn mul(self, rhs: Vector2<u32>) -> Self::Output
{
Vector2::new(self * rhs.x, self * rhs.y)
}
}

impl std::ops::Mul<Vector2<&u32>> for &u32
{
type Output = Vector2<u32>;

#[inline]
#[must_use]
fn mul(self, rhs: Vector2<&u32>) -> Self::Output
{
Vector2::new(self * rhs.x, self * rhs.y)
}
}

impl<T: Copy + std::ops::Div<Output = T>> std::ops::Div<T> for &Vector2<T>
{
type Output = Vector2<T>;
Expand Down
Loading