Last active
October 26, 2019 23:49
-
-
Save mooman219/e54a73f1f205742102e3c131203fa4cb 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
pub fn draw_line(&mut self, p0: &Point, p1: &Point) { | |
if p0.y == p1.y { | |
return; | |
} | |
let (dir, p0, p1) = if p0.y < p1.y { | |
(1.0, p0, p1) | |
} else { | |
(-1.0, p1, p0) | |
}; | |
let dxdy = (p1.x - p0.x) / (p1.y - p0.y); | |
let mut x = p0.x; | |
let y0 = p0.y as usize; | |
if p0.y < 0.0 { | |
x -= p0.y * dxdy; | |
} | |
for y in y0..min(self.h, p1.y.ceil() as usize) { | |
let linestart = (y * self.w) as i32; | |
let dy = ((y + 1) as f32).min(p1.y) - (y as f32).max(p0.y); | |
let xnext = x + dxdy * dy; | |
let d = dy * dir; | |
let (x0, x1) = if x < xnext { | |
(x, xnext) | |
} else { | |
(xnext, x) | |
}; | |
let x0floor = x0.floor(); | |
let x0i = x0floor as i32; | |
let x1ceil = x1.ceil(); | |
let x1i = x1ceil as i32; | |
if x1i <= x0i + 1 { | |
let xmf = 0.5 * (x + xnext) - x0floor; | |
self.add(linestart + x0i, d - d * xmf); | |
self.add(linestart + x0i + 1, d * xmf); | |
} else { | |
let s = (x1 - x0).recip(); | |
let x0f = x0 - x0floor; | |
let a0 = 0.5 * s * (1.0 - x0f) * (1.0 - x0f); | |
let x1f = x1 - x1ceil + 1.0; | |
let am = 0.5 * s * x1f * x1f; | |
self.add(linestart + x0i, d * a0); | |
if x1i == x0i + 2 { | |
self.add(linestart + x0i + 1, d * (1.0 - a0 - am)); | |
} else { | |
let a1 = s * (1.5 - x0f); | |
self.add(linestart + x0i + 1, d * (a1 - a0)); | |
for xi in x0i + 2..x1i - 1 { | |
self.add(linestart + xi, d * s); | |
} | |
let a2 = a1 + (x1i - x0i - 3) as f32 * s; | |
self.add(linestart + x1i - 1, d * (1.0 - a2 - am)); | |
} | |
self.add(linestart + x1i, d * am); | |
} | |
x = xnext; | |
} | |
} |
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 line(x0: f32, y0: f32, x1: f32, y1: f32) { | |
if y0 == y1 { | |
// Doesn't matter rn. | |
return; | |
} | |
// x0 == x1 does matter but w/e right now | |
let dx = x1 - x0; | |
let dy = y1 - y0; | |
let (mut px, sx) = if dx < 0.0 { | |
(x0.floor(), -1.0) | |
} else { | |
(x0.floor() + 1.0, 1.0) | |
}; | |
let (mut py, sy) = if dy < 0.0 { | |
(y0.floor(), -1.0) | |
} else { | |
(y0.floor() + 1.0, 1.0) | |
}; | |
let mut dist = (x1 as i32 - x0 as i32).abs() + (y1 as i32 - y0 as i32).abs(); | |
println!("dx {} dy {} dist {}", dx, dy, dist); | |
let xb = 1.0 / (x1 - x0); | |
let yb = 1.0 / (y1 - y0); | |
let xc = -x0 * xb; | |
let yc = -y0 * yb; | |
let mut x = x0; | |
let mut y = y0; | |
println!("S ({}, {})", x0, y0); | |
while dist > 0 { | |
dist -= 1; | |
let tx = px * xb + xc; | |
let ty = py * yb + yc; | |
if tx < ty { | |
x = px; | |
y = tx * dy + y0; | |
px += sx; | |
println!("X ({}, {})", x, y); | |
} else { | |
x = ty * dx + x0; | |
y = py; | |
py += sy; | |
println!("Y ({}, {})", x, y); | |
} | |
} | |
println!("E ({}, {})", x1, y1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment