1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use crate::{lerp::LerpPrim, map::Map};
use num_traits::FromPrimitive;
use std::ops::{Add, Div, Mul, Range, RangeInclusive, Sub};
pub type LinSpace<T> = Map<Range<usize>, LerpPrim<T>>;
pub fn lin_space<R, T>(range: R, steps: usize) -> LinSpace<T>
where
R: IntoLinSpace<T>,
{
range.into_lin_space(steps)
}
pub trait IntoLinSpace<T> {
fn into_lin_space(self, steps: usize) -> LinSpace<T>;
}
impl<T> IntoLinSpace<T> for RangeInclusive<T>
where
T: Linear,
{
fn into_lin_space(self, steps: usize) -> LinSpace<T> {
LinSpace::new(0..steps, LerpPrim::new_usize(0..=steps - 1, self))
}
}
impl<T> IntoLinSpace<T> for Range<T>
where
T: Linear,
{
fn into_lin_space(self, steps: usize) -> LinSpace<T> {
let Range { start, end } = self;
LinSpace::new(0..steps, LerpPrim::new_usize(0..=steps, start..=end))
}
}
pub trait Linear:
FromPrimitive
+ Mul<Output = Self>
+ Sub<Output = Self>
+ Add<Output = Self>
+ Div<Output = Self>
+ Copy
{
}
impl<T> Linear for T where
T: FromPrimitive + Mul<Output = T> + Sub<Output = T> + Add<Output = T> + Div<Output = T> + Copy
{
}