Skip to content

Commit

Permalink
Add/Sub/Mul/Div for all builtin CoordinateTuple types
Browse files Browse the repository at this point in the history
  • Loading branch information
busstoptaktik committed Apr 11, 2024
1 parent a111d53 commit 5095449
Show file tree
Hide file tree
Showing 7 changed files with 506 additions and 510 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- CoordinateSet: xyz(), set_xyz(), xyzt(), set_xyzt() methods
- Vector space operators (Add, Sub, Mul, Div) for all built
in coordinate tuple types (Coor4D, Coor3D, Coor2D, Coor32)

### Changed

Expand Down
59 changes: 1 addition & 58 deletions src/coordinate/coor3d.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::*;
use std::ops::{Add, Div, Mul, Sub};

/// Generic 3D coordinate tuple, with no fixed interpretation of the elements
#[derive(Debug, Default, PartialEq, Copy, Clone)]
Expand All @@ -23,63 +22,6 @@ impl CoordinateTuple for Coor3D {
}
}

// ----- O P E R A T O R T R A I T S -------------------------------------------------

impl Add for Coor3D {
type Output = Self;
fn add(self, other: Self) -> Self {
Coor3D([
self.0[0] + other.0[0],
self.0[1] + other.0[1],
self.0[2] + other.0[2],
])
}
}

impl Add<&Coor3D> for Coor3D {
type Output = Self;
fn add(self, other: &Self) -> Self {
Coor3D([
self.0[0] + other.0[0],
self.0[1] + other.0[1],
self.0[2] + other.0[2],
])
}
}

impl Sub for Coor3D {
type Output = Self;
fn sub(self, other: Self) -> Self {
Coor3D([
self.0[0] - other.0[0],
self.0[1] - other.0[1],
self.0[2] - other.0[2],
])
}
}

impl Mul for Coor3D {
type Output = Self;
fn mul(self, other: Self) -> Self {
Coor3D([
self.0[0] * other.0[0],
self.0[1] * other.0[1],
self.0[2] * other.0[2],
])
}
}

impl Div for Coor3D {
type Output = Self;
fn div(self, other: Self) -> Self {
Coor3D([
self.0[0] / other.0[0],
self.0[1] / other.0[1],
self.0[2] / other.0[2],
])
}
}

// ----- C O N S T R U C T O R S ---------------------------------------------

/// Constructors
Expand Down Expand Up @@ -177,6 +119,7 @@ impl Coor3D {
#[cfg(test)]
mod tests {
use super::*;
use std::ops::{Add, Div, Mul};

#[test]
fn distances() {
Expand Down
64 changes: 1 addition & 63 deletions src/coordinate/coor4d.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::coordinate::*;
use std::ops::{Add, Div, Mul, Sub};

/// Generic 4D coordinate tuple, with no fixed interpretation of the elements
#[derive(Debug, Default, PartialEq, Copy, Clone)]
Expand All @@ -23,68 +22,6 @@ impl CoordinateTuple for Coor4D {
}
}

// ----- O P E R A T O R T R A I T S -------------------------------------------------

impl Add for Coor4D {
type Output = Self;
fn add(self, other: Self) -> Self {
Coor4D([
self.0[0] + other.0[0],
self.0[1] + other.0[1],
self.0[2] + other.0[2],
self.0[3] + other.0[3],
])
}
}

impl Add<&Coor4D> for Coor4D {
type Output = Self;
fn add(self, other: &Self) -> Self {
Coor4D([
self.0[0] + other.0[0],
self.0[1] + other.0[1],
self.0[2] + other.0[2],
self.0[3] + other.0[3],
])
}
}

impl Sub for Coor4D {
type Output = Self;
fn sub(self, other: Self) -> Self {
Coor4D([
self.0[0] - other.0[0],
self.0[1] - other.0[1],
self.0[2] - other.0[2],
self.0[3] - other.0[3],
])
}
}

impl Mul for Coor4D {
type Output = Self;
fn mul(self, other: Self) -> Self {
Coor4D([
self.0[0] * other.0[0],
self.0[1] * other.0[1],
self.0[2] * other.0[2],
self.0[3] * other.0[3],
])
}
}

impl Div for Coor4D {
type Output = Self;
fn div(self, other: Self) -> Self {
Coor4D([
self.0[0] / other.0[0],
self.0[1] / other.0[1],
self.0[2] / other.0[2],
self.0[3] / other.0[3],
])
}
}

// ----- C O N S T R U C T O R S ---------------------------------------------

/// Constructors
Expand Down Expand Up @@ -161,6 +98,7 @@ impl Coor4D {
#[cfg(test)]
mod tests {
use super::*;
use std::ops::{Add, Div, Mul};

#[test]
fn distances() {
Expand Down
Loading

0 comments on commit 5095449

Please sign in to comment.