Plot: round hlines, vlines, and grid lines to pixels to avoid aliasing

This commit is contained in:
Emil Ernerfeldt 2022-11-22 13:42:52 +01:00
parent 8671aa26e1
commit 8602326af5
2 changed files with 21 additions and 6 deletions

View file

@ -176,7 +176,7 @@ impl HLine {
} }
impl PlotItem for HLine { impl PlotItem for HLine {
fn shapes(&self, _ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) { fn shapes(&self, ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
let HLine { let HLine {
y, y,
stroke, stroke,
@ -184,9 +184,15 @@ impl PlotItem for HLine {
style, style,
.. ..
} = self; } = self;
// Round to minimize aliasing:
let points = vec![ let points = vec![
ui.ctx().round_pos_to_pixels(
transform.position_from_point(&PlotPoint::new(transform.bounds().min[0], *y)), 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)), transform.position_from_point(&PlotPoint::new(transform.bounds().max[0], *y)),
),
]; ];
style.style_line(points, *stroke, *highlight, shapes); style.style_line(points, *stroke, *highlight, shapes);
} }
@ -286,7 +292,7 @@ impl VLine {
} }
impl PlotItem for VLine { impl PlotItem for VLine {
fn shapes(&self, _ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) { fn shapes(&self, ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
let VLine { let VLine {
x, x,
stroke, stroke,
@ -294,9 +300,15 @@ impl PlotItem for VLine {
style, style,
.. ..
} = self; } = self;
// Round to minimize aliasing:
let points = vec![ let points = vec![
ui.ctx().round_pos_to_pixels(
transform.position_from_point(&PlotPoint::new(*x, transform.bounds().min[1])), 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])), transform.position_from_point(&PlotPoint::new(*x, transform.bounds().max[1])),
),
]; ];
style.style_line(points, *stroke, *highlight, shapes); style.style_line(points, *stroke, *highlight, shapes);
} }

View file

@ -1416,6 +1416,9 @@ impl PreparedPlot {
let mut p1 = pos_in_gui; let mut p1 = pos_in_gui;
p0[1 - axis] = transform.frame().min[1 - axis]; p0[1 - axis] = transform.frame().min[1 - axis];
p1[1 - axis] = transform.frame().max[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))); shapes.push(Shape::line_segment([p0, p1], Stroke::new(1.0, line_color)));
} }