veloren_common/util/
projection.rs

1use vek::{Vec2, Vec3};
2
3/// Projection trait for projection of linear types and shapes
4pub trait Projection<T> {
5    type Output;
6
7    fn projected(self, onto: &T) -> Self::Output;
8}
9
10// Impls
11
12impl Projection<Vec2<f32>> for Vec2<f32> {
13    type Output = Self;
14
15    fn projected(self, v: &Self) -> Self::Output {
16        let v = *v;
17        self.dot(v) * v / v.magnitude_squared()
18    }
19}
20
21impl Projection<Vec3<f32>> for Vec3<f32> {
22    type Output = Self;
23
24    fn projected(self, v: &Self) -> Self::Output {
25        let v = *v;
26        v * self.dot(v) / v.magnitude_squared()
27    }
28}