line drawing works well, room for optimization
This commit is contained in:
parent
ba91baa302
commit
635585dd09
2 changed files with 20 additions and 9 deletions
25
src/main.rs
25
src/main.rs
|
@ -57,13 +57,24 @@ fn main() {
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
fn line(a: Point, b: Point, fb: &mut TGAImage, color: TGAColor) {
|
||||
let mut t = 0.0;
|
||||
let step = 0.02;
|
||||
while t < 1.0 {
|
||||
let x = (a.x as f32 + (b.x - a.x) as f32 * t).round_ties_even() as u32;
|
||||
fn line(mut a: Point, mut b: Point, fb: &mut TGAImage, color: TGAColor) {
|
||||
let is_steep = (a.x - b.x).abs() < (a.y - b.y).abs();
|
||||
if is_steep {
|
||||
std::mem::swap(&mut a.x, &mut a.y);
|
||||
std::mem::swap(&mut b.x, &mut b.y);
|
||||
}
|
||||
if a.x > b.x {
|
||||
std::mem::swap(&mut a.x, &mut b.x);
|
||||
std::mem::swap(&mut a.y, &mut b.y);
|
||||
}
|
||||
|
||||
for x in (a.x)..b.x {
|
||||
let t = (x - a.x) as f32 / (b.x - a.x) as f32;
|
||||
let y = (a.y as f32 + (b.y - a.y) as f32 * t).round_ties_even() as u32;
|
||||
fb.set(x, y, color);
|
||||
t += step;
|
||||
if is_steep {
|
||||
fb.set(y, x as u32, color);
|
||||
} else {
|
||||
fb.set(x as u32, y, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,8 +31,8 @@ impl Sub for Point {
|
|||
|
||||
fn sub(self, rhs: Self) -> Self::Output {
|
||||
Self {
|
||||
x: self.x.saturating_sub(rhs.x),
|
||||
y: self.y.saturating_sub(rhs.y),
|
||||
x: self.x - rhs.x,
|
||||
y: self.y - rhs.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue