KiCad PCB EDA Suite
EDA_RECT Class Reference

Class EDA_RECT handles the component boundary box. More...

#include <class_eda_rect.h>

Inheritance diagram for EDA_RECT:
BLOCK_SELECTOR

Public Member Functions

 EDA_RECT ()
 
 EDA_RECT (const wxPoint &aPos, const wxSize &aSize)
 
wxPoint Centre () const
 
void Move (const wxPoint &aMoveVector)
 Function Move moves the rectangle by the aMoveVector. More...
 
void Normalize ()
 Function Normalize ensures that the height ant width are positive. More...
 
bool Contains (const wxPoint &aPoint) const
 Function Contains. More...
 
bool Contains (int x, int y) const
 Function Contains. More...
 
bool Contains (const EDA_RECT &aRect) const
 Function Contains. More...
 
const wxSize & GetSize () const
 
int GetSizeMax () const
 GetSizeMax. More...
 
int GetX () const
 
int GetY () const
 
const wxPointGetOrigin () const
 
const wxPointGetPosition () const
 
const wxPoint GetEnd () const
 
const wxPoint GetCenter () const
 
int GetWidth () const
 
int GetHeight () const
 
int GetRight () const
 
int GetLeft () const
 
int GetTop () const
 
int GetBottom () const
 
void SetOrigin (const wxPoint &pos)
 
void SetOrigin (int x, int y)
 
void SetSize (const wxSize &size)
 
void SetSize (int w, int h)
 
void Offset (int dx, int dy)
 
void Offset (const wxPoint &offset)
 
void SetX (int val)
 
void SetY (int val)
 
void SetWidth (int val)
 
void SetHeight (int val)
 
void SetEnd (int x, int y)
 
void SetEnd (const wxPoint &pos)
 
void RevertYAxis ()
 Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size) More...
 
bool Intersects (const EDA_RECT &aRect) const
 Function Intersects tests for a common area between rectangles. More...
 
bool Intersects (const EDA_RECT &aRect, double aRot) const
 Tests for a common area between this rectangle, and a rectangle with arbitrary rotation. More...
 
bool Intersects (const wxPoint &aPoint1, const wxPoint &aPoint2) const
 Function Intersects tests for a common area between a segment and this rectangle. More...
 
const wxPoint ClosestPointTo (const wxPoint &aPoint) const
 Return the point in this rect that is closest to the provided point. More...
 
const wxPoint FarthestPointTo (const wxPoint &aPoint) const
 Return the point in this rect that is farthest from the provided point. More...
 
bool IntersectsCircle (const wxPoint &aCenter, const int aRadius) const
 Function IntersectsCircle tests for a common area between a circle and this rectangle. More...
 
bool IntersectsCircleEdge (const wxPoint &aCenter, const int aRadius, const int aWidth) const
 IntersectsCircleEdge Tests for intersection between this rect and the edge (radius) of a circle. More...
 
 operator wxRect () const
 Function operator(wxRect) overloads the cast operator to return a wxRect wxRect does not accept negative values for size, so ensure the wxRect size is always >= 0. More...
 
 operator BOX2I () const
 Function operator(BOX2I) overloads the cast operator to return a BOX2I. More...
 
EDA_RECTInflate (wxCoord dx, wxCoord dy)
 Function Inflate inflates the rectangle horizontally by dx and vertically by dy. More...
 
EDA_RECTInflate (int aDelta)
 Function Inflate inflates the rectangle horizontally and vertically by aDelta. More...
 
void Merge (const EDA_RECT &aRect)
 Function Merge modifies the position and size of the rectangle in order to contain aRect. More...
 
void Merge (const wxPoint &aPoint)
 Function Merge modifies the position and size of the rectangle in order to contain the given point. More...
 
double GetArea () const
 Function GetArea returns the area of the rectangle. More...
 
EDA_RECT Common (const EDA_RECT &aRect) const
 Function Common returns the area that is common with another rectangle. More...
 
const EDA_RECT GetBoundingBoxRotated (wxPoint aRotCenter, double aAngle)
 Function GetBoundingBoxRotated. More...
 

Private Attributes

wxPoint m_Pos
 
wxSize m_Size
 

Detailed Description

Class EDA_RECT handles the component boundary box.

This class is similar to wxRect, but some wxRect functions are very curious, and are working only if dimensions are >= 0 (not always the case in KiCad) and also KiCad needs some specific method. so I prefer this more suitable class

Definition at line 44 of file class_eda_rect.h.

Constructor & Destructor Documentation

EDA_RECT::EDA_RECT ( )
inline

Definition at line 51 of file class_eda_rect.h.

51 { };
EDA_RECT::EDA_RECT ( const wxPoint aPos,
const wxSize &  aSize 
)
inline

Definition at line 53 of file class_eda_rect.h.

53  :
54  m_Pos( aPos ),
55  m_Size( aSize )
56  { }
wxSize m_Size
wxPoint m_Pos

Member Function Documentation

wxPoint EDA_RECT::Centre ( ) const
inline

Definition at line 58 of file class_eda_rect.h.

References wxPoint::x, and wxPoint::y.

