Fix off-by-one error in grid snapping.

Fixes https://gitlab.com/kicad/code/kicad/issues/9013
This commit is contained in:
Jeff Young 2021-12-09 16:34:25 +00:00
parent f2d6251979
commit 832eb0d019
1 changed files with 15 additions and 1 deletions

View File

@ -413,7 +413,21 @@ int bumpToNextGrid( const int aVal, const int aDirection )
{
constexpr int gridSize = Mils2iu( 50 );
return ( KiROUND( aVal / gridSize ) * gridSize ) + ( aDirection * gridSize );
int base = aVal / gridSize;
int excess = abs( aVal % gridSize );
if( aDirection > 0 )
{
return ( base + 1 ) * gridSize;
}
else if( excess > 0 )
{
return ( base ) * gridSize;
}
else
{
return ( base - 1 ) * gridSize;
}
}