move some functions under more general generics (#726)

- [x] Changelog updated
This commit is contained in:
Corwin 2024-06-13 22:56:47 +01:00 committed by GitHub
commit bc061f676f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 22 deletions

View file

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Changed
- `manhattan_distance` and `magnitude_squared` no longer require the fixed point number.
## [0.20.3] - 2024/06/12 ## [0.20.3] - 2024/06/12
### Added ### Added

View file

@ -744,6 +744,17 @@ impl<T: Number + Signed> Vector2D<T> {
y: self.y.abs(), y: self.y.abs(),
} }
} }
#[must_use]
/// Calculates the manhattan distance, x.abs() + y.abs().
/// ```
/// # use agb_fixnum::*;
/// let v1: Vector2D<Num<i32, 8>> = (num!(3.), num!(4.)).into();
/// assert_eq!(v1.manhattan_distance(), 7.into());
/// ```
pub fn manhattan_distance(self) -> T {
self.x.abs() + self.y.abs()
}
} }
impl<I: FixedWidthUnsignedInteger, const N: usize> Vector2D<Num<I, N>> { impl<I: FixedWidthUnsignedInteger, const N: usize> Vector2D<Num<I, N>> {
@ -790,28 +801,6 @@ impl<I: FixedWidthUnsignedInteger, const N: usize> Vector2D<Num<I, N>> {
} }
impl<const N: usize> Vector2D<Num<i32, N>> { impl<const N: usize> Vector2D<Num<i32, N>> {
#[must_use]
/// Calculates the magnitude squared, ie (x*x + y*y)
/// ```
/// # use agb_fixnum::*;
/// let v1: Vector2D<Num<i32, 8>> = (num!(3.), num!(4.)).into();
/// assert_eq!(v1.magnitude_squared(), 25.into());
/// ```
pub fn magnitude_squared(self) -> Num<i32, N> {
self.x * self.x + self.y * self.y
}
#[must_use]
/// Calculates the manhattan distance, x.abs() + y.abs().
/// ```
/// # use agb_fixnum::*;
/// let v1: Vector2D<Num<i32, 8>> = (num!(3.), num!(4.)).into();
/// assert_eq!(v1.manhattan_distance(), 7.into());
/// ```
pub fn manhattan_distance(self) -> Num<i32, N> {
self.x.abs() + self.y.abs()
}
#[must_use] #[must_use]
/// Calculates the magnitude by square root /// Calculates the magnitude by square root
/// ``` /// ```
@ -1168,6 +1157,17 @@ impl<T: Number> Vector2D<T> {
y: self.x, y: self.x,
} }
} }
#[must_use]
/// Calculates the magnitude squared, ie (x*x + y*y)
/// ```
/// # use agb_fixnum::*;
/// let v1: Vector2D<Num<i32, 8>> = (num!(3.), num!(4.)).into();
/// assert_eq!(v1.magnitude_squared(), 25.into());
/// ```
pub fn magnitude_squared(self) -> T {
self.x * self.x + self.y * self.y
}
} }
impl<T: Number + Neg<Output = T>> Neg for Vector2D<T> { impl<T: Number + Neg<Output = T>> Neg for Vector2D<T> {