embedded_graphics/primitives/common/
thick_segment_iter.rs1use crate::{
4 geometry::Point,
5 primitives::common::{JoinKind, LineJoin, StrokeOffset, ThickSegment},
6};
7
8#[derive(Clone, Debug)]
15#[cfg_attr(feature = "defmt", derive(::defmt::Format))]
16pub struct ThickSegmentIter<'a> {
17 windows: core::slice::Windows<'a, Point>,
18 start_join: LineJoin,
19 end_join: LineJoin,
20 width: u32,
21 stroke_offset: StrokeOffset,
22 points: &'a [Point],
23 stop: bool,
24}
25
26static EMPTY: &[Point; 0] = &[];
27
28impl<'a> ThickSegmentIter<'a> {
29 pub fn new(points: &'a [Point], width: u32, _stroke_offset: StrokeOffset) -> Self {
31 let stroke_offset = StrokeOffset::None;
34
35 let mut windows = points.windows(3);
36
37 if let Some([start, mid, end]) = windows.next() {
38 let start_join = LineJoin::start(*start, *mid, width, stroke_offset);
39 let end_join = LineJoin::from_points(*start, *mid, *end, width, stroke_offset);
40
41 Self {
42 windows,
43 start_join,
44 end_join,
45 width,
46 stroke_offset,
47 points,
48 stop: false,
49 }
50 } else if let [start, end] = points {
51 let start_join = LineJoin::start(*start, *end, width, stroke_offset);
53 let end_join = LineJoin::end(*start, *end, width, stroke_offset);
54
55 Self {
56 windows: EMPTY.windows(3),
57 start_join,
58 end_join,
59 width,
60 stroke_offset,
61 points,
62 stop: false,
63 }
64 } else {
65 Self::empty()
67 }
68 }
69
70 fn empty() -> Self {
72 Self {
73 windows: EMPTY.windows(3),
74 start_join: LineJoin::empty(),
75 end_join: LineJoin::empty(),
76 width: 0,
77 stroke_offset: StrokeOffset::None,
78 points: EMPTY,
79 stop: true,
80 }
81 }
82}
83
84impl<'a> Iterator for ThickSegmentIter<'a> {
85 type Item = ThickSegment;
86
87 fn next(&mut self) -> Option<Self::Item> {
88 if self.stop {
89 return None;
90 }
91
92 let segment = ThickSegment::new(self.start_join, self.end_join);
93
94 self.start_join = self.end_join;
95
96 if let Some([start, mid, end]) = self.windows.next() {
97 self.end_join =
98 LineJoin::from_points(*start, *mid, *end, self.width, self.stroke_offset);
99 } else if self.end_join.kind != JoinKind::End {
100 let start = *self.points.get(self.points.len() - 2)?;
101 let end = *self.points.last()?;
102
103 self.end_join = LineJoin::end(start, end, self.width, self.stroke_offset);
104 } else {
105 self.stop = true;
106 }
107
108 Some(segment)
109 }
110}