Improve anti-overflow in SEG::intersects.

Caused issues when testing against
A=( -INT_MAX, ... ), B=( INT_MAX, ... ) segments.

Fixes https://gitlab.com/kicad/code/kicad/issues/14293


(cherry picked from commit 98e635869f)
This commit is contained in:
Alex 2023-03-16 05:03:21 +03:00 committed by dsa-t
parent 1c06f4cf06
commit b466bfa6c8
1 changed files with 5 additions and 5 deletions

View File

@ -149,9 +149,9 @@ const VECTOR2I SEG::NearestPoint( const SEG& aSeg ) const
bool SEG::intersects( const SEG& aSeg, bool aIgnoreEndpoints, bool aLines, VECTOR2I* aPt ) const bool SEG::intersects( const SEG& aSeg, bool aIgnoreEndpoints, bool aLines, VECTOR2I* aPt ) const
{ {
const VECTOR2I e( B - A ); const VECTOR2<ecoord> e = VECTOR2<ecoord>( B ) - A;
const VECTOR2I f( aSeg.B - aSeg.A ); const VECTOR2<ecoord> f = VECTOR2<ecoord>( aSeg.B ) - aSeg.A;
const VECTOR2I ac( aSeg.A - A ); const VECTOR2<ecoord> ac = VECTOR2<ecoord>( aSeg.A ) - A;
ecoord d = f.Cross( e ); ecoord d = f.Cross( e );
ecoord p = f.Cross( ac ); ecoord p = f.Cross( ac );
@ -174,8 +174,8 @@ bool SEG::intersects( const SEG& aSeg, bool aIgnoreEndpoints, bool aLines, VECTO
VECTOR2<ecoord> result( aSeg.A.x + rescale( q, (ecoord) f.x, d ), VECTOR2<ecoord> result( aSeg.A.x + rescale( q, (ecoord) f.x, d ),
aSeg.A.y + rescale( q, (ecoord) f.y, d ) ); aSeg.A.y + rescale( q, (ecoord) f.y, d ) );
if( abs( result.x ) >= std::numeric_limits<VECTOR2I::coord_type>::max() if( abs( result.x ) > std::numeric_limits<VECTOR2I::coord_type>::max()
|| abs( result.y ) >= std::numeric_limits<VECTOR2I::coord_type>::max() ) || abs( result.y ) > std::numeric_limits<VECTOR2I::coord_type>::max() )
{ {
return false; return false;
} }