use std::f32::consts::FRAC_PI_2;
use image::{ImageBuffer, Rgb};
use imageproc::drawing::{draw_filled_circle_mut, draw_polygon_mut};
use imageproc::point::Point;
pub fn fill_mut(buffer: &mut ImageBuffer<Rgb<u8>, Vec<u8>>, color: Rgb<u8>) {
for pixel in buffer.pixels_mut() {
*pixel = color;
}
}
pub fn draw_line_mut(
buffer: &mut ImageBuffer<Rgb<u8>, Vec<u8>>,
x1: u32,
y1: u32,
x2: u32,
y2: u32,
thickness: f32,
color: Rgb<u8>,
) {
assert!(thickness > 0.0);
let angle = {
if x1 == x2 {
FRAC_PI_2
} else {
((i64::from(y2) - i64::from(y1)) as f64 / (i64::from(x2) - i64::from(x1)) as f64).atan()
as f32
}
};
let perpendicular_angle = angle + FRAC_PI_2;
let p1 = Point::new(
(x1 as f32 + thickness * perpendicular_angle.cos()) as i32,
(y1 as f32 + thickness * perpendicular_angle.sin()) as i32,
);
let p2 = Point::new(
(x1 as f32 - thickness * perpendicular_angle.cos()) as i32,
(y1 as f32 - thickness * perpendicular_angle.sin()) as i32,
);
let p3 = Point::new(
(x2 as f32 + thickness * perpendicular_angle.cos()) as i32,
(y2 as f32 + thickness * perpendicular_angle.sin()) as i32,
);
let p4 = Point::new(
(x2 as f32 - thickness * perpendicular_angle.cos()) as i32,
(y2 as f32 - thickness * perpendicular_angle.sin()) as i32,
);
draw_polygon_mut(buffer, &[p1, p3, p4, p2], color);
draw_filled_circle_mut(
buffer,
(x1 as i32, y1 as i32),
(thickness / 1.5) as i32,
color,
);
draw_filled_circle_mut(
buffer,
(x2 as i32, y2 as i32),
(thickness / 1.5) as i32,
color,
);
}