gemini_engine/primitives/
line.rs1use crate::core::{CanDraw, ColChar, Vec2D};
2
3pub struct Line {
5    pub pos0: Vec2D,
7    pub pos1: Vec2D,
9    pub fill_char: ColChar,
11}
12
13impl Line {
14    #[must_use]
16    pub const fn new(pos0: Vec2D, pos1: Vec2D, fill_char: ColChar) -> Self {
17        Self {
18            pos0,
19            pos1,
20            fill_char,
21        }
22    }
23}
24
25impl CanDraw for Line {
26    fn draw_to(&self, canvas: &mut impl crate::core::Canvas) {
27        let (mut x, mut y) = self.pos0.into();
28        let (x1, y1) = self.pos1.into();
29
30        let dx = (x1 - x).abs();
31        let sx = if x < x1 { 1 } else { -1 };
32        let dy = -(y1 - y).abs();
33        let sy = if y < y1 { 1 } else { -1 };
34        let mut error = dx + dy;
35
36        loop {
37            canvas.plot(Vec2D::new(x, y), self.fill_char);
38            let e2 = error * 2;
39            if e2 >= dy {
40                if x == x1 {
41                    break;
42                };
43                error += dy;
44                x += sx;
45            };
46            if e2 <= dx {
47                if y == y1 {
48                    break;
49                };
50                error += dx;
51                y += sy;
52            };
53        }
54    }
55}