From 8602326af5884b988e69dc22a090d13882d1aac2 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 22 Nov 2022 13:42:52 +0100 Subject: [PATCH] Plot: round hlines, vlines, and grid lines to pixels to avoid aliasing --- crates/egui/src/widgets/plot/items/mod.rs | 24 +++++++++++++++++------ crates/egui/src/widgets/plot/mod.rs | 3 +++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/crates/egui/src/widgets/plot/items/mod.rs b/crates/egui/src/widgets/plot/items/mod.rs index aa24e60a..3be7e9f1 100644 --- a/crates/egui/src/widgets/plot/items/mod.rs +++ b/crates/egui/src/widgets/plot/items/mod.rs @@ -176,7 +176,7 @@ impl HLine { } impl PlotItem for HLine { - fn shapes(&self, _ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec) { + fn shapes(&self, ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec) { let HLine { y, stroke, @@ -184,9 +184,15 @@ impl PlotItem for HLine { style, .. } = self; + + // Round to minimize aliasing: let points = vec![ - transform.position_from_point(&PlotPoint::new(transform.bounds().min[0], *y)), - transform.position_from_point(&PlotPoint::new(transform.bounds().max[0], *y)), + ui.ctx().round_pos_to_pixels( + transform.position_from_point(&PlotPoint::new(transform.bounds().min[0], *y)), + ), + ui.ctx().round_pos_to_pixels( + transform.position_from_point(&PlotPoint::new(transform.bounds().max[0], *y)), + ), ]; style.style_line(points, *stroke, *highlight, shapes); } @@ -286,7 +292,7 @@ impl VLine { } impl PlotItem for VLine { - fn shapes(&self, _ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec) { + fn shapes(&self, ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec) { let VLine { x, stroke, @@ -294,9 +300,15 @@ impl PlotItem for VLine { style, .. } = self; + + // Round to minimize aliasing: let points = vec![ - transform.position_from_point(&PlotPoint::new(*x, transform.bounds().min[1])), - transform.position_from_point(&PlotPoint::new(*x, transform.bounds().max[1])), + ui.ctx().round_pos_to_pixels( + transform.position_from_point(&PlotPoint::new(*x, transform.bounds().min[1])), + ), + ui.ctx().round_pos_to_pixels( + transform.position_from_point(&PlotPoint::new(*x, transform.bounds().max[1])), + ), ]; style.style_line(points, *stroke, *highlight, shapes); } diff --git a/crates/egui/src/widgets/plot/mod.rs b/crates/egui/src/widgets/plot/mod.rs index 33331d11..8367d9ab 100644 --- a/crates/egui/src/widgets/plot/mod.rs +++ b/crates/egui/src/widgets/plot/mod.rs @@ -1416,6 +1416,9 @@ impl PreparedPlot { let mut p1 = pos_in_gui; p0[1 - axis] = transform.frame().min[1 - axis]; p1[1 - axis] = transform.frame().max[1 - axis]; + // Round to avoid aliasing + p0 = ui.ctx().round_pos_to_pixels(p0); + p1 = ui.ctx().round_pos_to_pixels(p1); shapes.push(Shape::line_segment([p0, p1], Stroke::new(1.0, line_color))); }