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
use crate::{
    geometry::Point,
    primitives::{
        arc::Arc,
        common::{DistanceIterator, PlaneSector},
        OffsetOutline,
    },
};

/// Iterator over all points on the arc line.
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "defmt", derive(::defmt::Format))]
pub struct Points {
    iter: DistanceIterator,

    plane_sector: PlaneSector,

    outer_threshold: u32,
    inner_threshold: u32,
}

impl Points {
    pub(in crate::primitives) fn new(arc: &Arc) -> Self {
        let outer_circle = arc.to_circle();
        let inner_circle = outer_circle.offset(-1);

        let plane_sector = PlaneSector::new(arc.angle_start, arc.angle_sweep);

        Self {
            // PERF: The distance iterator should use the smaller arc bounding box
            iter: outer_circle.distances(),
            plane_sector,
            outer_threshold: outer_circle.threshold(),
            inner_threshold: inner_circle.threshold(),
        }
    }
}

impl Iterator for Points {
    type Item = Point;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter
            .find(|(_, delta, distance)| {
                *distance < self.outer_threshold
                    && *distance >= self.inner_threshold
                    && self.plane_sector.contains(*delta)
            })
            .map(|(point, ..)| point)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        geometry::AngleUnit,
        pixelcolor::BinaryColor,
        primitives::{PointsIter, Primitive, PrimitiveStyle},
        Pixel,
    };

    #[test]
    fn points_equals_filled() {
        let arc = Arc::with_center(Point::new(10, 10), 5, 0.0.deg(), 90.0.deg());

        let styled_points = arc
            .clone()
            .into_styled(PrimitiveStyle::with_stroke(BinaryColor::On, 1))
            .pixels()
            .map(|Pixel(p, _)| p);

        assert!(arc.points().eq(styled_points));
    }
}