fast image operations
Diffstat (limited to 'src/drawing/tri.rs')
| -rw-r--r-- | src/drawing/tri.rs | 17 |
1 files changed, 7 insertions, 10 deletions
diff --git a/src/drawing/tri.rs b/src/drawing/tri.rs index 3cc390b..c7256c5 100644 --- a/src/drawing/tri.rs +++ b/src/drawing/tri.rs @@ -3,19 +3,15 @@ use crate::Image; impl<const CHANNELS: usize> Image<&mut [u8], CHANNELS> { /// Draw a (filled) triangle - /// - /// # Safety - /// - /// UB if any point is out of bounds /// ``` /// # use fimg::*; /// let mut a = Image::alloc(10, 10); /// // draw a triangle from point a v point b v point c v /// // with color white - /// unsafe { a.as_mut().tri((3.0, 2.0), (8.0, 7.0), (1.0, 8.0), [255]) }; + /// a.as_mut().tri((3.0, 2.0), (8.0, 7.0), (1.0, 8.0), [255]); /// # assert_eq!(a.buffer(), b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); /// ``` - pub unsafe fn tri( + pub fn tri( &mut self, (x1, y1): (f32, f32), (x2, y2): (f32, f32), @@ -33,10 +29,11 @@ impl<const CHANNELS: usize> Image<&mut [u8], CHANNELS> { } let d = (x3 - x2) * (y as f32 - y2) - (y3 - y2) * (x as f32 - x2); - if d == 0.0 || (d < 0.0) == (s + t <= 0.0) { - // SAFETY: - // caller gurantees triangle is in bounds, this loops over the - // bounding box of the triangle, therefore this is fine. + if (d == 0.0 || (d < 0.0) == (s + t <= 0.0)) + && x < self.width() + && y < self.height() + { + // SAFETY: we just checked the bounds unsafe { self.set_pixel(x, y, c) }; } } |