Created
January 23, 2020 02:55
-
-
Save mooman219/8825807fcc42e41c2bc87f90a9cc5c2e 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
// MIT | |
#[inline(always)] | |
pub fn line( | |
&mut self, | |
x0: f32, | |
y0: f32, | |
x1: f32, | |
y1: f32, | |
) { | |
let dy = y1 - y0; | |
let dx = x1 - x0; | |
let sy = (1f32).copysign(dy); | |
let sx = (1f32).copysign(dx); | |
let mut y = y0.floor(); | |
let mut x = x0.floor(); | |
if dy >= 0.0 { | |
y += 1.0 | |
} | |
if dx >= 0.0 { | |
x += 1.0 | |
} | |
let tdy = 1.0 / dy; | |
let tdx = if dx == 0.0 { | |
1048576.0 | |
} else { | |
1.0 / dx | |
}; | |
let mut tmy = tdy * (y - y0); | |
let mut tmx = tdx * (x - x0); | |
let tdy = tdy.abs(); | |
let tdx = tdx.abs(); | |
let mut x_prev = x0; | |
let mut y_prev = y0; | |
let mut x_next: f32; | |
let mut y_next: f32; | |
while tmx < 1.0 || tmy < 1.0 { | |
if tmx < tmy { | |
y_next = tmx * dy + y0; | |
x_next = x; | |
tmx += tdx; | |
x += sx; | |
} else { | |
y_next = y; | |
x_next = tmy * dx + x0; | |
tmy += tdy; | |
y += sy; | |
} | |
let coverage = Raster::coverage(x_prev, y_prev, x_next, y_next); | |
self.add(x_prev + x_next, y_prev + y_next, coverage); | |
x_prev = x_next; | |
y_prev = y_next; | |
} | |
let coverage = Raster::coverage(x_prev, y_prev, x1, y1); | |
self.add(x_prev + x1, y_prev + y1, coverage); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment