vector_space/
lib.rs

1#![no_std]
2#![deny(missing_docs)]
3/*!
4This crate contains new traits useful for working with vector spaces.
5They have default implementations, so you can just implement them to get a lot of useful methods for free for your vector type.
6But you can also just implement the methods yourself.
7
8You can also define some library in terms of these traits instead of using a specific vector math implementation, so the user can choose, which one to use, or simply add multiple vector math libraries which implement these traits yourself by using this library.
9**/
10
11mod dot;
12mod inner;
13mod outer;
14mod vector;
15
16pub use dot::DotProduct;
17pub use inner::InnerSpace;
18pub use outer::OuterProduct;
19pub use vector::{VectorSpace, VectorSpaceAssign};
20
21use core::ops::{Add, Sub};
22
23/// The linear interpolation of two points.
24pub fn interpolate<T>(a: T, b: T, ratio: <<T as Sub>::Output as VectorSpace>::Scalar) -> T
25where
26    T: Clone + Sub + Add<<T as Sub>::Output, Output = T>,
27    <T as Sub>::Output: VectorSpace,
28{
29    a.clone() + (b - a) * ratio
30}
31
32/// The distance between two points.
33pub fn distance<T: Sub>(a: T, b: T) -> <T::Output as VectorSpace>::Scalar
34where
35    T::Output: InnerSpace,
36{
37    (b - a).magnitude()
38}
39
40/// The squared distance between two points.
41pub fn distance2<T: Sub>(a: T, b: T) -> <T::Output as VectorSpace>::Scalar
42where
43    T::Output: InnerSpace,
44{
45    (b - a).magnitude2()
46}