Fix edge case in CIRCLE::Intersect that caused a divide-by-zero crash

Also add some additional test cases.
This commit is contained in:
Roberto Fernandez Bautista 2021-08-25 19:10:49 +01:00
parent d98e93de7e
commit ed39b33d74
2 changed files with 20 additions and 1 deletions

View File

@ -239,6 +239,9 @@ std::vector<VECTOR2I> CIRCLE::Intersect( const CIRCLE& aCircle ) const
if( d > ( r1 + r2 ) || ( d < ( std::abs( r1 - r2 ) ) ) ) if( d > ( r1 + r2 ) || ( d < ( std::abs( r1 - r2 ) ) ) )
return retval; //circles do not intersect return retval; //circles do not intersect
if( d == 0 )
return retval; // circles are co-centered. Don't return intersection points
// Equation (3) // Equation (3)
int64_t x = ( ( d * d ) + ( r1 * r1 ) - ( r2 * r2 ) ) / ( int64_t( 2 ) * d ); int64_t x = ( ( d * d ) + ( r1 * r1 ) - ( r2 * r2 ) ) / ( int64_t( 2 ) * d );
int64_t r1sqMinusXsq = ( r1 * r1 ) - ( x * x ); int64_t r1sqMinusXsq = ( r1 * r1 ) - ( x * x );

View File

@ -271,13 +271,21 @@ static const std::vector<CIR_CIR_VECPT_CASE> intersect_circle_cases = {
}, },
}, },
{ {
"tangent aligned", "tangent aligned, external",
{ { 10, 10 }, 20 }, { { 10, 10 }, 20 },
{ { 10, 50 }, 20 }, { { 10, 50 }, 20 },
{ {
{ 10, 30 }, { 10, 30 },
}, },
}, },
{
"tangent aligned, internal",
{ { 10, 10 }, 40 },
{ { 10, 30 }, 20 },
{
{ 10, 50 },
},
},
{ {
"no intersection", "no intersection",
{ { 10, 10 }, 20 }, { { 10, 10 }, 20 },
@ -318,6 +326,14 @@ static const std::vector<CIR_CIR_VECPT_CASE> intersect_circle_cases = {
//no points //no points
}, },
}, },
{
"Co-centered, same radius", // Exercise d=0
{ { 205999999, 136367974 }, 3742026 },
{ { 205999999, 136367974 }, 3742026 },
{
//no points
},
},
}; };
// clang-format on // clang-format on