Move Bresenham line drawing into library from Hough example
This commit is contained in:
1 parent
fd9b027eea
commit
d550a7e71e
3 files changed
+81
-93
No files matched your search
+43
@@ -0,0 +1,43 @@
|
||||
use crate::*;
|
||||
|
||||
pub fn line<P: Copy>(
|
||||
x0: usize,
|
||||
y0: usize,
|
||||
x1: usize,
|
||||
y1: usize,
|
||||
img: &mut image::Image<P>,
|
||||
colour: P,
|
||||
) {
|
||||
let dx = x0.abs_diff(x1) as isize;
|
||||
let dy = -(y0.abs_diff(y1) as isize);
|
||||
|
||||
let sx = if x0 < x1 { 1 } else { -1 };
|
||||
let sy = if y0 < y1 { 1 } else { -1 };
|
||||
|
||||
let mut x = x0;
|
||||
let mut y = y0;
|
||||
let mut error = dx + dy;
|
||||
|
||||
loop {
|
||||
if x >= img.extent.x || y >= img.extent.y {
|
||||
return;
|
||||
}
|
||||
|
||||
*img.at_mut(x, y) = colour;
|
||||
let e2 = 2 * error;
|
||||
if e2 >= dy {
|
||||
if x == x1 {
|
||||
break;
|
||||
}
|
||||
error += dy;
|
||||
x = (x as isize + sx) as usize;
|
||||
}
|
||||
if e2 <= dx {
|
||||
if y == y1 {
|
||||
break;
|
||||
}
|
||||
error += dx;
|
||||
y = (y as isize + sy) as usize;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod contour;
|
||||
pub mod draw;
|
||||
pub mod filter;
|
||||
pub mod hough;
|
||||
pub mod image;
|
||||
|
||||
Reference in new issue
Block a user