Referenced by PCB_EDIT_FRAME::AppendBoardFile(), LIB_VIEW_FRAME::BestZoom(), PCB_BASE_FRAME::BestZoom(), GERBVIEW_FRAME::BestZoom(), PCB_EDIT_FRAME::Block_Flip(), PCB_EDIT_FRAME::Block_Rotate(), compareX(), compareY(), SCH_FIELD::Draw(), LIB_TEXT::drawGraphic(), BOARD_PRINTOUT_CONTROLLER::DrawPage(), BOARD_NETLIST_UPDATER::estimateComponentInsertionPosition(), AUTOPLACER::field_box_placement(), AUTOPLACER::field_horiz_placement(), GENDRILL_WRITER_BASE::genDrillMapFile(), SELECTION::GetCenter(), DRAWSEGMENT::GetCenter(), FOOTPRINT_EDIT_FRAME::HandleBlockEnd(), LIB_EDIT_FRAME::HandleBlockEnd(), SCH_EDIT_FRAME::HandleBlockEnd(), LIB_EDIT_FRAME::HandleBlockPlace(), initializePlotter(), CINFO3D_VISU::InitSettings(), Intersects(), SCH_FIELD::IsHorizJustifyFlipped(), LIB_PIN::Matches(), SCH_TEXT::Matches(), SCH_FIELD::Matches(), SCH_SHEET_PIN::Matches(), SCH_EDIT_FRAME::MirrorSheet(), PCB_EDIT_FRAME::OnExportIDF3(), DIALOG_LIB_EDIT_PIN::OnPaintShowPanel(), LIB_TEXT::Plot(), SCH_FIELD::Plot(), LIB_FIELD::Plot(), DIALOG_RESCUE_EACH::renderPreview(), DIALOG_CHOOSE_COMPONENT::RenderPreview(), BOARD::ReplaceNetlist(), SCH_EDIT_FRAME::RotateHierarchicalSheet(), and EDA_DRAW_FRAME::Window_Zoom().

59  {
60  return wxPoint( m_Pos.x + ( m_Size.x >> 1 ),
61  m_Pos.y + ( m_Size.y >> 1 ) );
62  }
wxSize m_Size
wxPoint m_Pos
const wxPoint EDA_RECT::ClosestPointTo ( const wxPoint aPoint) const

Return the point in this rect that is closest to the provided point.

Definition at line 541 of file base_struct.cpp.

References GetBottom(), GetLeft(), GetRight(), GetTop(), max, min, Normalize(), wxPoint::x, and wxPoint::y.

Referenced by IntersectsCircle().

542 {
543  EDA_RECT me( *this );
544 
545  me.Normalize(); // ensure size is >= 0
546 
547  // Determine closest point to the circle centre within this rect
548  int nx = std::max( me.GetLeft(), std::min( aPoint.x, me.GetRight() ) );
549  int ny = std::max( me.GetTop(), std::min( aPoint.y, me.GetBottom() ) );
550 
551  return wxPoint( nx, ny );
552 }
#define max(a, b)
Definition: auxiliary.h:86
Class EDA_RECT handles the component boundary box.
#define min(a, b)
Definition: auxiliary.h:85
EDA_RECT EDA_RECT::Common ( const EDA_RECT aRect) const

Function Common returns the area that is common with another rectangle.

Parameters
aRectis the rectangle to find the common area with.
Returns
The common area rect or 0-sized rectangle if there is no intersection.

Definition at line 715 of file base_struct.cpp.

References GetEnd(), GetOrigin(), Intersects(), max, min, SetEnd(), SetOrigin(), point_s::x, wxPoint::x, point_s::y, and wxPoint::y.

Referenced by calcCommonArea(), and DRAWSEGMENT::HitTest().

716 {
717  EDA_RECT r;
718 
719  if( Intersects( aRect ) )
720  {
721  wxPoint originA( std::min( GetOrigin().x, GetEnd().x ),
722  std::min( GetOrigin().y, GetEnd().y ) );
723  wxPoint originB( std::min( aRect.GetOrigin().x, aRect.GetEnd().x ),
724  std::min( aRect.GetOrigin().y, aRect.GetEnd().y ) );
725  wxPoint endA( std::max( GetOrigin().x, GetEnd().x ),
726  std::max( GetOrigin().y, GetEnd().y ) );
727  wxPoint endB( std::max( aRect.GetOrigin().x, aRect.GetEnd().x ),
728  std::max( aRect.GetOrigin().y, aRect.GetEnd().y ) );
729 
730  r.SetOrigin( wxPoint( std::max( originA.x, originB.x ), std::max( originA.y, originB.y ) ) );
731  r.SetEnd ( wxPoint( std::min( endA.x, endB.x ), std::min( endA.y, endB.y ) ) );
732  }
733 
734  return r;
735 }
void SetOrigin(const wxPoint &pos)
const wxPoint & GetOrigin() const
void SetEnd(int x, int y)
const wxPoint GetEnd() const
bool Intersects(const EDA_RECT &aRect) const
Function Intersects tests for a common area between rectangles.
#define max(a, b)
Definition: auxiliary.h:86
Class EDA_RECT handles the component boundary box.
#define min(a, b)
Definition: auxiliary.h:85
bool EDA_RECT::Contains ( const wxPoint aPoint) const

Function Contains.

Parameters
aPoint= the wxPoint to test
Returns
true if aPoint is inside the boundary box. A point on a edge is seen as inside

Definition at line 355 of file base_struct.cpp.

References m_Pos, m_Size, wxPoint::x, and wxPoint::y.

Referenced by PCB_EDIT_FRAME::AutoPlaceModule(), compute_Ratsnest_PlaceModule(), Contains(), GRPutPixel(), SCH_JUNCTION::HitTest(), PCB_TARGET::HitTest(), SCH_NO_CONNECT::HitTest(), LIB_PIN::HitTest(), SCH_BUS_ENTRY_BASE::HitTest(), SCH_BITMAP::HitTest(), SCH_LINE::HitTest(), DRAWSEGMENT::HitTest(), SCH_FIELD::HitTest(), SCH_SHEET_PIN::HitTest(), SCH_TEXT::HitTest(), DIMENSION::HitTest(), TRACK::HitTest(), GERBER_DRAW_ITEM::HitTest(), WS_DRAW_ITEM_BITMAP::HitTest(), ZONE_CONTAINER::HitTest(), MODULE::HitTest(), VIA::HitTest(), D_PAD::HitTest(), SCH_SHEET::HitTest(), SCH_COMPONENT::HitTest(), MARKER_BASE::HitTestMarker(), LIB_BEZIER::Inside(), LIB_CIRCLE::Inside(), LIB_RECTANGLE::Inside(), LIB_POLYLINE::Inside(), LIB_TEXT::Inside(), LIB_ARC::Inside(), LIB_FIELD::Inside(), LIB_PIN::Inside(), Intersects(), EDA_DRAW_PANEL::IsPointOnDisplay(), MarkItemsInBloc(), PCB_EDIT_FRAME::SpreadFootprints(), TEXTE_MODULE::TextHitTest(), and EDA_TEXT::TextHitTest().

356 {
357  wxPoint rel_pos = aPoint - m_Pos;
358  wxSize size = m_Size;
359 
360  if( size.x < 0 )
361  {
362  size.x = -size.x;
363  rel_pos.x += size.x;
364  }
365 
366  if( size.y < 0 )
367  {
368  size.y = -size.y;
369  rel_pos.y += size.y;
370  }
371 
372  return (rel_pos.x >= 0) && (rel_pos.y >= 0) && ( rel_pos.y <= size.y) && ( rel_pos.x <= size.x);
373 }
wxSize m_Size
wxPoint m_Pos
bool EDA_RECT::Contains ( int  x,
int  y 
) const
inline

Function Contains.

Parameters
x= the x coordinate of the point to test
y= the x coordinate of the point to test
Returns
true if point is inside the boundary box. A point on a edge is seen as inside

Definition at line 90 of file class_eda_rect.h.

References Contains().

Referenced by Contains().

90 { return Contains( wxPoint( x, y ) ); }
bool Contains(const wxPoint &aPoint) const
Function Contains.
bool EDA_RECT::Contains ( const EDA_RECT aRect) const

Function Contains.

Parameters
aRect= the EDA_RECT to test
Returns
true if aRect is Contained. A common edge is seen as contained

Definition at line 376 of file base_struct.cpp.

References Contains(), GetEnd(), and GetOrigin().

377 {
378  return Contains( aRect.GetOrigin() ) && Contains( aRect.GetEnd() );
379 }
bool Contains(const wxPoint &aPoint) const
Function Contains.
const wxPoint & GetOrigin() const
const wxPoint GetEnd() const
const wxPoint EDA_RECT::FarthestPointTo ( const wxPoint aPoint) const

Return the point in this rect that is farthest from the provided point.

Definition at line 555 of file base_struct.cpp.

References abs, GetBottom(), GetLeft(), GetRight(), GetTop(), max, Normalize(), wxPoint::x, and wxPoint::y.

Referenced by IntersectsCircleEdge().

556 {
557  EDA_RECT me( *this );
558 
559  me.Normalize(); // ensure size is >= 0
560 
561  int fx = std::max( std::abs( aPoint.x - me.GetLeft() ), std::abs( aPoint.x - me.GetRight() ) );
562  int fy = std::max( std::abs( aPoint.y - me.GetTop() ), std::abs( aPoint.y - me.GetBottom() ) );
563 
564  return wxPoint( fx, fy );
565 }
#define abs(a)
Definition: auxiliary.h:84
#define max(a, b)
Definition: auxiliary.h:86
Class EDA_RECT handles the component boundary box.
double EDA_RECT::GetArea ( ) const

Function GetArea returns the area of the rectangle.

Returns
The area of the rectangle.

Definition at line 709 of file base_struct.cpp.

References GetHeight(), and GetWidth().

Referenced by calcArea(), calcCommonArea(), SCH_COMPONENT::operator<(), and MODULE::PadCoverageRatio().

710 {
711  return (double) GetWidth() * (double) GetHeight();
712 }
int GetHeight() const
int GetWidth() const
const EDA_RECT EDA_RECT::GetBoundingBoxRotated ( wxPoint  aRotCenter,
double  aAngle 
)

Function GetBoundingBoxRotated.

