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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! Discrete line iterator in hex space.

use num_traits::AsPrimitive;
use std::iter::{FusedIterator, Iterator};
use std::marker::PhantomData;

use super::coords::{Point, Vector};

type HexPointF = Point<f32>;
type HexVectorF = Vector<f32>;

/// A discrete line iterator in hex space.
pub struct Line<T> {
    current: HexPointF,
    step_size: HexVectorF,
    steps: usize,
    _phantom: PhantomData<T>,
}

impl<T> Line<T>
where
    T: AsPrimitive<f32>,
    f32: AsPrimitive<T>,
{
    /// Create a new line iterator from endpoints.
    ///
    /// Generally, it's easier to use `Point::line_to` variants instead.
    pub fn new(from: Point<T>, to: Point<T>, exclude_start: bool, exclude_end: bool) -> Self {
        let mut current = from.cast_fix();
        let delta = to.cast_fix() - current;

        // length is known to be non-negative
        #[allow(clippy::cast_sign_loss)]
        let mut steps = delta.length() as usize + 1;

        let step_size = delta * (1.0 / ((steps - 1) as f32));

        if exclude_start && steps > 0 {
            current += step_size;
            steps -= 1;
        }

        if exclude_end && steps > 0 {
            steps -= 1;
        }

        Line {
            current,
            step_size,
            steps,
            _phantom: PhantomData,
        }
    }
}

impl<T> Iterator for Line<T>
where
    T: 'static + Copy,
    f32: AsPrimitive<T>,
{
    type Item = Point<T>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.steps > 0 {
            let result = self.current;
            self.current += self.step_size;
            self.steps -= 1;
            Some(result.cast_round())
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.steps, Some(self.steps))
    }
}

impl<T> ExactSizeIterator for Line<T>
where
    T: 'static + Copy,
    f32: AsPrimitive<T>,
{
    fn len(&self) -> usize {
        self.steps
    }
}

impl<T> FusedIterator for Line<T>
where
    T: 'static + Copy,
    f32: AsPrimitive<T>,
{
}

#[cfg(test)]
mod tests {
    use super::*;

    type H = super::super::coords::Point<i16>;
    type V = super::super::coords::Vector<i16>;

    #[test]
    fn can_iter() {
        fn test_endpoints(from: H, to: H, expected_steps: &[V]) {
            let expected = expected_steps
                .iter()
                .scan(from, |s, &d| {
                    *s += d;
                    Some(*s)
                })
                .collect::<Vec<_>>();

            itertools::assert_equal(Line::new(from, to, false, false), expected.iter().cloned());
            itertools::assert_equal(
                Line::new(from, to, true, false),
                expected[1..].iter().cloned(),
            );
            itertools::assert_equal(
                Line::new(from, to, false, true),
                expected[..expected.len() - 1].iter().cloned(),
            );
            itertools::assert_equal(
                Line::new(from, to, true, true),
                expected[1..expected.len() - 1].iter().cloned(),
            );

            itertools::assert_equal(
                Line::new(to, from, false, false),
                expected.iter().cloned().rev(),
            );
            itertools::assert_equal(
                Line::new(to, from, true, false),
                expected[..expected.len() - 1].iter().cloned().rev(),
            );
            itertools::assert_equal(
                Line::new(to, from, false, true),
                expected[1..].iter().cloned().rev(),
            );
            itertools::assert_equal(
                Line::new(to, from, true, true),
                expected[1..expected.len() - 1].iter().cloned().rev(),
            );
        }

        test_endpoints(
            H::new(-1, 1, 0, 0),
            H::new(0, -1, 1, 0),
            &[V::zero(), V::zy(), V::xy()],
        );
        test_endpoints(
            H::new(-1, 1, 0, 0),
            H::new(1, -2, 1, 0),
            &[V::zero(), V::xy(), V::zy(), V::xy()],
        );
        test_endpoints(
            H::new(-1, 1, 0, 0),
            H::new(0, 2, -2, 0),
            &[V::zero(), V::yz(), V::xz()],
        );
    }
}