use crate::display::Display;
use anyhow::Result;
type Point = (isize, isize);
pub fn fill(display: &mut Display, value: u8) {
display.memory.iter_mut().for_each(|mem| *mem = value);
}
pub fn draw_point(display: &mut Display, point: Point, color: u8) {
let index = point.0 + (point.1 / 8) * display.width as isize;
display.memory[index as usize] = color;
}
pub fn draw_line(display: &mut Display, (x1, y1): Point, (x2, y2): Point) {
let dx = (x2 - x1).abs();
let dy = (y2 - y1).abs();
let mut x = x1;
let mut y = y1;
let x_inc = if x2 > x1 { 1 } else { -1 };
let y_inc = if y2 > y1 { 1 } else { -1 };
let mut error = dx - dy;
while x != x2 || y != y2 {
draw_point(display, (x, y), 0xFF);
let double_error = error * 2;
if double_error > -dy {
error -= dy;
x += x_inc;
}
if double_error < dx {
error += dx;
y += y_inc;
}
}
draw_point(display, (x2, y2), 0xFF);
}
pub fn clear(display: &mut Display) -> Result<()> {
fill(display, 0x00);
Ok(())
}