Returns
the bounding box of this, after rotation
Parameters
aAngle= the rotation angle in 0.1 deg.
aRotCenter= the rotation point. useful to calculate bounding box of rotated items, when rotation if not k*90 degrees

Definition at line 738 of file base_struct.cpp.

References GetEnd(), GetOrigin(), max, min, RotatePoint(), SetEnd(), SetOrigin(), wxPoint::x, and wxPoint::y.

Referenced by TEXTE_PCB::GetBoundingBox(), TEXTE_MODULE::GetBoundingBox(), D_PAD::GetBoundingBox(), and TEXTE_MODULE::ViewBBox().

739 {
740  wxPoint corners[4];
741 
742  // Build the corners list
743  corners[0] = GetOrigin();
744  corners[2] = GetEnd();
745  corners[1].x = corners[0].x;
746  corners[1].y = corners[2].y;
747  corners[3].x = corners[2].x;
748  corners[3].y = corners[0].y;
749 
750  // Rotate all corners, to find the bounding box
751  for( int ii = 0; ii < 4; ii ++ )
752  RotatePoint( &corners[ii], aRotCenter, aAngle );
753 
754  // Find the corners bounding box
755  wxPoint start = corners[0];
756  wxPoint end = corners[0];
757 
758  for( int ii = 1; ii < 4; ii ++ )
759  {
760  start.x = std::min( start.x, corners[ii].x);
761  start.y = std::min( start.y, corners[ii].y);
762  end.x = std::max( end.x, corners[ii].x);
763  end.y = std::max( end.y, corners[ii].y);
764  }
765 
766  EDA_RECT bbox;
767  bbox.SetOrigin( start );
768  bbox.SetEnd( end );
769 
770  return bbox;
771 }
void SetOrigin(const wxPoint &pos)
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
const wxPoint & GetOrigin() const
void SetEnd(int x, int y)
const wxPoint GetEnd() const
#define max(a, b)
Definition: auxiliary.h:86
Class EDA_RECT handles the component boundary box.
#define min(a, b)
Definition: auxiliary.h:85
const wxPoint EDA_RECT::GetCenter ( ) const
inline

Definition at line 113 of file class_eda_rect.h.

References wxPoint::x, and wxPoint::y.

Referenced by getAnchorPoint(), and PCB_EDIT_FRAME::moveExact().

113 { return wxPoint( m_Pos.x + ( m_Size.x / 2 ), m_Pos.y + ( m_Size.y / 2 ) ); }
wxSize m_Size
wxPoint m_Pos
int EDA_RECT::GetLeft ( ) const
inline

Definition at line 118 of file class_eda_rect.h.

References wxPoint::x.

Referenced by ClosestPointTo(), FarthestPointTo(), AUTOPLACER::field_horiz_placement(), and D_PAD::HitTest().

118 { return m_Pos.x; }
wxPoint m_Pos
int EDA_RECT::GetSizeMax ( ) const
inline

GetSizeMax.

Returns
the max size dimension

Definition at line 105 of file class_eda_rect.h.

Referenced by CINFO3D_VISU::AddShapeWithClearanceToContainer(), and CINFO3D_VISU::transformGraphicModuleEdgeToPolygonSet().

105 { return ( m_Size.x > m_Size.y )?m_Size.x:m_Size.y; }
wxSize m_Size
int EDA_RECT::GetTop ( ) const
inline
int EDA_RECT::GetWidth ( void  ) const
inline
EDA_RECT & EDA_RECT::Inflate ( wxCoord  dx,
wxCoord  dy 
)

Function Inflate inflates the rectangle horizontally by dx and vertically by dy.

If dx and/or dy is negative the rectangle is deflated.

Definition at line 610 of file base_struct.cpp.

References m_Pos, m_Size, wxPoint::x, and wxPoint::y.

Referenced by BuildBoardPolygonOutlines(), ZONE_CONTAINER::buildFeatureHoleList(), BuildUnconnectedThermalStubsPolygonList(), FillNegativeKnockout(), genModuleOnRoutingMatrix(), SCH_JUNCTION::GetBoundingBox(), SCH_NO_CONNECT::GetBoundingBox(), LIB_CIRCLE::GetBoundingBox(), LIB_RECTANGLE::GetBoundingBox(), LIB_BEZIER::GetBoundingBox(), LIB_POLYLINE::GetBoundingBox(), SCH_BUS_ENTRY_BASE::GetBoundingBox(), LIB_ARC::GetBoundingBox(), MODULE::GetBoundingBox(), LIB_PIN::GetBoundingBox(), BITMAP_BASE::GetBoundingBox(), DRAWSEGMENT::GetBoundingBox(), GERBER_DRAW_ITEM::GetBoundingBox(), SCH_SHEET::GetBoundingBox(), D_PAD::GetBoundingBox(), MODULE::GetFootprintRect(), GRCSegm(), GRLineArray(), GRSFilledRect(), SCH_JUNCTION::HitTest(), PCB_TARGET::HitTest(), SCH_NO_CONNECT::HitTest(), LIB_PIN::HitTest(), SCH_BUS_ENTRY_BASE::HitTest(), SCH_BITMAP::HitTest(), SCH_LINE::HitTest(), DRAWSEGMENT::HitTest(), SCH_FIELD::HitTest(), SCH_SHEET_PIN::HitTest(), SCH_TEXT::HitTest(), DIMENSION::HitTest(), TRACK::HitTest(), ZONE_CONTAINER::HitTest(), MODULE::HitTest(), VIA::HitTest(), D_PAD::HitTest(), SCH_SHEET::HitTest(), SCH_COMPONENT::HitTest(), Inflate(), CINFO3D_VISU::InitSettings(), Intersects(), EDA_DRAW_PANEL::IsPointOnDisplay(), moveBitmap(), SCH_EDIT_FRAME::OnDragItem(), SCH_SCREEN::SelectBlockItems(), PCB_EDIT_FRAME::SpreadFootprints(), TEXTE_MODULE::TextHitTest(), EDA_TEXT::TextHitTest(), TEXTE_PCB::TransformBoundingBoxWithClearanceToPolygon(), TstModuleOnBoard(), TstRectangle(), and WinClipAndDrawLine().

