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-2020 KiCad Developers, see AUTHORS.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 EDA_RECT_H
30 #define EDA_RECT_H
31 
32 #include <wx/gdicmn.h>
33 #include <math/box2.h>
34 
42 class EDA_RECT
43 {
44 private:
45  wxPoint m_pos; // Rectangle Origin
46  wxSize m_size; // Rectangle Size
47  bool m_init; // Is the rectangle initialized
48 
49 public:
50  EDA_RECT() : m_init( false ) { };
51 
52  EDA_RECT( const wxPoint& aPos, const wxSize& aSize ) :
53  m_pos( aPos ),
54  m_size( aSize ),
55  m_init( true )
56  { }
57 
58  virtual ~EDA_RECT() { };
59 
60  wxPoint Centre() const
61  {
62  return wxPoint( m_pos.x + ( m_size.x >> 1 ), m_pos.y + ( m_size.y >> 1 ) );
63  }
64 
70  void Move( const wxPoint& aMoveVector );
71 
75  void Normalize();
76 
81  bool Contains( const wxPoint& aPoint ) const;
82 
88  bool Contains( int x, int y ) const { return Contains( wxPoint( x, y ) ); }
89 
94  bool Contains( const EDA_RECT& aRect ) const;
95 
96  const wxSize GetSize() const { return m_size; }
97 
101  int GetSizeMax() const { return ( m_size.x > m_size.y ) ? m_size.x : m_size.y; }
102 
103  int GetX() const { return m_pos.x; }
104  int GetY() const { return m_pos.y; }
105 
106  const wxPoint GetOrigin() const { return m_pos; }
107  const wxPoint GetPosition() const { return m_pos; }
108  const wxPoint GetEnd() const { return wxPoint( m_pos.x + m_size.x, m_pos.y + m_size.y ); }
109  const wxPoint GetCenter() const
110  {
111  return wxPoint( m_pos.x + ( m_size.x / 2 ), m_pos.y + ( m_size.y / 2 ) );
112  }
113 
114  int GetWidth() const { return m_size.x; }
115  int GetHeight() const { return m_size.y; }
116  int GetRight() const { return m_pos.x + m_size.x; }
117  int GetLeft() const { return m_pos.x; }
118  int GetTop() const { return m_pos.y; }
119  int GetBottom() const { return m_pos.y + m_size.y; } // Y axis from top to bottom
120 
121  bool IsValid() const
122  {
123  return m_init;
124  }
125 
126  void SetOrigin( const wxPoint &pos )
127  {
128  m_pos = pos;
129  m_init = true;
130  }
131 
132  void SetOrigin( int x, int y )
133  {
134  m_pos.x = x;
135  m_pos.y = y;
136  m_init = true;
137  }
138 
139  void SetSize( const wxSize &size )
140  {
141  m_size = size;
142  m_init = true;
143  }
144 
145  void SetSize( int w, int h )
146  {
147  m_size.x = w;
148  m_size.y = h;
149  m_init = true;
150  }
151 
152  void Offset( int dx, int dy )
153  {
154  m_pos.x += dx;
155  m_pos.y += dy;
156  }
157 
158  void Offset( const wxPoint &offset )
159  {
160  m_pos += offset;
161  }
162 
163  void SetX( int val )
164  {
165  m_pos.x = val;
166  m_init = true;
167  }
168 
169  void SetY( int val )
170  {
171  m_pos.y = val;
172  m_init = true;
173  }
174 
175  void SetWidth( int val )
176  {
177  m_size.x = val;
178  m_init = true;
179  }
180 
181  void SetHeight( int val )
182  {
183  m_size.y = val;
184  m_init = true;
185  }
186 
187  void SetEnd( int x, int y )
188  {
189  SetEnd( wxPoint( x, y ) );
190  m_init = true;
191  }
192 
193  void SetEnd( const wxPoint &pos )
194  {
195  m_size.x = pos.x - m_pos.x;
196  m_size.y = pos.y - m_pos.y;
197  m_init = true;
198  }
199 
203  void RevertYAxis()
204  {
205  m_pos.y = -m_pos.y;
206  m_size.y = -m_size.y;
207  Normalize();
208  }
209 
217  bool Intersects( const EDA_RECT& aRect ) const;
218 
225  bool Intersects( const EDA_RECT& aRect, double aRot ) const;
226 
235  bool Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2 ) const;
236 
246  bool Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2,
247  wxPoint* aIntersection1, wxPoint* aIntersection2 ) const;
248 
252  const wxPoint ClosestPointTo( const wxPoint& aPoint ) const;
253 
257  const wxPoint FarthestPointTo( const wxPoint& aPoint ) const;
258 
265  bool IntersectsCircle( const wxPoint& aCenter, const int aRadius ) const;
266 
274  bool IntersectsCircleEdge( const wxPoint& aCenter, const int aRadius, const int aWidth ) const;
275 
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 
304  EDA_RECT& Inflate( wxCoord dx, wxCoord dy );
305 
310  EDA_RECT& Inflate( int aDelta );
311 
319  void Merge( const EDA_RECT& aRect );
320 
326  void Merge( const wxPoint& aPoint );
327 
333  double GetArea() const;
334 
341  EDA_RECT Common( const EDA_RECT& aRect ) const;
342 
350  const EDA_RECT GetBoundingBoxRotated( wxPoint aRotCenter, double aAngle ) const;
351 };
352 
353 
354 #endif // EDA_RECT_H
void Offset(int dx, int dy)
Definition: eda_rect.h:152
BOX2< VECTOR2I > BOX2I
Definition: box2.h:522
void Move(const wxPoint &aMoveVector)
Move the rectangle by the aMoveVector.
Definition: eda_rect.cpp:51
void Merge(const EDA_RECT &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: eda_rect.cpp:431
const EDA_RECT GetBoundingBoxRotated(wxPoint aRotCenter, double aAngle) const
Useful to calculate bounding box of rotated items, when rotation if not k*90 degrees.
Definition: eda_rect.cpp:511
int GetX() const
Definition: eda_rect.h:103
int GetTop() const
Definition: eda_rect.h:118
int GetLeft() const
Definition: eda_rect.h:117
int GetWidth() const
Definition: eda_rect.h:114
bool IntersectsCircle(const wxPoint &aCenter, const int aRadius) const
Test for a common area between a circle and this rectangle.
Definition: eda_rect.cpp:314
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:126
bool IntersectsCircleEdge(const wxPoint &aCenter, const int aRadius, const int aWidth) const
Test for intersection between this rect and the edge (radius) of a circle.
Definition: eda_rect.cpp:330
EDA_RECT Common(const EDA_RECT &aRect) const
Return the area that is common with another rectangle.
Definition: eda_rect.cpp:487
void RevertYAxis()
Mirror the rectangle from the X axis (negate Y pos and size).
Definition: eda_rect.h:203
void SetSize(int w, int h)
Definition: eda_rect.h:145
bool Contains(const wxPoint &aPoint) const
Definition: eda_rect.cpp:57
int GetBottom() const
Definition: eda_rect.h:119
const wxPoint GetEnd() const
Definition: eda_rect.h:108
EDA_RECT(const wxPoint &aPos, const wxSize &aSize)
Definition: eda_rect.h:52
void SetHeight(int val)
Definition: eda_rect.h:181
const wxPoint GetOrigin() const
Definition: eda_rect.h:106
void SetEnd(int x, int y)
Definition: eda_rect.h:187
const wxPoint GetPosition() const
Definition: eda_rect.h:107
const wxPoint FarthestPointTo(const wxPoint &aPoint) const
Return the point in this rect that is farthest from the provided point.
Definition: eda_rect.cpp:301
int GetRight() const
Definition: eda_rect.h:116
void SetX(int val)
Definition: eda_rect.h:163
double GetArea() const
Return the area of the rectangle.
Definition: eda_rect.cpp:481
void SetWidth(int val)
Definition: eda_rect.h:175
int GetHeight() const
Definition: eda_rect.h:115
void SetY(int val)
Definition: eda_rect.h:169
bool Contains(int x, int y) const
Definition: eda_rect.h:88
virtual ~EDA_RECT()
Definition: eda_rect.h:58
void Normalize()
Ensures that the height ant width are positive.
Definition: eda_rect.cpp:35
void SetSize(const wxSize &size)
Definition: eda_rect.h:139
wxPoint m_pos
Definition: eda_rect.h:45
wxSize m_size
Definition: eda_rect.h:46
bool m_init
Definition: eda_rect.h:47
Handle the component boundary box.
Definition: eda_rect.h:42
int GetY() const
Definition: eda_rect.h:104
void SetEnd(const wxPoint &pos)
Definition: eda_rect.h:193
wxPoint Centre() const
Definition: eda_rect.h:60
bool Intersects(const EDA_RECT &aRect) const
Test for a common area between rectangles.
Definition: eda_rect.cpp:150
EDA_RECT()
Definition: eda_rect.h:50
bool IsValid() const
Definition: eda_rect.h:121
const wxPoint GetCenter() const
Definition: eda_rect.h:109
void SetOrigin(int x, int y)
Definition: eda_rect.h:132
int GetSizeMax() const
Definition: eda_rect.h:101
void Offset(const wxPoint &offset)
Definition: eda_rect.h:158
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Inflate the rectangle horizontally by dx and vertically by dy.
Definition: eda_rect.cpp:363
const wxSize GetSize() const
Definition: eda_rect.h:96
const wxPoint ClosestPointTo(const wxPoint &aPoint) const
Return the point in this rect that is closest to the provided point.
Definition: eda_rect.cpp:287