KiCad PCB EDA Suite
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) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2004-2014 KiCad Developers, see change_log.txt for contributors.
6  * Copyright (C) 2018 KiCad Developers, see AUTHORS.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 EDA_RECT_H
31 #define 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  bool m_init; // Is the rectangle initialized
50 
51 public:
52  EDA_RECT() : m_init( false ) { };
53 
54  EDA_RECT( const wxPoint& aPos, const wxSize& aSize ) :
55  m_Pos( aPos ),
56  m_Size( aSize ),
57  m_init( true )
58  { }
59 
60  virtual ~EDA_RECT() { };
61 
62  wxPoint Centre() const
63  {
64  return wxPoint( m_Pos.x + ( m_Size.x >> 1 ),
65  m_Pos.y + ( m_Size.y >> 1 ) );
66  }
67 
73  void Move( const wxPoint& aMoveVector );
74 
79  void Normalize();
80 
86  bool Contains( const wxPoint& aPoint ) const;
87 
94  bool Contains( int x, int y ) const { return Contains( wxPoint( x, y ) ); }
95 
101  bool Contains( const EDA_RECT& aRect ) const;
102 
103  const wxSize GetSize() const { return m_Size; }
104 
109  int GetSizeMax() const { return ( m_Size.x > m_Size.y )?m_Size.x:m_Size.y; }
110 
111  int GetX() const { return m_Pos.x; }
112  int GetY() const { return m_Pos.y; }
113 
114  const wxPoint GetOrigin() const { return m_Pos; }
115  const wxPoint GetPosition() const { return m_Pos; }
116  const wxPoint GetEnd() const { return wxPoint( m_Pos.x + m_Size.x, m_Pos.y + m_Size.y ); }
117  const wxPoint GetCenter() const { return wxPoint( m_Pos.x + ( m_Size.x / 2 ), m_Pos.y + ( m_Size.y / 2 ) ); }
118 
119  int GetWidth() const { return m_Size.x; }
120  int GetHeight() const { return m_Size.y; }
121  int GetRight() const { return m_Pos.x + m_Size.x; }
122  int GetLeft() const { return m_Pos.x; }
123  int GetTop() const { return m_Pos.y; }
124  int GetBottom() const { return m_Pos.y + m_Size.y; } // Y axis from top to bottom
125 
126  bool IsValid() const
127  {
128  return m_init;
129  }
130 
131  void SetOrigin( const wxPoint &pos )
132  {
133  m_Pos = pos;
134  m_init = true;
135  }
136 
137  void SetOrigin( int x, int y )
138  {
139  m_Pos.x = x;
140  m_Pos.y = y;
141  m_init = true;
142  }
143 
144  void SetSize( const wxSize &size )
145  {
146  m_Size = size;
147  m_init = true;
148  }
149 
150  void SetSize( int w, int h )
151  {
152  m_Size.x = w;
153  m_Size.y = h;
154  m_init = true;
155  }
156 
157  void Offset( int dx, int dy )
158  {
159  m_Pos.x += dx;
160  m_Pos.y += dy;
161  }
162 
163  void Offset( const wxPoint &offset )
164  {
165  m_Pos += offset;
166  }
167 
168  void SetX( int val )
169  {
170  m_Pos.x = val;
171  m_init = true;
172  }
173 
174  void SetY( int val )
175  {
176  m_Pos.y = val;
177  m_init = true;
178  }
179 
180  void SetWidth( int val )
181  {
182  m_Size.x = val;
183  m_init = true;
184  }
185 
186  void SetHeight( int val )
187  {
188  m_Size.y = val;
189  m_init = true;
190  }
191 
192  void SetEnd( int x, int y )
193  {
194  SetEnd( wxPoint( x, y ) );
195  m_init = true;
196  }
197 
198  void SetEnd( const wxPoint &pos )
199  {
200  m_Size.x = pos.x - m_Pos.x;
201  m_Size.y = pos.y - m_Pos.y;
202  m_init = true;
203  }
204 
209  void RevertYAxis()
210  {
211  m_Pos.y = -m_Pos.y;
212  m_Size.y = -m_Size.y;
213  Normalize();
214  }
215 
224  bool Intersects( const EDA_RECT& aRect ) const;
225 
233  bool Intersects( const EDA_RECT& aRect, double aRot ) const;
234 
244  bool Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2 ) const;
245 
249  const wxPoint ClosestPointTo( const wxPoint& aPoint ) const;
250 
254  const wxPoint FarthestPointTo( const wxPoint& aPoint ) const;
255 
263  bool IntersectsCircle( const wxPoint& aCenter, const int aRadius ) const;
264 
273  bool IntersectsCircleEdge( const wxPoint& aCenter, const int aRadius, const int aWidth ) const;
274 
281  operator wxRect() const
282  {
283  EDA_RECT rect( m_Pos, m_Size );
284  rect.Normalize();
285  return wxRect( rect.m_Pos, rect.m_Size );
286  }
287 
293  operator BOX2I() const
294  {
295  EDA_RECT rect( m_Pos, m_Size );
296  rect.Normalize();
297  return BOX2I( rect.GetOrigin(), rect.GetSize() );
298  }
299 
305  EDA_RECT& Inflate( wxCoord dx, wxCoord dy );
306 
312  EDA_RECT& Inflate( int aDelta );
313 
320  void Merge( const EDA_RECT& aRect );
321 
327  void Merge( const wxPoint& aPoint );
328 
334  double GetArea() const;
335 
342  EDA_RECT Common( const EDA_RECT& aRect ) const;
343 
352  const EDA_RECT GetBoundingBoxRotated( wxPoint aRotCenter, double aAngle );
353 };
354 
355 
356 #endif // EDA_RECT_H
void Offset(int dx, int dy)
Definition: eda_rect.h:157
BOX2< VECTOR2I > BOX2I
Definition: box2.h:522
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 GetX() const
Definition: eda_rect.h:111
int GetTop() const
Definition: eda_rect.h:123
int GetLeft() const
Definition: eda_rect.h:122
int GetWidth() const
Definition: eda_rect.h:119
bool IntersectsCircle(const wxPoint &aCenter, const int aRadius) const
Function IntersectsCircle tests for a common area between a circle and this rectangle.
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:131
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.
EDA_RECT Common(const EDA_RECT &aRect) const
Function Common returns the area that is common with another rectangle.
void RevertYAxis()
Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size)
Definition: eda_rect.h:209
void SetSize(int w, int h)
Definition: eda_rect.h:150
bool Contains(const wxPoint &aPoint) const
Function Contains.
int GetBottom() const
Definition: eda_rect.h:124
const wxPoint GetEnd() const
Definition: eda_rect.h:116
EDA_RECT(const wxPoint &aPos, const wxSize &aSize)
Definition: eda_rect.h:54
wxSize m_Size
Definition: eda_rect.h:48
void SetHeight(int val)
Definition: eda_rect.h:186
const wxPoint GetOrigin() const
Definition: eda_rect.h:114
void SetEnd(int x, int y)
Definition: eda_rect.h:192
const wxPoint GetPosition() const
Definition: eda_rect.h:115
const EDA_RECT GetBoundingBoxRotated(wxPoint aRotCenter, double aAngle)
Function GetBoundingBoxRotated.
const wxPoint FarthestPointTo(const wxPoint &aPoint) const
Return the point in this rect that is farthest from the provided point.
int GetRight() const
Definition: eda_rect.h:121
void SetX(int val)
Definition: eda_rect.h:168
double GetArea() const
Function GetArea returns the area of the rectangle.
void SetWidth(int val)
Definition: eda_rect.h:180
int GetHeight() const
Definition: eda_rect.h:120
void SetY(int val)
Definition: eda_rect.h:174
bool Contains(int x, int y) const
Function Contains.
Definition: eda_rect.h:94
virtual ~EDA_RECT()
Definition: eda_rect.h:60
void Normalize()
Function Normalize ensures that the height ant width are positive.
void SetSize(const wxSize &size)
Definition: eda_rect.h:144
bool m_init
Definition: eda_rect.h:49
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
wxPoint m_Pos
Definition: eda_rect.h:47
int GetY() const
Definition: eda_rect.h:112
void SetEnd(const wxPoint &pos)
Definition: eda_rect.h:198
wxPoint Centre() const
Definition: eda_rect.h:62
bool Intersects(const EDA_RECT &aRect) const
Function Intersects tests for a common area between rectangles.
EDA_RECT()
Definition: eda_rect.h:52
bool IsValid() const
Definition: eda_rect.h:126
const wxPoint GetCenter() const
Definition: eda_rect.h:117
void SetOrigin(int x, int y)
Definition: eda_rect.h:137
int GetSizeMax() const
GetSizeMax.
Definition: eda_rect.h:109
void Offset(const wxPoint &offset)
Definition: eda_rect.h:163
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
const wxSize GetSize() const
Definition: eda_rect.h:103
const wxPoint ClosestPointTo(const wxPoint &aPoint) const
Return the point in this rect that is closest to the provided point.