added a line drawing algorithm
will be needed to make it a bit more accurate than just 8 directions.
This commit is contained in:
parent
bd29cd97c3
commit
e9770286ec
42
src/line_drawing.rs
Normal file
42
src/line_drawing.rs
Normal file
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,6 +16,7 @@ use rtt_target::{rprintln, rtt_init_print};
|
||||||
mod calibration;
|
mod calibration;
|
||||||
mod led;
|
mod led;
|
||||||
mod tilt_compensation;
|
mod tilt_compensation;
|
||||||
|
mod line_drawing;
|
||||||
|
|
||||||
use microbit::{display::blocking::Display, hal::Timer};
|
use microbit::{display::blocking::Display, hal::Timer};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue