From 97894620cd648648bdeba62e9b35c84dcf0e1962 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sun, 8 Jan 2023 19:18:53 +0100 Subject: fix janky lines Turns out there was a small copy-and-paste error that caused the polygons to not line up with the circles. The other change doesn't impact line rendering, but ensures that we stay in u64 territory as much as possible to avoid floating point imprecisions if the integers do get too big. --- src/renderer.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/renderer.rs b/src/renderer.rs index 14dfb6f..c523268 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -38,6 +38,20 @@ fn render_circle(layer: &mut TileLayer

, center: (u64, u64), radius: } } +fn direction_vector(a: (u64, u64), b: (u64, u64)) -> Vector2 { + let dx = if b.0 > a.0 { + (b.0 - a.0) as f64 + } else { + -((a.0 - b.0) as f64) + }; + let dy = if b.1 > a.1 { + (b.1 - a.1) as f64 + } else { + -((a.1 - b.1) as f64) + }; + vector![dx, dy] +} + fn render_line( layer: &mut TileLayer

, start: (u64, u64), @@ -58,16 +72,15 @@ fn render_line( a[0] + b[0] as u64 }; let y = if b[1] < 0 { - a[1] - b[0].abs() as u64 + a[1] - b[1].abs() as u64 } else { a[1] + b[1] as u64 }; vector![x, y] } - let r = vector![end.0 as f64, end.1 as f64] - vector![start.0 as f64, start.1 as f64]; - let r = r.normalize(); - let normal = vector![r[1], -r[0]]; + let r = direction_vector(start, end); + let normal = vector![r[1], -r[0]].normalize(); let start = vector![start.0, start.1]; let end = vector![end.0, end.1]; -- cgit v1.2.3