Handle rounding errors in seg length calculation

SQRT needs to be passed through KiROUND before returning otherwise,
truncation errrors will accumulate between routing and DRC

Fixes https://gitlab.com/kicad/code/kicad/issues/8541
This commit is contained in:
Seth Hillbrand 2021-06-02 16:41:36 -07:00
parent af2707929d
commit cf1c75ecd4
1 changed files with 3 additions and 3 deletions

View File

@ -239,7 +239,7 @@ public:
*/ */
int Distance( const SEG& aSeg ) const int Distance( const SEG& aSeg ) const
{ {
return sqrt( SquaredDistance( aSeg ) ); return KiROUND( sqrt( SquaredDistance( aSeg ) ) );
} }
ecoord SquaredDistance( const VECTOR2I& aP ) const ecoord SquaredDistance( const VECTOR2I& aP ) const
@ -255,7 +255,7 @@ public:
*/ */
int Distance( const VECTOR2I& aP ) const int Distance( const VECTOR2I& aP ) const
{ {
return sqrt( SquaredDistance( aP ) ); return KiROUND( sqrt( SquaredDistance( aP ) ) );
} }
void CanonicalCoefs( ecoord& qA, ecoord& qB, ecoord& qC ) const void CanonicalCoefs( ecoord& qA, ecoord& qB, ecoord& qC ) const
@ -411,7 +411,7 @@ inline int SEG::LineDistance( const VECTOR2I& aP, bool aDetermineSide ) const
ecoord q = ecoord{ B.x } - A.x; ecoord q = ecoord{ B.x } - A.x;
ecoord r = -p * A.x - q * A.y; ecoord r = -p * A.x - q * A.y;
ecoord dist = ( p * aP.x + q * aP.y + r ) / sqrt( p * p + q * q ); ecoord dist = KiROUND( ( p * aP.x + q * aP.y + r ) / sqrt( p * p + q * q ) );
return aDetermineSide ? dist : std::abs( dist ); return aDetermineSide ? dist : std::abs( dist );
} }