Fix off-by-one error in distance check for arcs

This commit is contained in:
Seth Hillbrand 2024-06-03 16:28:44 -07:00
parent 18e33a0957
commit 590bd6237d
3 changed files with 26 additions and 2 deletions

View File

@ -74,6 +74,18 @@ public:
*/
VECTOR2I NearestPoint( const VECTOR2I& aP ) const;
/**
* Compute the point (floating point version) on the circumference of the circle that
* is the closest to aP.
*
* In other words: finds the intersection point of this circle and a line that passes through
* both this circle's center and aP.
*
* @param aP.
* @return nearest point to aP.
*/
VECTOR2D NearestPoint( const VECTOR2D& aP ) const;
/**
* Compute the intersection points between this circle and \a aCircle.
*

View File

@ -206,6 +206,18 @@ VECTOR2I CIRCLE::NearestPoint( const VECTOR2I& aP ) const
}
VECTOR2D CIRCLE::NearestPoint( const VECTOR2D& aP ) const
{
VECTOR2D vec = aP - Center;
// Handle special case where aP is equal to this circle's center
if( vec.x == 0 && vec.y == 0 )
vec.x = 1; // Arbitrary, to ensure the return value is always on the circumference
return vec.Resize( Radius ) + Center;
}
std::vector<VECTOR2I> CIRCLE::Intersect( const CIRCLE& aCircle ) const
{
// From https://mathworld.wolfram.com/Circle-CircleIntersection.html

View File

@ -432,8 +432,8 @@ bool SHAPE_ARC::Collide( const VECTOR2I& aP, int aClearance, int* aActual,
VECTOR2L center = GetCenter();
double radius = ( center - m_start ).EuclideanNorm();
CIRCLE fullCircle( center, radius );
VECTOR2I nearestPt = fullCircle.NearestPoint( aP );
int dist = ( nearestPt - aP ).EuclideanNorm();
VECTOR2D nearestPt = fullCircle.NearestPoint( VECTOR2D( aP ) );
int dist = nearestPt.Distance( aP );
EDA_ANGLE angleToPt( aP - fullCircle.Center ); // Angle from center to the point
if( !dist )