From 81ad3366f5d7d585e7d87490bc5b3cda8bfd0c07 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Tue, 7 Nov 2017 09:45:03 +0100 Subject: [PATCH] Avoid division by 0 in GAL::GetGridPoint( const VECTOR2D& aPoint ) --- common/gal/graphics_abstraction_layer.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/common/gal/graphics_abstraction_layer.cpp b/common/gal/graphics_abstraction_layer.cpp index b5d763e040..4ed7b6d392 100644 --- a/common/gal/graphics_abstraction_layer.cpp +++ b/common/gal/graphics_abstraction_layer.cpp @@ -394,8 +394,19 @@ void GAL::DrawGrid() VECTOR2D GAL::GetGridPoint( const VECTOR2D& aPoint ) const { +#if 0 + // This old code expects a non zero grid size, which can be wrong here. return VECTOR2D( KiROUND( ( aPoint.x - gridOffset.x ) / gridSize.x ) * gridSize.x + gridOffset.x, KiROUND( ( aPoint.y - gridOffset.y ) / gridSize.y ) * gridSize.y + gridOffset.y ); +#else + // if grid size == 0.0 there is no grid, so use aPoint as grid reference position + double cx = gridSize.x > 0.0 ? KiROUND( ( aPoint.x - gridOffset.x ) / gridSize.x ) * gridSize.x + gridOffset.x + : aPoint.x; + double cy = gridSize.y > 0.0 ? KiROUND( ( aPoint.y - gridOffset.y ) / gridSize.y ) * gridSize.y + gridOffset.y + : aPoint.y; + + return VECTOR2D( cx, cy ); +#endif } const int GAL::MIN_DEPTH = -1024;