fast image operations
Update clipline to 0.4 (#4)
Nurzhan Saken 5 months ago
parent 9910292 · commit 86389b3
-rw-r--r--Cargo.toml2
-rw-r--r--src/drawing/line.rs20
2 files changed, 11 insertions, 11 deletions
diff --git a/Cargo.toml b/Cargo.toml
index e205693..676fb1e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -18,7 +18,7 @@ vecto = "0.1.1"
umath = "0.0.7"
fr = { version = "0.1.1", package = "fer", optional = true }
slur = { version = "0.1.0", optional = true }
-clipline = "0.1.2"
+clipline = "0.4.0"
minifb = { version = "0.25.0", default-features = false, features = [
"x11",
"wayland",
diff --git a/src/drawing/line.rs b/src/drawing/line.rs
index ef30d08..f180680 100644
--- a/src/drawing/line.rs
+++ b/src/drawing/line.rs
@@ -3,19 +3,19 @@ use crate::Image;
use vecto::Vec2;
impl<T: AsMut<[u8]> + AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS> {
- /// Draw a line from point a to point b.
+ /// Draw a half-open line from point a to point b.
///
/// Points not in bounds will not be included.
pub fn line(&mut self, a: (i32, i32), b: (i32, i32), color: [u8; CHANNELS]) {
- clipline::clipline(
- ((a.0 as isize, a.1 as isize), (b.0 as isize, b.1 as isize)),
- (
- (0, 0),
- (self.width() as isize - 1, self.height() as isize - 1),
- ),
- // SAFETY: clipline clips
- |x, y| unsafe { self.set_pixel(x as u32, y as u32, color) },
- );
+ clipline::Clip::<i32>::from_size(self.width(), self.height())
+ .unwrap() // fixme: panics if width or height > i32::MAX + 1
+ .line_b_proj(a.0, a.1, b.0, b.1)
+ .into_iter()
+ .flatten()
+ .for_each(|(x, y)| {
+ // SAFETY: x, y are clipped to self.
+ unsafe { self.set_pixel(x, y, color) }
+ });
}
/// Draw a thick line from point a to point b.