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 
42 class EDA_RECT
43 {
44 private:
45  wxPoint m_Pos; // Rectangle Origin
46  wxSize m_Size; // Rectangle Size
47 
48 public:
49  EDA_RECT() { };
50 
51  EDA_RECT( const wxPoint& aPos, const wxSize& aSize ) :
52  m_Pos( aPos ),
53  m_Size( aSize )
54  { }
55 
56  wxPoint Centre() const
57  {
58  return wxPoint( m_Pos.x + ( m_Size.x >> 1 ),
59  m_Pos.y + ( m_Size.y >> 1 ) );
60  }
61 
67  void Move( const wxPoint& aMoveVector );
68 
73  void Normalize();
74 
80  bool Contains( const wxPoint& aPoint ) const;
87  bool Contains( int x, int y ) const { return Contains( wxPoint( x, y ) ); }
88 
94  bool Contains( const EDA_RECT& aRect ) const;
95 
96  const wxSize& GetSize() const { return m_Size; }
97 
102  int GetSizeMax() const { return ( m_Size.x > m_Size.y )?m_Size.x:m_Size.y; }
103 
104  int GetX() const { return m_Pos.x; }
105  int GetY() const { return m_Pos.y; }
106 
107  const wxPoint& GetOrigin() const { return m_Pos; }
108  const wxPoint& GetPosition() const { return m_Pos; }
109  const wxPoint GetEnd() const { return wxPoint( m_Pos.x + m_Size.x, m_Pos.y + m_Size.y ); }
110 
111  int GetWidth() const { return m_Size.x; }
112  int GetHeight() const { return m_Size.y; }
113  int GetRight() const { return m_Pos.x + m_Size.x; }
114  int GetLeft() const { return m_Pos.x; }
115  int GetTop() const { return m_Pos.y; }
116  int GetBottom() const { return m_Pos.y + m_Size.y; } // Y axis from top to bottom
117 
118  void SetOrigin( const wxPoint& pos ) { m_Pos = pos; }
119  void SetOrigin( int x, int y ) { m_Pos.x = x; m_Pos.y = y; }
120  void SetSize( const wxSize& size ) { m_Size = size; }
121  void SetSize( int w, int h ) { m_Size.x = w; m_Size.y = h; }
122  void Offset( int dx, int dy ) { m_Pos.x += dx; m_Pos.y += dy; }
123  void Offset( const wxPoint& offset ) { m_Pos.x += offset.x; m_Pos.y +=
124  offset.y; }
125  void SetX( int val ) { m_Pos.x = val; }
126  void SetY( int val ) { m_Pos.y = val; }
127  void SetWidth( int val ) { m_Size.x = val; }
128  void SetHeight( int val ) { m_Size.y = val; }
129  void SetEnd( int x, int y ) { SetEnd( wxPoint( x, y ) ); }
130  void SetEnd( const wxPoint& pos )
131  {
132  m_Size.x = pos.x - m_Pos.x; m_Size.y = pos.y - m_Pos.y;
133  }
134 
139  void RevertYAxis()
140  {
141  m_Pos.y = -m_Pos.y;
142  m_Size.y = -m_Size.y;
143  Normalize();
144  }
145 
154  bool Intersects( const EDA_RECT& aRect ) const;
155 
165  bool Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2 ) const;
166 
173  operator wxRect() const
174  {
175  EDA_RECT rect( m_Pos, m_Size );
176  rect.Normalize();
177  return wxRect( rect.m_Pos, rect.m_Size );
178  }
179 
185  EDA_RECT& Inflate( wxCoord dx, wxCoord dy );
186 
192  EDA_RECT& Inflate( int aDelta );
193 
200  void Merge( const EDA_RECT& aRect );
201 
207  void Merge( const wxPoint& aPoint );
208 
214  double GetArea() const;
215 
222  EDA_RECT Common( const EDA_RECT& aRect ) const;
223 
232  const EDA_RECT GetBoundingBoxRotated( wxPoint aRotCenter, double aAngle );
233 };
234 
235 
236 #endif // CLASS_EDA_RECT_H
void Offset(int dx, int dy)
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