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
//! Scanline iterator.

use crate::primitives::{
    common::{PointType, Scanline, StrokeOffset},
    triangle::scanline_intersections::ScanlineIntersections,
    Rectangle, Triangle,
};
use core::ops::Range;

/// Iterate over every scanline in the triangle's bounding box.
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
#[cfg_attr(feature = "defmt", derive(::defmt::Format))]
pub(in crate::primitives::triangle) struct ScanlineIterator {
    rows: Range<i32>,
    scanline_y: i32,
    intersections: ScanlineIntersections,
}

impl ScanlineIterator {
    /// New.
    pub fn new(
        triangle: &Triangle,
        stroke_width: u32,
        stroke_offset: StrokeOffset,
        has_fill: bool,
        bounding_box: &Rectangle,
    ) -> Self {
        let triangle = triangle.sorted_clockwise();

        let mut rows = bounding_box.rows();

        if let Some(scanline_y) = rows.next() {
            let intersections = ScanlineIntersections::new(
                &triangle,
                stroke_width,
                stroke_offset,
                has_fill,
                scanline_y,
            );

            Self {
                rows,
                scanline_y,
                intersections,
            }
        } else {
            Self::empty()
        }
    }

    const fn empty() -> Self {
        Self {
            rows: 0i32..0,
            scanline_y: 0,
            intersections: ScanlineIntersections::empty(),
        }
    }
}

impl Iterator for ScanlineIterator {
    type Item = (Scanline, PointType);

    fn next(&mut self) -> Option<Self::Item> {
        self.intersections.next().or_else(|| {
            self.scanline_y = self.rows.next()?;

            self.intersections.reset_with_new_scanline(self.scanline_y);

            self.intersections.next()
        })
    }
}