7
mirror of https://gitlab.com/kicad/code/kicad.git synced 2025-04-21 00:21:25 +00:00

BOX2: By-value GetInflated method

This allows better value-semantics and const correctness.

This is a common method, as the Inflate method is
in-place:

  BOX2I inflated = other;
  // 'Inflated' is not inflated yet
  inflated.Inflate( delta );
  // Now it is inflated, but it's not const

This is annoying, as the 'inflated' box cannot easily
be made const. Instead:

  const BOX2I inflated = other.GetInflated( delta );
This commit is contained in:
John Beard 2024-09-10 08:42:54 +01:00
parent 7fe5caffa6
commit f4eb173c43
4 changed files with 29 additions and 13 deletions
common
libs/kimath/include/math
qa/tests/libs/kimath/math

View File

@ -734,21 +734,15 @@ BOX2I EDA_TEXT::GetTextBox( int aLine ) const
bool EDA_TEXT::TextHitTest( const VECTOR2I& aPoint, int aAccuracy ) const
{
BOX2I rect = GetTextBox();
VECTOR2I location = aPoint;
rect.Inflate( aAccuracy );
RotatePoint( location, GetDrawPos(), -GetDrawRotation() );
const BOX2I rect = GetTextBox().GetInflated( aAccuracy );
const VECTOR2I location = GetRotated( aPoint, GetDrawPos(), -GetDrawRotation() );
return rect.Contains( location );
}
bool EDA_TEXT::TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy ) const
{
BOX2I rect = aRect;
rect.Inflate( aAccuracy );
const BOX2I rect = aRect.GetInflated( aAccuracy );
if( aContains )
return rect.Contains( GetTextBox() );

View File

@ -89,8 +89,7 @@ MARKER_BASE::~MARKER_BASE()
bool MARKER_BASE::HitTestMarker( const VECTOR2I& aHitPosition, int aAccuracy ) const
{
BOX2I bbox = GetBoundingBoxMarker();
bbox.Inflate( aAccuracy );
const BOX2I bbox = GetBoundingBoxMarker().GetInflated( aAccuracy );
// Fast hit test using boundary box. A finer test will be made if requested
bool hit = bbox.Contains( aHitPosition );

View File

@ -620,6 +620,24 @@ public:
return *this;
}
/**
* Get a new rectangle that is this one, inflated by \a aDx and \a aDy.
*/
BOX2<Vec> GetInflated( coord_type aDx, coord_type aDy ) const
{
BOX2<Vec> ret( *this );
ret.Inflate( aDx, aDy );
return ret;
}
/**
* Get a new rectangle that is this one, inflated by \a aDelta.
*/
BOX2<Vec> GetInflated( coord_type aDelta ) const
{
return GetInflated( aDelta, aDelta );
}
/**
* Modify the position and size of the rectangle in order to contain \a aRect.
*

View File

@ -54,10 +54,15 @@ BOOST_AUTO_TEST_CASE( BasicInt )
// Check the equality operator
BOOST_CHECK( box == BOX2I( VECTOR2I( 1, 2 ), VECTOR2I( 3, 4 ) ) );
// Inflate
const BOX2I inflated = BOX2I( box ).Inflate( 1 );
// Inflate in-place
BOX2I inflated = box;
inflated.Inflate( 1 );
BOOST_TEST( inflated.GetPosition() == VECTOR2I( 0, 1 ) );
BOOST_TEST( inflated.GetSize() == VECTOR2I( 5, 6 ) );
// GetInflated
const BOX2I inflated2 = box.GetInflated( 1 );
BOOST_TEST( inflated2 == inflated );
}
BOOST_AUTO_TEST_CASE( BasicDouble )