From e9770286ec622d32cb75a78b94dd575b28a43e52 Mon Sep 17 00:00:00 2001 From: Gabe Venberg Date: Sun, 22 Oct 2023 16:48:11 -0500 Subject: [PATCH] added a line drawing algorithm will be needed to make it a bit more accurate than just 8 directions. --- src/line_drawing.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + 2 files changed, 43 insertions(+) create mode 100644 src/line_drawing.rs diff --git a/src/line_drawing.rs b/src/line_drawing.rs new file mode 100644 index 0000000..479513b --- /dev/null +++ b/src/line_drawing.rs @@ -0,0 +1,42 @@ +use core::{iter::Iterator, mem::swap}; + +struct Point { + x: isize, + y: isize, +} + + + +fn draw_line(mut p0: Point, mut p1: Point, matrix: &mut [[u8; 5]; 5]) { + let steep = (p0.x - p1.x).abs() < (p0.y - p1.x).abs(); + + if steep { + swap(&mut p0.x, &mut p0.y); + swap(&mut p1.x, &mut p1.y); + } + + if p0.x > p1.x { + swap(&mut p0, &mut p1) + } + + let dx = p1.x - p0.x; + let dy = p1.y - p0.y; + let derror2 = dy.abs() * 2; + let mut error2 = 0; + let mut y = p0.y; + + for x in p0.x..=p1.x { + if steep { + matrix[y as usize][x as usize] = 1; + } else { + matrix[x as usize][y as usize] = 1; + } + + error2 += derror2; + + if error2 > dx { + y += if p1.y > p0.y { 1 } else { -1 }; + error2 -= dx * 2 + } + } +} diff --git a/src/main.rs b/src/main.rs index 429c737..5ec174e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ use rtt_target::{rprintln, rtt_init_print}; mod calibration; mod led; mod tilt_compensation; +mod line_drawing; use microbit::{display::blocking::Display, hal::Timer};