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  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
29 #ifndef CLASS_EDA_RECT_H
30 #define CLASS_EDA_RECT_H
31 
32 #include <wx/gdicmn.h>
33 #include <math/box2.h>
34 
43 class EDA_RECT
44 {
45 private:
46  wxPoint m_Pos; // Rectangle Origin
47  wxSize m_Size; // Rectangle Size
48 
49 public:
50  EDA_RECT() { };
51 
52  EDA_RECT( const wxPoint& aPos, const wxSize& aSize ) :
53  m_Pos( aPos ),
54  m_Size( aSize )
55  { }
56 
57  wxPoint Centre() const
58  {
59  return wxPoint( m_Pos.x + ( m_Size.x >> 1 ),
60  m_Pos.y + ( m_Size.y >> 1 ) );
61  }
62 
68  void Move( const wxPoint& aMoveVector );
69 
74  void Normalize();
75 
81  bool Contains( const wxPoint& aPoint ) const;
82 
89  bool Contains( int x, int y ) const { return Contains( wxPoint( x, y ) ); }
90 
96  bool Contains( const EDA_RECT& aRect ) const;
97 
98  const wxSize& GetSize() const { return m_Size; }
99 
104  int GetSizeMax() const { return ( m_Size.x > m_Size.y )?m_Size.x:m_Size.y; }
105 
106  int GetX() const { return m_Pos.x; }
107  int GetY() const { return m_Pos.y; }
108 
109  const wxPoint& GetOrigin() const { return m_Pos; }
110  const wxPoint& GetPosition() const { return m_Pos; }
111  const wxPoint GetEnd() const { return wxPoint( m_Pos.x + m_Size.x, m_Pos.y + m_Size.y ); }
112 
113  int GetWidth() const { return m_Size.x; }
114  int GetHeight() const { return m_Size.y; }
115  int GetRight() const { return m_Pos.x + m_Size.x; }
116  int GetLeft() const { return m_Pos.x; }
117  int GetTop() const { return m_Pos.y; }
118  int GetBottom() const { return m_Pos.y + m_Size.y; } // Y axis from top to bottom
119 
120  void SetOrigin( const wxPoint& pos ) { m_Pos = pos; }
121  void SetOrigin( int x, int y ) { m_Pos.x = x; m_Pos.y = y; }
122  void SetSize( const wxSize& size ) { m_Size = size; }
123  void SetSize( int w, int h ) { m_Size.x = w; m_Size.y = h; }
124  void Offset( int dx, int dy ) { m_Pos.x += dx; m_Pos.y += dy; }
125  void Offset( const wxPoint& offset ) { m_Pos.x += offset.x; m_Pos.y +=
126  offset.y; }
127  void SetX( int val ) { m_Pos.x = val; }
128  void SetY( int val ) { m_Pos.y = val; }
129  void SetWidth( int val ) { m_Size.x = val; }
130  void SetHeight( int val ) { m_Size.y = val; }
131  void SetEnd( int x, int y ) { SetEnd( wxPoint( x, y ) ); }
132  void SetEnd( const wxPoint& pos )
133  {
134  m_Size.x = pos.x - m_Pos.x; m_Size.y = pos.y - m_Pos.y;
135  }
136 
141  void RevertYAxis()
142  {
143  m_Pos.y = -m_Pos.y;
144  m_Size.y = -m_Size.y;
145  Normalize();
146  }
147 
156  bool Intersects( const EDA_RECT& aRect ) const;
157 
167  bool Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2 ) const;
168 
175  operator wxRect() const
176  {
177  EDA_RECT rect( m_Pos, m_Size );
178  rect.Normalize();
179  return wxRect( rect.m_Pos, rect.m_Size );
180  }
181 
187  operator BOX2I() const
188  {
189  EDA_RECT rect( m_Pos, m_Size );
190  rect.Normalize();
191  return BOX2I( rect.GetPosition(), rect.GetEnd() );
192  }
193 
199  EDA_RECT& Inflate( wxCoord dx, wxCoord dy );
200 
206  EDA_RECT& Inflate( int aDelta );
207 
214  void Merge( const EDA_RECT& aRect );
215 
221  void Merge( const wxPoint& aPoint );
222 
228  double GetArea() const;
229 
236  EDA_RECT Common( const EDA_RECT& aRect ) const;
237 
246  const EDA_RECT GetBoundingBoxRotated( wxPoint aRotCenter, double aAngle );
247 };
248 
249 
250 #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 Contains(const wxPoint &aPoint) const
Function Contains.
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)
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)
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
int GetTop() const