diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fc7fb74..f2bdbf6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Added `dot` and `cross` product methods for `Vector2D`. + ### Fixed - Fixed an issue with agb tracker where XM files with linear frequencies were playing the wrong notes diff --git a/agb-fixnum/src/lib.rs b/agb-fixnum/src/lib.rs index 6b75318f..d61852ae 100644 --- a/agb-fixnum/src/lib.rs +++ b/agb-fixnum/src/lib.rs @@ -1113,6 +1113,48 @@ impl Vector2D { } } + #[doc(alias = "scalar_product")] + /// Calculates the dot product / scalar product of two vectors + /// ``` + /// use agb_fixnum::Vector2D; + /// + /// let v1 = Vector2D::new(3, 5); + /// let v2 = Vector2D::new(7, 11); + /// + /// let dot = v1.dot(v2); + /// assert_eq!(dot, 76); + /// ``` + /// The dot product for vectors *A* and *B* is defined as + /// > *A**x* × *B**x* + *A**y* × *B**y*. + pub fn dot(self, b: Self) -> T { + self.x * b.x + self.y * b.y + } + + #[doc(alias = "vector_product")] + /// Calculates the *z* component of the cross product / vector product of two + /// vectors + /// ``` + /// use agb_fixnum::Vector2D; + /// + /// let v1 = Vector2D::new(3, 5); + /// let v2 = Vector2D::new(7, 11); + /// + /// let dot = v1.cross(v2); + /// assert_eq!(dot, -2); + /// ``` + /// The *z* component cross product for vectors *A* and *B* is defined as + /// > *A**x* × *B**y* - *A**y* × *B**x*. + /// + /// + /// Normally the cross product / vector product is itself a vector. This is + /// in the 3D case where the cross product of two vectors is perpendicular + /// to both vectors. The only vector perpendicular to two 2D vectors is + /// purely in the *z* direction, hence why this method only returns that + /// component. The *x* and *y* components are always zero. + pub fn cross(self, b: Self) -> T { + self.x * b.y - self.y * b.x + } + #[must_use] /// Swaps the x and y coordinate /// ```