From 45d1af9c5967dd04926c00e580d578bef89ffc21 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Tue, 17 Dec 2019 22:19:37 +0100 Subject: [PATCH] Add first transformed Polygon --- fractional/src/easel.rs | 5 ++--- fractional/src/main.rs | 47 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/fractional/src/easel.rs b/fractional/src/easel.rs index 4d7ef90..405ae1d 100644 --- a/fractional/src/easel.rs +++ b/fractional/src/easel.rs @@ -50,8 +50,7 @@ impl Coordinate { let Coordinate(x, y) = v[0]; if x != bx || y != by { - let doinc = (2*err >= dy, 2*err <= dx); - let (x, y, err) = match doinc { + let (x, y, err) = match (2*err >= dy, 2*err <= dx) { (true, false) => (x + sx, y, err + dy), (false, true) => ( x, y + sy, err + dx), _ => (x + sx, y + sy, err + dx + dy ), @@ -218,7 +217,7 @@ impl Drawable for Polygon { r.append(&mut i.line(j)[1..].to_vec()); i = *j; } - let mut j = i.line(&a); + let mut j = a.line(&i); let l = j.len(); r.append(&mut j[1..l-1].to_vec()); Coordinates(r) diff --git a/fractional/src/main.rs b/fractional/src/main.rs index 5c8a757..c407919 100644 --- a/fractional/src/main.rs +++ b/fractional/src/main.rs @@ -254,9 +254,50 @@ fn _line() { println!(); let pg = Polygon( - Coordinates(vec!( Coordinate( 0, -20) - , Coordinate( 20, 20) - , Coordinate(-20, 20) ))); + Coordinates(vec!( Coordinate( 0, -10) + , Coordinate( 10, 10) + , Coordinate(-10, 10) ))); + println!("{:>14} : {}", pg, pg.plot()); + + let i = Vector(Fractional( 0,1), Fractional(-30,1), Fractional(0,1)); + let j = Vector(Fractional( 30,1), Fractional( 30,1), Fractional(0,1)); + let k = Vector(Fractional(-30,1), Fractional( 30,1), Fractional(0,1)); + + let rot :TMatrix = rotate_z(20); + let Vector(ix, iy, _) = rot.apply(&i); + let Vector(jx, jy, _) = rot.apply(&j); + let Vector(kx, ky, _) = rot.apply(&k); + + fn to_i32(x :Fractional) -> i32 { + let Fractional(n, d) = x; + (n / d + if (n % d).abs() < (n / 2).abs() { 0 } else { 1 }) as i32 + } + + println!(); + let pg = Polygon( + Coordinates(vec!( Coordinate(to_i32(ix) + 100, to_i32(iy) + 100) + , Coordinate(to_i32(jx) + 100, to_i32(jy) + 100) + , Coordinate(to_i32(kx) + 100, to_i32(ky) + 100) ))); + println!("{:>14} : {}", pg, pg.plot()); + + let i = Vector( 0.0, -30.0, 0.0); + let j = Vector( 30.0, 30.0, 0.0); + let k = Vector(-30.0, 30.0, 0.0); + + let rot :TMatrix = rotate_z(20); + let Vector(ix, iy, _) = rot.apply(&i); + let Vector(jx, jy, _) = rot.apply(&j); + let Vector(kx, ky, _) = rot.apply(&k); + + fn to_i32_2(x :f64) -> i32 { + x.round() as i32 + } + + println!(); + let pg = Polygon( + Coordinates(vec!( Coordinate(to_i32_2(ix) + 100, to_i32_2(iy) + 100) + , Coordinate(to_i32_2(jx) + 100, to_i32_2(jy) + 100) + , Coordinate(to_i32_2(kx) + 100, to_i32_2(ky) + 100) ))); println!("{:>14} : {}", pg, pg.plot()); }