mirror of
https://github.com/hannobraun/Fornjot
synced 2025-05-05 18:38:28 +00:00
Add Vector::dot
This commit is contained in:
parent
878f6051bc
commit
10a2036740
@ -11,17 +11,21 @@ pub struct Vector<const D: usize> {
|
|||||||
|
|
||||||
impl<const D: usize> Vector<D> {
|
impl<const D: usize> Vector<D> {
|
||||||
pub fn magnitude(&self) -> Scalar {
|
pub fn magnitude(&self) -> Scalar {
|
||||||
self.components
|
self.dot(self).sqrt()
|
||||||
.into_iter()
|
|
||||||
.map(|coord| coord * coord)
|
|
||||||
.reduce(|a, b| a + b)
|
|
||||||
.unwrap_or(Scalar::zero())
|
|
||||||
.sqrt()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn normalize(self) -> Self {
|
pub fn normalize(self) -> Self {
|
||||||
self / self.magnitude()
|
self / self.magnitude()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn dot(&self, other: &Self) -> Scalar {
|
||||||
|
self.components
|
||||||
|
.into_iter()
|
||||||
|
.zip(other.components)
|
||||||
|
.map(|(a, b)| a * b)
|
||||||
|
.reduce(|a, b| a + b)
|
||||||
|
.unwrap_or(Scalar::zero())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Vector<3> {
|
impl Vector<3> {
|
||||||
|
Loading…
Reference in New Issue
Block a user