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
use crate::{
geometry::{Dimensions, Point},
primitives::{
common::{Scanline, StrokeOffset},
triangle::{scanline_iterator::ScanlineIterator, Triangle},
},
};
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
#[cfg_attr(feature = "defmt", derive(::defmt::Format))]
pub struct Points {
scanline_iter: ScanlineIterator,
current_line: Scanline,
}
impl Points {
pub(in crate::primitives) fn new(triangle: &Triangle) -> Self {
let scanline_iter = ScanlineIterator::new(
triangle,
0,
StrokeOffset::None,
true,
&triangle.bounding_box(),
);
let current_line = Scanline::new_empty(0);
Self {
scanline_iter,
current_line,
}
}
}
impl Iterator for Points {
type Item = Point;
fn next(&mut self) -> Option<Self::Item> {
self.current_line.next().or_else(|| {
self.current_line = self.scanline_iter.next()?.0;
self.current_line.next()
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
pixelcolor::BinaryColor,
primitives::{PointsIter, Primitive, PrimitiveStyle},
transform::Transform,
Pixel,
};
#[test]
fn points_iter() {
let triangle = Triangle::new(Point::new(5, 10), Point::new(15, 20), Point::new(10, 15));
let styled_points = triangle
.clone()
.into_styled(PrimitiveStyle::with_fill(BinaryColor::On))
.pixels()
.map(|Pixel(p, _)| p);
assert!(triangle.points().eq(styled_points));
}
#[test]
fn off_screen_still_draws_points() {
let off_screen = Triangle::new(Point::new(10, 10), Point::new(20, 20), Point::new(30, -30));
let on_screen = off_screen.translate(Point::new(0, 35));
assert!(off_screen
.points()
.eq(on_screen.points().map(|p| p - Point::new(0, 35))));
}
#[test]
fn it_draws_unfilled_tri_line_y() {
let mut tri = Triangle::new(Point::new(2, 2), Point::new(2, 4), Point::new(2, 4)).points();
assert_eq!(tri.next(), Some(Point::new(2, 2)));
assert_eq!(tri.next(), Some(Point::new(2, 3)));
assert_eq!(tri.next(), Some(Point::new(2, 4)));
assert_eq!(tri.next(), None);
}
#[test]
fn it_draws_unfilled_tri_line_x() {
let mut tri = Triangle::new(Point::new(2, 2), Point::new(4, 2), Point::new(4, 2)).points();
assert_eq!(tri.next(), Some(Point::new(2, 2)));
assert_eq!(tri.next(), Some(Point::new(3, 2)));
assert_eq!(tri.next(), Some(Point::new(4, 2)));
assert_eq!(tri.next(), None);
}
}