fast image operations
Diffstat (limited to 'src/drawing/tri.rs')
| -rw-r--r-- | src/drawing/tri.rs | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/src/drawing/tri.rs b/src/drawing/tri.rs index b50dfe4..ca44509 100644 --- a/src/drawing/tri.rs +++ b/src/drawing/tri.rs @@ -1,5 +1,7 @@ //! trongle drawing +use crate::math::madd; +use std::cmp::{max, min}; use crate::Image; @@ -25,21 +27,22 @@ impl<T: AsMut<[u8]> + AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS> { c: [u8; CHANNELS], ) { // TODO optimize - for y in y1.min(y2).min(y3) as u32..y1.max(y2).max(y3) as u32 { - for x in x1.min(x2).min(x3) as u32..x1.max(x2).max(x3) as u32 { - let s = (x1 - x3).mul_add(y as f32 - y3, -(y1 - y2) * (x as f32 - x3)); - let t = (x2 - x1).mul_add(y as f32 - y1, -(y2 - y1) * (x as f32 - x1)); + let ymin = max(y1.min(y2).min(y3) as u32, 0); + let ymax = min(y1.max(y2).max(y3) as u32, self.height()); + let xmin = max(x1.min(x2).min(x3) as u32, 0); + let xmax = min(x1.max(x2).max(x3) as u32, self.width()); + for y in ymin..ymax { + for x in xmin..xmax { + let s = madd(x1 - x3, y as f32 - y3, -(y1 - y2) * (x as f32 - x3)); + let t = madd(x2 - x1, y as f32 - y1, -(y2 - y1) * (x as f32 - x1)); if (s < 0.0) != (t < 0.0) && s != 0.0 && t != 0.0 { continue; } - let d = (x3 - x2).mul_add(y as f32 - y2, -(y3 - y2) * (x as f32 - x2)); - if (d == 0.0 || (d < 0.0) == (s + t <= 0.0)) - && x < self.width() - && y < self.height() - { - // SAFETY: we just checked the bounds + let d = madd(x3 - x2, y as f32 - y2, -(y3 - y2) * (x as f32 - x2)); + if d == 0.0 || (d < 0.0) == (s + t <= 0.0) { + // SAFETY: x, y are bounded unsafe { self.set_pixel(x, y, c) }; } } |