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
//! Curves.

#[cfg(test)]
extern crate assert;

extern crate num_traits as num;

use num::Float;
use std::marker::PhantomData;

/// A curve.
pub trait Curve<T: Float> {
    /// Evalute the curve at a point in `[0, 1]`.
    fn evaluate(&self, T) -> T;
}

/// A trace of a curve.
#[derive(Clone, Copy, Debug)]
pub struct Trace<'l, T: Float, C: 'l + Curve<T>> {
    curve: &'l C,
    steps: usize,
    position: usize,
    phantom: PhantomData<T>,
}

impl<'l, T: Float, C: Curve<T>> Trace<'l, T, C> {
    #[inline]
    fn new(curve: &'l C, steps: usize) -> Self {
        Trace { curve: curve, steps: steps, position: 0, phantom: PhantomData }
    }
}

macro_rules! implement {
    ($($float:ty),*) => ($(
        impl<'l, T: Curve<$float>> Iterator for Trace<'l, $float, T> {
            type Item = $float;

            fn next(&mut self) -> Option<Self::Item> {
                let position = self.position;
                if position < self.steps {
                    self.position += 1;
                    Some(self.curve.evaluate(position as $float / (self.steps - 1) as $float))
                } else {
                    None
                }
            }
        }
    )*);
}

implement!(f32, f64);

pub mod bezier;