55 BOX2(
const Vec& aPos,
const Vec& aSize = Vec(0, 0) ) :
65 m_Pos.x =
m_Pos.y = coord_limits::lowest() / 2 + coord_limits::epsilon();
66 m_Size.x =
m_Size.y = coord_limits::max() - coord_limits::epsilon();
81 template <
class Container>
82 void Compute(
const Container& aPointList )
86 typename Container::const_iterator i;
88 if( !aPointList.size() )
91 vmin = vmax = aPointList[0];
93 for( i = aPointList.begin(); i != aPointList.end(); ++i )
96 vmin.x = std::min( vmin.x, p.x );
97 vmin.y = std::min( vmin.y, p.y );
98 vmax.x = std::max( vmax.x, p.x );
99 vmax.y = std::max( vmax.y, p.y );
111 void Move(
const Vec& aMoveVector )
113 m_Pos += aMoveVector;
143 Vec rel_pos = aPoint -
m_Pos;
158 return ( rel_pos.x >= 0 ) && ( rel_pos.y >= 0 ) && ( rel_pos.y <= size.y) &&
159 ( rel_pos.x <= size.x);
232 Offset( offset.x, offset.y );
284 int top = std::max( me.
m_Pos.y, rect.
m_Pos.y );
307 Vec topLeft, bottomRight;
309 topLeft.x = std::max( me.
m_Pos.x, rect.
m_Pos.x );
311 topLeft.y = std::max( me.
m_Pos.y, rect.
m_Pos.y );
314 if ( topLeft.x < bottomRight.x && topLeft.y < bottomRight.y )
315 return BOX2<Vec>( topLeft, bottomRight - topLeft );
317 return BOX2<Vec>( Vec( 0, 0 ), Vec( 0, 0 ) );
323 bool Intersects(
const Vec& aPoint1,
const Vec& aPoint2 )
const
374 if(
std::abs( rotation - ii ) < ROT_EPSILON )
381 if(
std::abs( rotation - jj ) < ROT_EPSILON )
409 for(
int i = 0; i < 4; i++ )
430 for(
int j = 0; j < 4; j++ )
433 corners[j] += rCentre;
459 double dx =
static_cast<double>( aCenter.x ) - closest.x;
460 double dy =
static_cast<double>( aCenter.y ) - closest.y;
462 double r =
static_cast<double>( aRadius );
464 return ( dx * dx + dy * dy ) <= ( r * r );
485 double fx = (double) farpt.x - aCenter.x;
486 double fy = (
double) farpt.y - aCenter.y;
488 double r = (double) aRadius - (
double) aWidth / 2;
490 return ( fx * fx + fy * fy ) > ( r * r );
495 std::stringstream ss;
497 ss <<
"( box corner " <<
m_Pos.Format() <<
" w " <<
m_Size.x <<
" h " <<
m_Size.y <<
" )";
606 Vec rect_end = rect.
GetEnd();
611 end.x = std::max( end.x, rect_end.x );
612 end.y = std::max( end.y, rect_end.y );
639 end.x = std::max( end.x, aPoint.x );
640 end.y = std::max( end.y, aPoint.y );
658 corners[1].
x = corners[0].
x;
659 corners[1].
y = corners[2].
y;
660 corners[3].
x = corners[2].
x;
661 corners[3].
y = corners[0].
y;
664 for(
int ii = 0; ii < 4; ii++ )
671 for(
int ii = 1; ii < 4; ii++ )
673 start.
x = std::min( start.
x, corners[ii].
x );
674 start.
y = std::min( start.
y, corners[ii].
y );
675 end.
x = std::max( end.
x, corners[ii].
x );
676 end.
y = std::max( end.
y, corners[ii].
y );
713 return m_Size.EuclideanNorm();
724 return xdiff * xdiff + ydiff * ydiff;
791 return Vec( nx, ny );
808 if( aPoint.x < center.x )
813 if( aPoint.y < center.y )
818 return Vec( fx, fy );
827 return ( t1.m_Pos == t2.m_Pos && t1.m_Size == t2.m_Size );
836 return ( t1.m_Pos != t2.m_Pos || t1.m_Size != t2.m_Size );
std::optional< BOX2I > OPT_BOX2I
A 2D bounding box built on top of an origin point and size vector.
ecoord_type SquaredDistance(const BOX2< Vec > &aBox) const
Return the square of the minimum distance between self and box aBox.
void SetOrigin(const Vec &pos)
BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
bool operator==(const BOX2< Vec > &aOther) const
const Vec & GetPosition() const
ecoord_type Distance(const BOX2< Vec > &aBox) const
Return the minimum distance between self and aBox.
const Vec & GetOrigin() const
void Offset(coord_type dx, coord_type dy)
const std::string Format() const
bool Intersects(const BOX2< Vec > &aRect, const EDA_ANGLE &aRotation) const
BOX2< Vec > Intersect(const BOX2< Vec > &aRect)
ecoord_type Diagonal() const
Return the length of the diagonal of the rectangle.
void SetSize(coord_type w, coord_type h)
bool Intersects(const Vec &aPoint1, const Vec &aPoint2) const
void RevertYAxis()
Mirror the rectangle from the X axis (negate Y pos and size).
BOX2(const Vec &aPos, const Vec &aSize=Vec(0, 0))
bool Contains(coord_type x, coord_type y) const
void SetX(coord_type val)
BOX2< Vec > & Inflate(int aDelta)
Inflate the rectangle horizontally and vertically by aDelta.
BOX2< Vec > & Merge(const Vec &aPoint)
Modify the position and size of the rectangle in order to contain the given point.
const Vec FarthestPointTo(const Vec &aPoint) const
Return the point in this rect that is farthest from the provided point.
bool operator!=(const BOX2< Vec > &aOther) const
const Vec GetCenter() const
bool Intersects(const BOX2< Vec > &aRect) const
void SetY(coord_type val)
bool Contains(const BOX2< Vec > &aRect) const
Vec::extended_type ecoord_type
bool IntersectsCircleEdge(const Vec &aCenter, const int aRadius, const int aWidth) const
coord_type GetTop() const
bool IntersectsCircle(const Vec &aCenter, const int aRadius) const
coord_type GetHeight() const
ecoord_type SquaredDistance(const Vec &aP) const
void Offset(const Vec &offset)
coord_type GetWidth() const
void SetWidth(coord_type val)
void Move(const Vec &aMoveVector)
Move the rectangle by the aMoveVector.
std::numeric_limits< coord_type > coord_limits
void SetEnd(const Vec &pos)
const Vec ClosestPointTo(const Vec &aPoint) const
Return the point in this rect that is closest to the provided point.
Vec::coord_type coord_type
void SetSize(const Vec &size)
bool Contains(const Vec &aPoint) const
void SetOrigin(coord_type x, coord_type y)
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
ecoord_type Distance(const Vec &aP) const
coord_type GetRight() const
ecoord_type GetArea() const
Return the area of the rectangle.
coord_type GetLeft() const
const Vec & GetSize() const
const BOX2< Vec > GetBoundingBoxRotated(const VECTOR2I &aRotCenter, const EDA_ANGLE &aAngle) const
Useful to calculate bounding box of rotated items, when rotation is not cardinal.
void SetHeight(coord_type val)
void Compute(const Container &aPointList)
Compute the bounding box from a given list of points.
coord_type GetBottom() const
void SetEnd(coord_type x, coord_type y)
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
static constexpr EDA_ANGLE & ANGLE_180
static constexpr EDA_ANGLE & ANGLE_360
static constexpr EDA_ANGLE & ANGLE_90
static constexpr EDA_ANGLE & ANGLE_0
static constexpr EDA_ANGLE & ANGLE_270
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
bool SegmentIntersectsSegment(const VECTOR2I &a_p1_l1, const VECTOR2I &a_p2_l1, const VECTOR2I &a_p1_l2, const VECTOR2I &a_p2_l2, VECTOR2I *aIntersectionPoint=nullptr)
Test if two lines intersect.