aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2023-01-08 19:18:53 +0100
committerDaniel Schadt <kingdread@gmx.de>2023-01-08 19:18:53 +0100
commit97894620cd648648bdeba62e9b35c84dcf0e1962 (patch)
treecf9d321d84bdea1c1937dace02a23d474bd80f3b /src
parent5a046bdd740bb74372baf4bba7ca2130cc174355 (diff)
downloadhittekaart-97894620cd648648bdeba62e9b35c84dcf0e1962.tar.gz
hittekaart-97894620cd648648bdeba62e9b35c84dcf0e1962.tar.bz2
hittekaart-97894620cd648648bdeba62e9b35c84dcf0e1962.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/renderer.rs21
1 files 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<P: Pixel>(layer: &mut TileLayer<P>, center: (u64, u64), radius:
}
}
+fn direction_vector(a: (u64, u64), b: (u64, u64)) -> Vector2<f64> {
+ 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<P: Pixel>(
layer: &mut TileLayer<P>,
start: (u64, u64),
@@ -58,16 +72,15 @@ fn render_line<P: Pixel>(
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];