vector_space/
lib.rs

1#![deny(missing_docs)]
2/*!
3This crate contains new traits useful for working with vector spaces.
4They have default implementations, so you can just implement them to get a lot of useful methods for free for your vector type.
5But you can also just implement the methods yourself.
6
7You 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.
8**/
9
10mod dot;
11mod inner;
12mod outer;
13mod vector;
14
15pub use dot::DotProduct;
16pub use inner::InnerSpace;
17pub use outer::OuterProduct;
18pub use vector::VectorSpace;
19
20use std::ops::{Add, Sub};
21
22/// The linear interpolation of two points.
23pub fn interpolate<T: Copy>(a: T, b: T, ratio: <<T as Sub>::Output as VectorSpace>::Scalar) -> T
24where
25    T: Sub + Add<<T as Sub>::Output, Output = T>,
26    <T as Sub>::Output: VectorSpace,
27{
28    a + (b - a) * ratio
29}
30
31/// The distance between two points.
32pub fn distance<T: Sub>(a: T, b: T) -> <T::Output as VectorSpace>::Scalar
33where
34    T::Output: InnerSpace,
35{
36    (b - a).magnitude()
37}
38
39/// The squared distance between two points.
40pub fn distance2<T: Sub>(a: T, b: T) -> <T::Output as VectorSpace>::Scalar
41where
42    T::Output: InnerSpace,
43{
44    (b - a).magnitude2()
45}