KiCad PCB EDA Suite
class_eda_rect.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2004-2014 KiCad Developers, see change_log.txt for contributors.
6  * Copyright (C) 2017 KiCad Developers, see CHANGELOG.TXT for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
30 #ifndef CLASS_EDA_RECT_H
31 #define CLASS_EDA_RECT_H
32 
33 #include <wx/gdicmn.h>
34 #include <math/box2.h>
35 
44 class EDA_RECT
45 {
46 private:
47  wxPoint m_Pos; // Rectangle Origin
48  wxSize m_Size; // Rectangle Size
49 
50 public:
51  EDA_RECT() { };
52 
53  EDA_RECT( const wxPoint& aPos, const wxSize& aSize ) :
54  m_Pos( aPos ),
55  m_Size( aSize )
56  { }
57 
58  wxPoint Centre() const
59  {
60  return wxPoint( m_Pos.x + ( m_Size.x >> 1 ),
61  m_Pos.y + ( m_Size.y >> 1 ) );
62  }
63 
69  void Move( const wxPoint& aMoveVector );
70 
75  void Normalize();
76 
82  bool Contains( const wxPoint& aPoint ) const;
83 
90  bool Contains( int x, int y ) const { return Contains( wxPoint( x, y ) ); }
91 
97  bool Contains( const EDA_RECT& aRect ) const;
98 
99  const wxSize& GetSize() const { return m_Size; }
100 
105  int GetSizeMax() const { return ( m_Size.x > m_Size.y )?m_Size.x:m_Size.y; }
106 
107  int GetX() const { return m_Pos.x; }
108  int GetY() const { return m_Pos.y; }
109 
110  const wxPoint& GetOrigin() const { return m_Pos; }
111  const wxPoint& GetPosition() const { return m_Pos; }
112  const wxPoint GetEnd() const { return wxPoint( m_Pos.x + m_Size.x, m_Pos.y + m_Size.y ); }
113  const wxPoint GetCenter() const { return wxPoint( m_Pos.x + ( m_Size.x / 2 ), m_Pos.y + ( m_Size.y / 2 ) ); }
114 
115  int GetWidth() const { return m_Size.x; }
116  int GetHeight() const { return m_Size.y; }
117  int GetRight() const { return m_Pos.x + m_Size.x; }
118  int GetLeft() const { return m_Pos.x; }
119  int GetTop() const { return m_Pos.y; }
120  int GetBottom() const { return m_Pos.y + m_Size.y; } // Y axis from top to bottom
121 
122  void SetOrigin( const wxPoint& pos ) { m_Pos = pos; }
123  void SetOrigin( int x, int y ) { m_Pos.x = x; m_Pos.y = y; }
124  void SetSize( const wxSize& size ) { m_Size = size; }
125  void SetSize( int w, int h ) { m_Size.x = w; m_Size.y = h; }
126  void Offset( int dx, int dy ) { m_Pos.x += dx; m_Pos.y += dy; }
127  void Offset( const wxPoint& offset ) { m_Pos.x += offset.x; m_Pos.y +=
128  offset.y; }
129  void SetX( int val ) { m_Pos.x = val; }
130  void SetY( int val ) { m_Pos.y = val; }
131  void SetWidth( int val ) { m_Size.x = val; }
132  void SetHeight( int val ) { m_Size.y = val; }
133  void SetEnd( int x, int y ) { SetEnd( wxPoint( x, y ) ); }
134  void SetEnd( const wxPoint& pos )
135  {
136  m_Size.x = pos.x - m_Pos.x; m_Size.y = pos.y - m_Pos.y;
137  }
138 
143  void RevertYAxis()
144  {
145  m_Pos.y = -m_Pos.y;
146  m_Size.y = -m_Size.y;
147  Normalize();
148  }
149 
158  bool Intersects( const EDA_RECT& aRect ) const;
159 
167  bool Intersects( const EDA_RECT& aRect, double aRot ) const;
168 
178  bool Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2 ) const;
179 
183  const wxPoint ClosestPointTo( const wxPoint& aPoint ) const;
184 
188  const wxPoint FarthestPointTo( const wxPoint& aPoint ) const;
189 
197  bool IntersectsCircle( const wxPoint& aCenter, const int aRadius ) const;
198 
207  bool IntersectsCircleEdge( const wxPoint& aCenter, const int aRadius, const int aWidth ) const;
208 
215  operator wxRect() const
216  {
217  EDA_RECT rect( m_Pos, m_Size );
218  rect.Normalize();
219  return wxRect( rect.m_Pos, rect.m_Size );
220  }
221 
227  operator BOX2I() const
228  {
229  EDA_RECT rect( m_Pos, m_Size );
230  rect.Normalize();
231  return BOX2I( rect.GetPosition(), rect.GetEnd() );
232  }
233 
239  EDA_RECT& Inflate( wxCoord dx, wxCoord dy );
240 
246  EDA_RECT& Inflate( int aDelta );
247 
254  void Merge( const EDA_RECT& aRect );
255 
261  void Merge( const wxPoint& aPoint );
262 
268  double GetArea() const;
269 
276  EDA_RECT Common( const EDA_RECT& aRect ) const;
277 
286  const EDA_RECT GetBoundingBoxRotated( wxPoint aRotCenter, double aAngle );
287 };
288 
289 
290 #endif // CLASS_EDA_RECT_H
void Offset(int dx, int dy)
BOX2< VECTOR2I > BOX2I
Definition: box2.h:468
void Move(const wxPoint &aMoveVector)
Function Move moves the rectangle by the aMoveVector.
void Merge(const EDA_RECT &aRect)
Function Merge modifies the position and size of the rectangle in order to contain aRect...
int GetLeft() const
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...
bool Contains(const wxPoint &aPoint) const
Function Contains.
bool IntersectsCircle(const wxPoint &aCenter, const int aRadius) const
Function IntersectsCircle tests for a common area between a circle and this rectangle.
EDA_RECT Common(const EDA_RECT &aRect) const
Function Common returns the area that is common with another rectangle.
int GetHeight() const
bool Contains(int x, int y) const
Function Contains.
void SetOrigin(const wxPoint &pos)
const wxPoint ClosestPointTo(const wxPoint &aPoint) const
Return the point in this rect that is closest to the provided point.
void RevertYAxis()
Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size) ...
void SetSize(int w, int h)
const wxPoint & GetOrigin() const
EDA_RECT(const wxPoint &aPos, const wxSize &aSize)
wxSize m_Size
double GetArea() const
Function GetArea returns the area of the rectangle.
void SetHeight(int val)
void SetEnd(int x, int y)
const EDA_RECT GetBoundingBoxRotated(wxPoint aRotCenter, double aAngle)
Function GetBoundingBoxRotated.
int GetSizeMax() const
GetSizeMax.
void SetX(int val)
int GetBottom() const
wxPoint Centre() const
int GetRight() const
void SetWidth(int val)
const wxPoint & GetPosition() const
void SetY(int val)
const wxPoint GetEnd() const
void Normalize()
Function Normalize ensures that the height ant width are positive.
void SetSize(const wxSize &size)
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 GetX() const
int GetWidth() const
void SetEnd(const wxPoint &pos)
int GetY() const
void SetOrigin(int x, int y)
const wxSize & GetSize() const
void Offset(const wxPoint &offset)
const wxPoint FarthestPointTo(const wxPoint &aPoint) const
Return the point in this rect that is farthest from the provided point.
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
const wxPoint GetCenter() const
int GetTop() const