Add dot and cross product (#695)

Docs:

![image](https://github.com/agbrs/agb/assets/8143879/88f10d03-27c6-43b3-baf3-b79469ed7a12)


- [x] Changelog updated
This commit is contained in:
Corwin 2024-05-15 22:40:09 +01:00 committed by GitHub
commit 5a80acd501
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 46 additions and 0 deletions

View file

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Added
- Added `dot` and `cross` product methods for `Vector2D`.
### Fixed ### Fixed
- Fixed an issue with agb tracker where XM files with linear frequencies were playing the wrong notes - Fixed an issue with agb tracker where XM files with linear frequencies were playing the wrong notes

View file

@ -1113,6 +1113,48 @@ impl<T: Number> Vector2D<T> {
} }
} }
#[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*<sub>*x*</sub> × *B*<sub>*x*</sub> + *A*<sub>*y*</sub> × *B*<sub>*y*</sub>.
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*<sub>*x*</sub> × *B*<sub>*y*</sub> - *A*<sub>*y*</sub> × *B*<sub>*x*</sub>.
///
///
/// 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] #[must_use]
/// Swaps the x and y coordinate /// Swaps the x and y coordinate
/// ``` /// ```