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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use vec2;
use number_traits::{Cast, Num};

#[inline]
pub fn quadratic<'a, 'b, T, F>(
    out: &'a mut [T; 2],
    p0: &'b [T; 2],
    p1: &'b [T; 2],
    p2: &'b [T; 2],
    p3: &'b [T; 2],
    t: F,
) -> &'a mut [T; 2]
where
    T: Copy + Num + Cast<F>,
    F: Copy + Num + Cast<T>,
{
    if t <= F::zero() {
        vec2::copy(out, p0)
    } else if t >= F::one() {
        vec2::copy(out, p3)
    } else {
        let three = F::from_usize(3);
        let one_min_t = F::one() - t;
        let one_min_t_sq = one_min_t * one_min_t;
        let one_min_t_cb = one_min_t_sq * one_min_t;
        let t_sq = t * t;
        let t_cb = t_sq * t;

        let p0x = p0[0].cast();
        let p0y = p0[1].cast();
        let p1x = p1[0].cast();
        let p1y = p1[1].cast();
        let p2x = p2[0].cast();
        let p2y = p2[1].cast();
        let p3x = p3[0].cast();
        let p3y = p3[1].cast();

        vec2::set(
            out,
            (one_min_t_cb * p0x + three * one_min_t_sq * t * p1x + three * one_min_t * t_sq * p2x
                + t_cb * p3x)
                .cast(),
            (one_min_t_cb * p0y + three * one_min_t_sq * t * p1y + three * one_min_t * t_sq * p2y
                + t_cb * p3y)
                .cast(),
        )
    }
}
#[test]
fn test_quadratic() {
    assert_eq!(
        quadratic(
            &mut [0, 0],
            &[0, 0],
            &[0, 200],
            &[200, 200],
            &[200, 0],
            0.25
        ),
        &[31, 112]
    );
    assert_eq!(
        quadratic(&mut [0, 0], &[0, 0], &[0, 200], &[200, 200], &[200, 0], 0.5),
        &[100, 150]
    );
    assert_eq!(
        quadratic(
            &mut [0, 0],
            &[0, 0],
            &[0, 200],
            &[200, 200],
            &[200, 0],
            0.75
        ),
        &[168, 112]
    );
}