611 {
612  if( m_Size.x >= 0 )
613  {
614  if( m_Size.x < -2 * dx )
615  {
616  // Don't allow deflate to eat more width than we have,
617  m_Pos.x += m_Size.x / 2;
618  m_Size.x = 0;
619  }
620  else
621  {
622  // The inflate is valid.
623  m_Pos.x -= dx;
624  m_Size.x += 2 * dx;
625  }
626  }
627  else // size.x < 0:
628  {
629  if( m_Size.x > -2 * dx )
630  {
631  // Don't allow deflate to eat more width than we have,
632  m_Pos.x -= m_Size.x / 2;
633  m_Size.x = 0;
634  }
635  else
636  {
637  // The inflate is valid.
638  m_Pos.x += dx;
639  m_Size.x -= 2 * dx; // m_Size.x <0: inflate when dx > 0
640  }
641  }
642 
643  if( m_Size.y >= 0 )
644  {
645  if( m_Size.y < -2 * dy )
646  {
647  // Don't allow deflate to eat more height than we have,
648  m_Pos.y += m_Size.y / 2;
649  m_Size.y = 0;
650  }
651  else
652  {
653  // The inflate is valid.
654  m_Pos.y -= dy;
655  m_Size.y += 2 * dy;
656  }
657  }
658  else // size.y < 0:
659  {
660  if( m_Size.y > 2 * dy )
661  {
662  // Don't allow deflate to eat more height than we have,
663  m_Pos.y -= m_Size.y / 2;
664  m_Size.y = 0;
665  }
666  else
667  {
668  // The inflate is valid.
669  m_Pos.y += dy;
670  m_Size.y -= 2 * dy; // m_Size.y <0: inflate when dy > 0
671  }
672  }
673 
674  return *this;
675 }
wxSize m_Size
wxPoint m_Pos
EDA_RECT & EDA_RECT::Inflate ( int  aDelta)

Function Inflate inflates the rectangle horizontally and vertically by aDelta.

If aDelta is negative the rectangle is deflated.

Definition at line 603 of file base_struct.cpp.

References Inflate().

604 {
605  Inflate( aDelta, aDelta );
606  return *this;
607 }
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
bool EDA_RECT::Intersects ( const EDA_RECT aRect) const

Function Intersects tests for a common area between rectangles.

Parameters
aRectA rectangle to test intersection with.
Returns
bool - true if the argument rectangle intersects this rectangle. (i.e. if the 2 rectangles have at least a common point)

Definition at line 408 of file base_struct.cpp.

References m_Pos, m_Size, max, min, Normalize(), wxPoint::x, and wxPoint::y.

Referenced by ZONE_CONTAINER::buildFeatureHoleList(), BuildUnconnectedThermalStubsPolygonList(), Common(), AUTOPLACER::filtered_colliders(), PCB_TARGET::HitTest(), SCH_JUNCTION::HitTest(), SCH_NO_CONNECT::HitTest(), SCH_BUS_ENTRY_BASE::HitTest(), SCH_BITMAP::HitTest(), SCH_LINE::HitTest(), WS_DRAW_ITEM_LINE::HitTest(), DRAWSEGMENT::HitTest(), SCH_FIELD::HitTest(), SCH_TEXT::HitTest(), DIMENSION::HitTest(), TRACK::HitTest(), WS_DRAW_ITEM_POLYGON::HitTest(), WS_DRAW_ITEM_RECT::HitTest(), WS_DRAW_ITEM_BITMAP::HitTest(), ZONE_CONTAINER::HitTest(), MODULE::HitTest(), D_PAD::HitTest(), SCH_SHEET::HitTest(), SCH_COMPONENT::HitTest(), Intersects(), TEXTE_MODULE::TextHitTest(), and EDA_TEXT::TextHitTest().

