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-2021 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 public:
45  EDA_RECT() : m_init( false ) { };
46 
47  EDA_RECT( const wxPoint& aPos, const wxSize& aSize ) :
48  m_pos( aPos ),
49  m_size( aSize ),
50  m_init( true )
51  { }
52 
53  virtual ~EDA_RECT() { };
54 
55  wxPoint Centre() const
56  {
57  return wxPoint( m_pos.x + ( m_size.x >> 1 ), m_pos.y + ( m_size.y >> 1 ) );
58  }
59 
65  void Move( const wxPoint& aMoveVector );
66 
70  void Normalize();
71 
76  bool Contains( const wxPoint& aPoint ) const;
77 
83  bool Contains( int x, int y ) const { return Contains( wxPoint( x, y ) ); }
84 
89  bool Contains( const EDA_RECT& aRect ) const;
90 
91  const wxSize GetSize() const { return m_size; }
92 
96  int GetSizeMax() const { return ( m_size.x > m_size.y ) ? m_size.x : m_size.y; }
97 
98  int GetX() const { return m_pos.x; }
99  int GetY() const { return m_pos.y; }
100 
101  const wxPoint GetOrigin() const { return m_pos; }
102  const wxPoint GetPosition() const { return m_pos; }
103  const wxPoint GetEnd() const { return wxPoint( m_pos.x + m_size.x, m_pos.y + m_size.y ); }
104  const wxPoint GetCenter() const
105  {
106  return wxPoint( m_pos.x + ( m_size.x / 2 ), m_pos.y + ( m_size.y / 2 ) );
107  }
108 
109  int GetWidth() const { return m_size.x; }
110  int GetHeight() const { return m_size.y; }
111  int GetRight() const { return m_pos.x + m_size.x; }
112  int GetLeft() const { return m_pos.x; }
113  int GetTop() const { return m_pos.y; }
114  int GetBottom() const { return m_pos.y + m_size.y; } // Y axis from top to bottom
115 
116  bool IsValid() const
117  {
118  return m_init;
119  }
120 
121  void SetOrigin( const wxPoint& pos )
122  {
123  m_pos = pos;
124  m_init = true;
125  }
126 
127  void SetOrigin( int x, int y )
128  {
129  m_pos.x = x;
130  m_pos.y = y;
131  m_init = true;
132  }
133 
134  void SetSize( const wxSize& size )
135  {
136  m_size = size;
137  m_init = true;
138  }
139 
140  void SetSize( int w, int h )
141  {
142  m_size.x = w;
143  m_size.y = h;
144  m_init = true;
145  }
146 
147  void Offset( int dx, int dy )
148  {
149  m_pos.x += dx;
150  m_pos.y += dy;
151  }
152 
153  void Offset( const wxPoint& offset )
154  {
155  m_pos += offset;
156  }
157 
158  void SetX( int val )
159  {
160  m_pos.x = val;
161  m_init = true;
162  }
163 
164  void SetY( int val )
165  {
166  m_pos.y = val;
167  m_init = true;
168  }
169 
170  void SetWidth( int val )
171  {
172  m_size.x = val;
173  m_init = true;
174  }
175 
176  void SetHeight( int val )
177  {
178  m_size.y = val;
179  m_init = true;
180  }
181 
182  void SetEnd( int x, int y )
183  {
184  SetEnd( wxPoint( x, y ) );
185  m_init = true;
186  }
187 
188  void SetEnd( const wxPoint& pos )
189  {
190  m_size.x = pos.x - m_pos.x;
191  m_size.y = pos.y - m_pos.y;
192  m_init = true;
193  }
194 
198  void RevertYAxis()
199  {
200  m_pos.y = -m_pos.y;
201  m_size.y = -m_size.y;
202  Normalize();
203  }
204 
212  bool Intersects( const EDA_RECT& aRect ) const;
213 
220  bool Intersects( const EDA_RECT& aRect, double aRot ) const;
221 
230  bool Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2 ) const;
231 
241  bool Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2,
242  wxPoint* aIntersection1, wxPoint* aIntersection2 ) const;
243 
247  const wxPoint ClosestPointTo( const wxPoint& aPoint ) const;
248 
252  const wxPoint FarthestPointTo( const wxPoint& aPoint ) const;
253 
260  bool IntersectsCircle( const wxPoint& aCenter, const int aRadius ) const;
261 
269  bool IntersectsCircleEdge( const wxPoint& aCenter, const int aRadius, const int aWidth ) const;
270 
276  operator wxRect() const
277  {
278  EDA_RECT rect( m_pos, m_size );
279  rect.Normalize();
280  return wxRect( rect.m_pos, rect.m_size );
281  }
282 
288  operator BOX2I() const
289  {
290  EDA_RECT rect( m_pos, m_size );
291  rect.Normalize();
292  return BOX2I( rect.GetOrigin(), rect.GetSize() );
293  }
294 
299  EDA_RECT& Inflate( wxCoord dx, wxCoord dy );
300 
305  EDA_RECT& Inflate( int aDelta );
306 
314  void Merge( const EDA_RECT& aRect );
315 
321  void Merge( const wxPoint& aPoint );
322 
328  double GetArea() const;
329 
336  EDA_RECT Common( const EDA_RECT& aRect ) const;
337 
345  const EDA_RECT GetBoundingBoxRotated( const wxPoint& aRotCenter, double aAngle ) const;
346 
347 private:
348  wxPoint m_pos; // Rectangle Origin
349  wxSize m_size; // Rectangle Size
350  bool m_init; // Is the rectangle initialized
351 };
352 
353 
354 #endif // EDA_RECT_H
void Offset(int dx, int dy)
Definition: eda_rect.h:147
BOX2< VECTOR2I > BOX2I
Definition: box2.h:506
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:432
int GetX() const
Definition: eda_rect.h:98
int GetTop() const
Definition: eda_rect.h:113
int GetLeft() const
Definition: eda_rect.h:112
int GetWidth() const
Definition: eda_rect.h:109
bool IntersectsCircle(const wxPoint &aCenter, const int aRadius) const
Test for a common area between a circle and this rectangle.
Definition: eda_rect.cpp:315
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:121
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:331
EDA_RECT Common(const EDA_RECT &aRect) const
Return the area that is common with another rectangle.
Definition: eda_rect.cpp:489
void RevertYAxis()
Mirror the rectangle from the X axis (negate Y pos and size).
Definition: eda_rect.h:198
void SetSize(int w, int h)
Definition: eda_rect.h:140
bool Contains(const wxPoint &aPoint) const
Definition: eda_rect.cpp:57
int GetBottom() const
Definition: eda_rect.h:114
const wxPoint GetEnd() const
Definition: eda_rect.h:103
EDA_RECT(const wxPoint &aPos, const wxSize &aSize)
Definition: eda_rect.h:47
void SetHeight(int val)
Definition: eda_rect.h:176
const wxPoint GetOrigin() const
Definition: eda_rect.h:101
void SetEnd(int x, int y)
Definition: eda_rect.h:182
const wxPoint GetPosition() const
Definition: eda_rect.h:102
const wxPoint FarthestPointTo(const wxPoint &aPoint) const
Return the point in this rect that is farthest from the provided point.
Definition: eda_rect.cpp:302
int GetRight() const
Definition: eda_rect.h:111
void SetX(int val)
Definition: eda_rect.h:158
double GetArea() const
Return the area of the rectangle.
Definition: eda_rect.cpp:483
void SetWidth(int val)
Definition: eda_rect.h:170
int GetHeight() const
Definition: eda_rect.h:110
void SetY(int val)
Definition: eda_rect.h:164
bool Contains(int x, int y) const
Definition: eda_rect.h:83
virtual ~EDA_RECT()
Definition: eda_rect.h:53
void Normalize()
Ensures that the height ant width are positive.
Definition: eda_rect.cpp:35
void SetSize(const wxSize &size)
Definition: eda_rect.h:134
wxPoint m_pos
Definition: eda_rect.h:348
wxSize m_size
Definition: eda_rect.h:349
bool m_init
Definition: eda_rect.h:350
Handle the component boundary box.
Definition: eda_rect.h:42
int GetY() const
Definition: eda_rect.h:99
void SetEnd(const wxPoint &pos)
Definition: eda_rect.h:188
wxPoint Centre() const
Definition: eda_rect.h:55
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:45
bool IsValid() const
Definition: eda_rect.h:116
const EDA_RECT GetBoundingBoxRotated(const wxPoint &aRotCenter, double aAngle) const
Useful to calculate bounding box of rotated items, when rotation if not k*90 degrees.
Definition: eda_rect.cpp:513
const wxPoint GetCenter() const
Definition: eda_rect.h:104
void SetOrigin(int x, int y)
Definition: eda_rect.h:127
int GetSizeMax() const
Definition: eda_rect.h:96
void Offset(const wxPoint &offset)
Definition: eda_rect.h:153
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Inflate the rectangle horizontally by dx and vertically by dy.
Definition: eda_rect.cpp:364
const wxSize GetSize() const
Definition: eda_rect.h:91
const wxPoint ClosestPointTo(const wxPoint &aPoint) const
Return the point in this rect that is closest to the provided point.
Definition: eda_rect.cpp:288