Skip to content

Commit

Permalink
Add Vector::opposite
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Dec 11, 2023
1 parent a1131f4 commit e9179cf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Add Vector::distance to calculate the Euclidean Distance between two points
- Add Vector::manhattan_distance to calculate the Manhattan Distance between points
- Add Vector::sum to sum the values of all components
- Add Vector::opposite to create a new vector with all components negated

## v0.3.0 – November 26, 2023

Expand Down
12 changes: 11 additions & 1 deletion src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ impl<T: Num + Copy + Ord, const N: usize> Vector<T, N> {
}

impl<T: Num + Copy + Signed, const N: usize> Vector<T, N> {
/// Calculates the opposite of a vector.
/// This is the vector with all components negated.
pub fn opposite(&self) -> Self {
let mut components = [T::zero(); N];
for (i, e) in components.iter_mut().enumerate() {
*e = -self.components[i];
}
Self { components }
}

/// Calculates the sign of each component of a vector.
/// This is -1 if the component is negative, 0 if it is zero, and 1 if it is positive.
pub fn signum(&self) -> Self {
Expand Down Expand Up @@ -224,7 +234,7 @@ impl<T: Num + Copy + Sum + Real, const N: usize> Vector<T, N> {
*self / self.magnitude()
}

/// Calculates the [euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance) of two vectors.
/// Calculates the [Euclidean Distance](https://en.wikipedia.org/wiki/Euclidean_distance) of two vectors.
pub fn distance(&self, other: &Self) -> T {
(*self - *other).magnitude()
}
Expand Down

0 comments on commit e9179cf

Please sign in to comment.