409 {
410  // this logic taken from wxWidgets' geometry.cpp file:
411  bool rc;
412  EDA_RECT me(*this);
413  EDA_RECT rect(aRect);
414  me.Normalize(); // ensure size is >= 0
415  rect.Normalize(); // ensure size is >= 0
416 
417  // calculate the left common area coordinate:
418  int left = std::max( me.m_Pos.x, rect.m_Pos.x );
419  // calculate the right common area coordinate:
420  int right = std::min( me.m_Pos.x + me.m_Size.x, rect.m_Pos.x + rect.m_Size.x );
421  // calculate the upper common area coordinate:
422  int top = std::max( me.m_Pos.y, aRect.m_Pos.y );
423  // calculate the lower common area coordinate:
424  int bottom = std::min( me.m_Pos.y + me.m_Size.y, rect.m_Pos.y + rect.m_Size.y );
425 
426  // if a common area exists, it must have a positive (null accepted) size
427  if( left <= right && top <= bottom )
428  rc = true;
429  else
430  rc = false;
431 
432  return rc;
433 }
#define max(a, b)
Definition: auxiliary.h:86
Class EDA_RECT handles the component boundary box.
wxPoint m_Pos
#define min(a, b)
Definition: auxiliary.h:85
bool EDA_RECT::Intersects ( const EDA_RECT aRect,
double  aRot 
) const

Tests for a common area between this rectangle, and a rectangle with arbitrary rotation.

Parameters
aRecta rectangle to test intersection with
aRotrectangle rotation (in 1/10 degrees)

Definition at line 436 of file base_struct.cpp.

References Centre(), Contains(), delta, GetHeight(), GetWidth(), Inflate(), Intersects(), m_Pos, m_Size, RotatePoint(), and SetOrigin().

437 {
438  /* Most rectangles will be axis aligned.
439  * It is quicker to check for this case and pass the rect
440  * to the simpler intersection test
441  */
442 
443  // Prevent floating point comparison errors
444  static const double ROT_EPS = 0.000000001;
445 
446  static const double ROT_PARALLEL[] = { -3600, -1800, 0, 1800, 3600 };
447  static const double ROT_PERPENDICULAR[] = { -2700, -900, 0, 900, 2700 };
448 
449  NORMALIZE_ANGLE_POS<double>( aRot );
450 
451  // Test for non-rotated rectangle
452  for( int ii = 0; ii < 5; ii++ )
453  {
454  if( std::fabs( aRot - ROT_PARALLEL[ii] ) < ROT_EPS )
455  {
456  return Intersects( aRect );
457  }
458  }
459 
460  // Test for rectangle rotated by multiple of 90 degrees
461  for( int jj = 0; jj < 4; jj++ )
462  {
463  if( std::fabs( aRot - ROT_PERPENDICULAR[jj] ) < ROT_EPS )
464  {
465  EDA_RECT rotRect;
466 
467  // Rotate the supplied rect by 90 degrees
468  rotRect.SetOrigin( aRect.Centre() );
469  rotRect.Inflate( aRect.GetHeight(), aRect.GetWidth() );
470  return Intersects( rotRect );
471  }
472  }
473 
474  /* There is some non-orthogonal rotation.
475  * There are three cases to test:
476  * A) One point of this rect is inside the rotated rect
477  * B) One point of the rotated rect is inside this rect
478  * C) One of the sides of the rotated rect intersect this
479  */
480 
481  wxPoint corners[4];
482 
483  /* Test A : Any corners exist in rotated rect? */
484 
485  corners[0] = m_Pos;
486  corners[1] = m_Pos + wxPoint( m_Size.x, 0 );
487  corners[2] = m_Pos + wxPoint( m_Size.x, m_Size.y );
488  corners[3] = m_Pos + wxPoint( 0, m_Size.y );
489 
490  wxPoint rCentre = aRect.Centre();
491 
492  for( int i = 0; i < 4; i++ )
493  {
494  wxPoint delta = corners[i] - rCentre;
495  RotatePoint( &delta, -aRot );
496  delta += rCentre;
497 
498  if( aRect.Contains( delta ) )
499  {
500  return true;
501  }
502  }
503 
504  /* Test B : Any corners of rotated rect exist in this one? */
505  int w = aRect.GetWidth() / 2;
506  int h = aRect.GetHeight() / 2;
507 
508  // Construct corners around center of shape
509  corners[0] = wxPoint( -w, -h );
510  corners[1] = wxPoint( w, -h );
511  corners[2] = wxPoint( w, h );
512  corners[3] = wxPoint( -w, h );
513 
514  // Rotate and test each corner
515  for( int j=0; j<4; j++ )
516  {
517  RotatePoint( &corners[j], aRot );
518  corners[j] += rCentre;
519 
520  if( Contains( corners[j] ) )
521  {
522  return true;
523  }
524  }
525 
526  /* Test C : Any sides of rotated rect intersect this */
527 
528  if( Intersects( corners[0], corners[1] ) ||
529  Intersects( corners[1], corners[2] ) ||
530  Intersects( corners[2], corners[3] ) ||
531  Intersects( corners[3], corners[0] ) )
532  {
533  return true;
534  }
535 
536 
537  return false;
538 }
bool Contains(const wxPoint &aPoint) const
Function Contains.
int GetHeight() const
void SetOrigin(const wxPoint &pos)
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
static const int delta[8][2]
Definition: solve.cpp:112
wxSize m_Size
wxPoint Centre() const
bool Intersects(const EDA_RECT &aRect) const
Function Intersects tests for a common area between rectangles.
Class EDA_RECT handles the component boundary box.
wxPoint m_Pos
int GetWidth() const
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
bool EDA_RECT::Intersects ( const wxPoint aPoint1,
const wxPoint aPoint2 
) const

