Skip to content

Commit 5095449

Browse files
committed
Add/Sub/Mul/Div for all builtin CoordinateTuple types
1 parent a111d53 commit 5095449

File tree

7 files changed

+506
-510
lines changed

7 files changed

+506
-510
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

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

1416
### Changed
1517

src/coordinate/coor3d.rs

+1-58
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use super::*;
2-
use std::ops::{Add, Div, Mul, Sub};
32

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

26-
// ----- O P E R A T O R T R A I T S -------------------------------------------------
27-
28-
impl Add for Coor3D {
29-
type Output = Self;
30-
fn add(self, other: Self) -> Self {
31-
Coor3D([
32-
self.0[0] + other.0[0],
33-
self.0[1] + other.0[1],
34-
self.0[2] + other.0[2],
35-
])
36-
}
37-
}
38-
39-
impl Add<&Coor3D> for Coor3D {
40-
type Output = Self;
41-
fn add(self, other: &Self) -> Self {
42-
Coor3D([
43-
self.0[0] + other.0[0],
44-
self.0[1] + other.0[1],
45-
self.0[2] + other.0[2],
46-
])
47-
}
48-
}
49-
50-
impl Sub for Coor3D {
51-
type Output = Self;
52-
fn sub(self, other: Self) -> Self {
53-
Coor3D([
54-
self.0[0] - other.0[0],
55-
self.0[1] - other.0[1],
56-
self.0[2] - other.0[2],
57-
])
58-
}
59-
}
60-
61-
impl Mul for Coor3D {
62-
type Output = Self;
63-
fn mul(self, other: Self) -> Self {
64-
Coor3D([
65-
self.0[0] * other.0[0],
66-
self.0[1] * other.0[1],
67-
self.0[2] * other.0[2],
68-
])
69-
}
70-
}
71-
72-
impl Div for Coor3D {
73-
type Output = Self;
74-
fn div(self, other: Self) -> Self {
75-
Coor3D([
76-
self.0[0] / other.0[0],
77-
self.0[1] / other.0[1],
78-
self.0[2] / other.0[2],
79-
])
80-
}
81-
}
82-
8325
// ----- C O N S T R U C T O R S ---------------------------------------------
8426

8527
/// Constructors
@@ -177,6 +119,7 @@ impl Coor3D {
177119
#[cfg(test)]
178120
mod tests {
179121
use super::*;
122+
use std::ops::{Add, Div, Mul};
180123

181124
#[test]
182125
fn distances() {

src/coordinate/coor4d.rs

+1-63
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::coordinate::*;
2-
use std::ops::{Add, Div, Mul, Sub};
32

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

26-
// ----- O P E R A T O R T R A I T S -------------------------------------------------
27-
28-
impl Add for Coor4D {
29-
type Output = Self;
30-
fn add(self, other: Self) -> Self {
31-
Coor4D([
32-
self.0[0] + other.0[0],
33-
self.0[1] + other.0[1],
34-
self.0[2] + other.0[2],
35-
self.0[3] + other.0[3],
36-
])
37-
}
38-
}
39-
40-
impl Add<&Coor4D> for Coor4D {
41-
type Output = Self;
42-
fn add(self, other: &Self) -> Self {
43-
Coor4D([
44-
self.0[0] + other.0[0],
45-
self.0[1] + other.0[1],
46-
self.0[2] + other.0[2],
47-
self.0[3] + other.0[3],
48-
])
49-
}
50-
}
51-
52-
impl Sub for Coor4D {
53-
type Output = Self;
54-
fn sub(self, other: Self) -> Self {
55-
Coor4D([
56-
self.0[0] - other.0[0],
57-
self.0[1] - other.0[1],
58-
self.0[2] - other.0[2],
59-
self.0[3] - other.0[3],
60-
])
61-
}
62-
}
63-
64-
impl Mul for Coor4D {
65-
type Output = Self;
66-
fn mul(self, other: Self) -> Self {
67-
Coor4D([
68-
self.0[0] * other.0[0],
69-
self.0[1] * other.0[1],
70-
self.0[2] * other.0[2],
71-
self.0[3] * other.0[3],
72-
])
73-
}
74-
}
75-
76-
impl Div for Coor4D {
77-
type Output = Self;
78-
fn div(self, other: Self) -> Self {
79-
Coor4D([
80-
self.0[0] / other.0[0],
81-
self.0[1] / other.0[1],
82-
self.0[2] / other.0[2],
83-
self.0[3] / other.0[3],
84-
])
85-
}
86-
}
87-
8825
// ----- C O N S T R U C T O R S ---------------------------------------------
8926

9027
/// Constructors
@@ -161,6 +98,7 @@ impl Coor4D {
16198
#[cfg(test)]
16299
mod tests {
163100
use super::*;
101+
use std::ops::{Add, Div, Mul};
164102

165103
#[test]
166104
fn distances() {

0 commit comments

Comments
 (0)