aboutsummaryrefslogtreecommitdiff
path: root/evc/src/debug.rs
blob: fb2b53d43a4a4b75a47f3def04b1c793a84132ba (plain)
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
use crate::{frame::Frame, helpers::pixel::Pixel, helpers::vector::Vec2, view::View};

impl View<&mut Frame> {
    pub fn draw_box(&mut self, color: Pixel) {
        let w = self.size.x;
        let h = self.size.y;
        for x in 0..w {
            self[(x, 0)] = color;
            self[(x, h - 1)] = color;
        }
        for y in 0..h {
            self[(0, y)] = color;
            self[(w - 1, y)] = color;
        }
    }
}

impl Frame {
    pub fn draw_line(&mut self, start: Vec2<isize>, end: Vec2<isize>, color: Pixel) {
        let (sx, sy) = (start.x as f32, start.y as f32);
        let (ex, ey) = (end.x as f32, end.y as f32);
        let (dx, dy) = (ex - sx, ey - sy);
        let len = (dx * dx + dy * dy).sqrt();
        let (nx, ny) = (dx / len, dy / len);
        let (mut cx, mut cy) = (sx, sy);
        let mut lc = 0.0;
        while lc < len {
            self.set(
                Vec2 {
                    x: cx as isize,
                    y: cy as isize,
                },
                color,
            );
            lc += 0.5;
            cx += nx * 0.5;
            cy += ny * 0.5;
        }
    }
}

impl Pixel {
    pub const RED: Pixel = Pixel { r: 255, g: 0, b: 0 };
    pub const GREEN: Pixel = Pixel { r: 0, g: 255, b: 0 };
    pub const BLUE: Pixel = Pixel { r: 0, g: 0, b: 255 };
}