Function Intersects tests for a common area between a segment and this rectangle.

Parameters
aPoint1First point of the segment to test intersection with.
aPoint2Second point of the segment to test intersection with.
Returns
bool - true if the argument segment intersects this rectangle. (i.e. if the segment and rectangle have at least a common point)

Definition at line 382 of file base_struct.cpp.

References Contains(), GetEnd(), GetOrigin(), SegmentIntersectsSegment(), wxPoint::x, and wxPoint::y.

383 {
384  wxPoint point2, point4;
385 
386  if( Contains( aPoint1 ) || Contains( aPoint2 ) )
387  return true;
388 
389  point2.x = GetEnd().x;
390  point2.y = GetOrigin().y;
391  point4.x = GetOrigin().x;
392  point4.y = GetEnd().y;
393 
394  //Only need to test 3 sides since a straight line cant enter and exit on same side
395  if( SegmentIntersectsSegment( aPoint1, aPoint2, GetOrigin() , point2 ) )
396  return true;
397 
398  if( SegmentIntersectsSegment( aPoint1, aPoint2, point2 , GetEnd() ) )
399  return true;
400 
401  if( SegmentIntersectsSegment( aPoint1, aPoint2, GetEnd() , point4 ) )
402  return true;
403 
404  return false;
405 }
bool Contains(const wxPoint &aPoint) const
Function Contains.
const wxPoint & GetOrigin() const
bool SegmentIntersectsSegment(const wxPoint &a_p1_l1, const wxPoint &a_p2_l1, const wxPoint &a_p1_l2, const wxPoint &a_p2_l2)
Function SegmentIntersectsSegment.
Definition: trigo.cpp:58
const wxPoint GetEnd() const
bool EDA_RECT::IntersectsCircle ( const wxPoint aCenter,
const int  aRadius 
) const

Function IntersectsCircle tests for a common area between a circle and this rectangle.

Parameters
aCentercenter of the circle
aRadiusradius of the circle

Definition at line 568 of file base_struct.cpp.

References ClosestPointTo(), wxPoint::x, and wxPoint::y.

Referenced by VIA::HitTest(), D_PAD::HitTest(), and IntersectsCircleEdge().

569 {
570  wxPoint closest = ClosestPointTo( aCenter );
571 
572  double dx = aCenter.x - closest.x;
573  double dy = aCenter.y - closest.y;
574 
575  double r = (double) aRadius;
576 
577  return ( dx * dx + dy * dy ) <= ( r * r );
578 }
const wxPoint ClosestPointTo(const wxPoint &aPoint) const
Return the point in this rect that is closest to the provided point.
bool EDA_RECT::IntersectsCircleEdge ( const wxPoint aCenter,
const int  aRadius,
const int  aWidth 
) const

IntersectsCircleEdge Tests for intersection between this rect and the edge (radius) of a circle.

Parameters
aCentercenter of the circle
aRadiusradius of the circle
aWidthwidth of the circle edge

Definition at line 581 of file base_struct.cpp.

References FarthestPointTo(), IntersectsCircle(), Normalize(), wxPoint::x, and wxPoint::y.

Referenced by DRAWSEGMENT::HitTest().

582 {
583  EDA_RECT me( *this );
584  me.Normalize(); // ensure size is >= 0
585 
586  // Test if the circle intersects at all
587  if( !IntersectsCircle( aCenter, aRadius + aWidth / 2 ) )
588  {
589  return false;
590  }
591 
592  wxPoint farpt = FarthestPointTo( aCenter );
593  // Farthest point must be further than the inside of the line
594  double fx = (double) farpt.x;
595  double fy = (double) farpt.y;
596 
597  double r = (double) aRadius - (double) aWidth / 2;
598 
599  return ( fx * fx + fy * fy ) > ( r * r );
600 }
bool IntersectsCircle(const wxPoint &aCenter, const int aRadius) const
Function IntersectsCircle tests for a common area between a circle and this rectangle.
Class EDA_RECT handles the component boundary box.
const wxPoint FarthestPointTo(const wxPoint &aPoint) const
Return the point in this rect that is farthest from the provided point.
void EDA_RECT::Merge ( const EDA_RECT aRect)

Function Merge modifies the position and size of the rectangle in order to contain aRect.

It is mainly used to calculate bounding boxes.

Parameters
aRectThe rectangle to merge with this rectangle.

Definition at line 678 of file base_struct.cpp.

References GetEnd(), m_Pos, max, min, Normalize(), SetEnd(), wxPoint::x, and wxPoint::y.

Referenced by PCB_EDIT_FRAME::AppendBoardFile(), DRAWSEGMENT::computeArcBBox(), GBR_LAYOUT::ComputeBoundingBox(), BOARD::ComputeBoundingBox(), LIB_PART::GetBodyBoundingBox(), SELECTION::GetBoundingBox(), MODULE::GetBoundingBox(), SCH_COMPONENT::GetBoundingBox(), D_PAD::GetBoundingBox(), MODULE::GetFootprintRect(), LIB_PART::GetUnitBoundingBox(), and SELECTION::ViewBBox().

