Created
February 21, 2021 03:15
-
-
Save zesterer/1b0bd338d7f0afa83faf33ed64726d8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn ptr_at(pos: Vec2<isize>) -> *mut Px { ... } | |
type Px = u16; | |
pub unsafe fn line(&mut self, a: Vec2<isize>, b: Vec2<isize>, px: Px) { | |
let delta = b - a; | |
let delta_abs = delta.map(|e| e.abs() as usize); | |
let ptr = ptr_at(a.map(|e| e as usize)); | |
let dx = if delta.x > 0 { 1 } else { -1 }; | |
let dy = if delta.y > 0 { 1 } else { -1 } * WIDTH; | |
if delta_abs.x > delta_abs.y { | |
bresenham(ptr, delta_abs, |p| p.offset(dx), |p| p.offset(dy)).for_each(|p| p.write(px)); | |
} else { | |
bresenham(ptr, delta_abs.yx(), |p| p.offset(dy), |p| p.offset(dx)).for_each(|p| p.write(px)); | |
} | |
} | |
/// x is major axis, y is minor axis (flip as needed). | |
#[inline(always)] | |
fn bresenham<P, X, Y>(mut pos: P, delta: Vec2<usize>, mut x: X, mut y: Y) -> impl ExactSizeIterator<Item = P> | |
where P: Copy, X: FnMut(P) -> P, Y: FnMut(P) -> P, | |
{ | |
let dy2 = (delta.y * 2) as isize; | |
let dd2 = dy2 - (delta.x * 2) as isize; | |
let mut err = dy2 - delta.x as isize; | |
(0..delta.x + 1).map(move |_| { | |
err += if err >= 0 { | |
pos = y(pos); | |
dd2 | |
} else { | |
dy2 | |
}; | |
pos = x(pos); | |
pos | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment