diff options
| author | Arne Dußin | 2020-11-28 02:22:35 +0100 |
|---|---|---|
| committer | Arne Dußin | 2020-11-28 02:22:35 +0100 |
| commit | 90db142588e1b78250dc9b266bae359cf9e22721 (patch) | |
| tree | ae7655b9384397b8a144503669bc9bae364101bb /src/grid.rs | |
| parent | 4c4b57dc24bc36b3091931c9dcc36f6b1894a017 (diff) | |
| download | graf_karto-90db142588e1b78250dc9b266bae359cf9e22721.tar.gz graf_karto-90db142588e1b78250dc9b266bae359cf9e22721.zip | |
Limit grid inaccuracy to half a pixel
Before, the grid was calculated accumutatively, which was pretty
alright. However, with some zoom levels towards the bottom of the
screen there was a discrepancy between the grid and something drawn.
This should be fixed now, since this update makes the grid use the same
method of calculation for every grid cell that everything else uses to
calculate points. Also, sub-pixel positions are rounded for the grid,
while they were floored before, which should improve accuracy.
Diffstat (limited to 'src/grid.rs')
| -rw-r--r-- | src/grid.rs | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/src/grid.rs b/src/grid.rs index c885f19..ec27fa7 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -27,26 +27,35 @@ pub fn draw_grid<D>(rld: &mut D, screen_width: i32, screen_height: i32, transfor where D: RaylibDraw, { - /* Calculate the actual screen offset of the grid, by modulo-ing the translation of the - * transform. + /* Calculate the first whole meter that can be seen on the grid. This is the first meter that + * will be seen on screen. */ - let translation_x_px: i32 = - transform.translation_px().x as i32 % transform.pixels_per_m() as i32; - let translation_y_px: i32 = - transform.translation_px().y as i32 % transform.pixels_per_m() as i32; + let mut first_cell = *transform.translation_px() / -transform.pixels_per_m(); + first_cell.x = first_cell.x.floor(); + first_cell.y = first_cell.y.floor(); - // Draw the row lines. Add back the subpixels to the translation - let mut line_y: f64 = translation_y_px as f64 - + (transform.translation_px().y - transform.translation_px().y as i32 as f64); - while line_y <= screen_height as f64 { - rld.draw_line(0, line_y as i32, screen_width, line_y as i32, LINE_COLOUR); - line_y += transform.pixels_per_m(); + let mut cell = first_cell; + let mut draw_y = transform.point_m_to_px(&cell).y; + loop { + draw_y = math::round(draw_y, 1.); + rld.draw_line(0, draw_y as i32, screen_width, draw_y as i32, LINE_COLOUR); + cell.y += 1.; + draw_y = transform.point_m_to_px(&cell).y; + + if draw_y as i32 > screen_height { + break; + } } - // Draw the column lines. - let mut line_x: f64 = translation_x_px as f64 - + (transform.translation_px().x - transform.translation_px().x as i32 as f64); - while line_x <= screen_width as f64 { - rld.draw_line(line_x as i32, 0, line_x as i32, screen_height, LINE_COLOUR); - line_x += transform.pixels_per_m(); + + let mut draw_x = transform.point_m_to_px(&cell).x; + loop { + draw_x = math::round(draw_x, 1.); + rld.draw_line(draw_x as i32, 0, draw_x as i32, screen_height, LINE_COLOUR); + cell.x += 1.; + draw_x = transform.point_m_to_px(&cell).x; + + if draw_x as i32 > screen_width { + break; + } } } |
