diff options
Diffstat (limited to 'src/grid.rs')
| -rw-r--r-- | src/grid.rs | 55 |
1 files changed, 22 insertions, 33 deletions
diff --git a/src/grid.rs b/src/grid.rs index 72a849a..ec27fa7 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -4,7 +4,7 @@ use raylib::drawing::RaylibDraw; use raylib::ffi::Color; /// The internal grid length which will be used to snap things to it. -pub const SNAP_SIZE: f32 = 0.5; +pub const SNAP_SIZE: f64 = 0.5; pub const LINE_COLOUR: Color = Color { r: 255, @@ -15,7 +15,7 @@ pub const LINE_COLOUR: Color = Color { /// Snap a vector to the grid with the factor being the sub-grid accuracy. For instance, 0.5 will /// snap to half a grid cell, while 2.0 would snap to every second grid cell -pub fn snap_to_grid(mut vec: Vec2<f32>, snap_fraction: f32) -> Vec2<f32> { +pub fn snap_to_grid(mut vec: Vec2<f64>, snap_fraction: f64) -> Vec2<f64> { vec.x = math::round(vec.x, snap_fraction); vec.y = math::round(vec.y, snap_fraction); @@ -27,46 +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(); - /* - let mut row = 0; + let mut cell = first_cell; + let mut draw_y = transform.point_m_to_px(&cell).y; loop { - let line_y = translation_y_px + (transform.pixels_per_m() * row as f32) as i32; - if line_y > screen_height { + 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; } - rld.draw_line(0, line_y as i32, screen_width, line_y as i32, LINE_COLOUR); - row += 1; } - let mut column = 0; + let mut draw_x = transform.point_m_to_px(&cell).x; loop { - let line_x = translation_x_px + (transform.pixels_per_m() * column as f32) as i32; - if line_x > screen_width { + 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; } - rld.draw_line(line_x as i32, 0, line_x as i32, screen_height, LINE_COLOUR); - column += 1; - } - */ - - // Draw the row lines. - let mut line_y: f32 = translation_y_px as f32; - while line_y <= screen_height as f32 { - rld.draw_line(0, line_y as i32, screen_width, line_y as i32, LINE_COLOUR); - line_y += transform.pixels_per_m(); - } - // Draw the column lines. - let mut line_x: f32 = translation_x_px as f32; - while line_x <= screen_width as f32 { - rld.draw_line(line_x as i32, 0, line_x as i32, screen_height, LINE_COLOUR); - line_x += transform.pixels_per_m(); } } |