679 {
680  Normalize(); // ensure width and height >= 0
681  EDA_RECT rect = aRect;
682  rect.Normalize(); // ensure width and height >= 0
683  wxPoint end = GetEnd();
684  wxPoint rect_end = rect.GetEnd();
685 
686  // Change origin and size in order to contain the given rect
687  m_Pos.x = std::min( m_Pos.x, rect.m_Pos.x );
688  m_Pos.y = std::min( m_Pos.y, rect.m_Pos.y );
689  end.x = std::max( end.x, rect_end.x );
690  end.y = std::max( end.y, rect_end.y );
691  SetEnd( end );
692 }
void SetEnd(int x, int y)
const wxPoint GetEnd() const
void Normalize()
Function Normalize ensures that the height ant width are positive.
#define max(a, b)
Definition: auxiliary.h:86
Class EDA_RECT handles the component boundary box.
wxPoint m_Pos
#define min(a, b)
Definition: auxiliary.h:85
void EDA_RECT::Merge ( const wxPoint aPoint)

Function Merge modifies the position and size of the rectangle in order to contain the given point.

Parameters
aPointThe point to merge with the rectangle.

Definition at line 695 of file base_struct.cpp.

References GetEnd(), m_Pos, max, min, Normalize(), SetEnd(), wxPoint::x, and wxPoint::y.

696 {
697  Normalize(); // ensure width and height >= 0
698 
699  wxPoint end = GetEnd();
700  // Change origin and size in order to contain the given rect
701  m_Pos.x = std::min( m_Pos.x, aPoint.x );
702  m_Pos.y = std::min( m_Pos.y, aPoint.y );
703  end.x = std::max( end.x, aPoint.x );
704  end.y = std::max( end.y, aPoint.y );
705  SetEnd( end );
706 }
void SetEnd(int x, int y)
const wxPoint GetEnd() const
void Normalize()
Function Normalize ensures that the height ant width are positive.
#define max(a, b)
Definition: auxiliary.h:86
wxPoint m_Pos
#define min(a, b)
Definition: auxiliary.h:85
void EDA_RECT::Move ( const wxPoint aMoveVector)
void EDA_RECT::Offset ( int  dx,
int  dy 
)
inline

Definition at line 126 of file class_eda_rect.h.

References wxPoint::x, and wxPoint::y.

Referenced by SCH_COMPONENT::GetBodyBoundingBox().

126 { m_Pos.x += dx; m_Pos.y += dy; }
wxPoint m_Pos
void EDA_RECT::Offset ( const wxPoint offset)
inline

Definition at line 127 of file class_eda_rect.h.

References wxPoint::x, and wxPoint::y.

127  { m_Pos.x += offset.x; m_Pos.y +=
128  offset.y; }
wxPoint m_Pos
EDA_RECT::operator BOX2I ( ) const
inline

Function operator(BOX2I) overloads the cast operator to return a BOX2I.

Returns
BOX2I - this box shaped as a BOX2I object.

Definition at line 227 of file class_eda_rect.h.

References GetEnd(), GetPosition(), and Normalize().

228  {
229  EDA_RECT rect( m_Pos, m_Size );
230  rect.Normalize();
231  return BOX2I( rect.GetPosition(), rect.GetEnd() );
232  }
BOX2< VECTOR2I > BOX2I
Definition: box2.h:468
wxSize m_Size
Class EDA_RECT handles the component boundary box.
wxPoint m_Pos
EDA_RECT::operator wxRect ( ) const
inline

Function operator(wxRect) overloads the cast operator to return a wxRect wxRect does not accept negative values for size, so ensure the wxRect size is always >= 0.

Definition at line 215 of file class_eda_rect.h.

References m_Pos, m_Size, and Normalize().

216  {
217  EDA_RECT rect( m_Pos, m_Size );
218  rect.Normalize();
219  return wxRect( rect.m_Pos, rect.m_Size );
220  }
wxSize m_Size
Class EDA_RECT handles the component boundary box.
wxPoint m_Pos
void EDA_RECT::RevertYAxis ( )
inline
void EDA_RECT::SetEnd ( const wxPoint pos)
inline

Definition at line 134 of file class_eda_rect.h.

References wxPoint::x, and wxPoint::y.

135  {
136  m_Size.x = pos.x - m_Pos.x; m_Size.y = pos.y - m_Pos.y;
137  }
wxSize m_Size
wxPoint m_Pos
void EDA_RECT::SetHeight ( int  val)
inline
void EDA_RECT::SetOrigin ( int  x,
int  y 
)
inline

Definition at line 123 of file class_eda_rect.h.

References wxPoint::x, and wxPoint::y.

123 { m_Pos.x = x; m_Pos.y = y; }
wxPoint m_Pos
void EDA_RECT::SetSize ( int  w,
int  h 
)
inline

Definition at line 125 of file class_eda_rect.h.

125 { m_Size.x = w; m_Size.y = h; }
wxSize m_Size
void EDA_RECT::SetWidth ( int  val)
inline

Member Data Documentation

wxPoint EDA_RECT::m_Pos
private
wxSize EDA_RECT::m_Size
private

Definition at line 48 of file class_eda_rect.h.

Referenced by Contains(), GetSize(), Inflate(), Intersects(), Normalize(), and operator wxRect().


The documentation for this class was generated from the following files: