diff --git a/agb/src/number.rs b/agb/src/number.rs index 91aece71..99b2a6c9 100644 --- a/agb/src/number.rs +++ b/agb/src/number.rs @@ -7,6 +7,8 @@ use core::{ }, }; +use crate::syscall; + pub trait Number: Sized + Copy @@ -293,6 +295,13 @@ impl Num { } } +impl Num { + pub fn sqrt(self) -> Self { + assert_eq!(N % 2, 0, "N must be even to be able to square root"); + Self(syscall::sqrt(self.0) << (N / 2)) + } +} + #[test_case] fn test_numbers(_gba: &mut super::Gba) { // test addition @@ -528,6 +537,19 @@ impl Vector2D> { } } +impl Vector2D> { + pub fn magnitude_squared(self) -> Num { + self.x * self.x + self.y * self.y + } + + pub fn magnitude(self) -> Num { + self.magnitude_squared().sqrt() + } + pub fn normalise(self) -> Self { + self / self.magnitude() + } +} + impl> From<(P, P)> for Vector2D { fn from(f: (P, P)) -> Self { Vector2D::new(f.0.into(), f.1.into())