Add Vec4::xyz()

This commit is contained in:
Matthew Gordon 2025-03-29 15:13:40 -03:00
parent 80b2d87d22
commit 908da3d995
1 changed files with 15 additions and 1 deletions

View File

@ -1,4 +1,4 @@
use super::Float; use super::{Float, Vec3};
use itertools::izip; use itertools::izip;
@ -32,6 +32,12 @@ impl<T: Float> Vec4<T> {
self.coords[3] self.coords[3]
} }
pub fn xyz(&self) -> Vec3<T> {
let mut coords = [T::zero(); 3];
coords.copy_from_slice(&self.coords[0..3]);
Vec3 { coords }
}
pub fn dot(&self, rhs: &Vec4<T>) -> T { pub fn dot(&self, rhs: &Vec4<T>) -> T {
self.coords self.coords
.iter() .iter()
@ -138,6 +144,14 @@ mod tests {
assert!(target.w() == 4.0); assert!(target.w() == 4.0);
} }
#[test]
fn xyz_returns_expected_value() {
let target = Vec4::new(1.0, 2.0, 3.0, 4.0).xyz();
assert!(target.x() == 1.0);
assert!(target.y() == 2.0);
assert!(target.z() == 3.0);
}
#[test] #[test]
fn dot_product_returns_correct_result() { fn dot_product_returns_correct_result() {
let a = Vec4::new(1.0, 2.0, 3.0, 4.0); let a = Vec4::new(1.0, 2.0, 3.0, 4.0);