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  template<class T>
54  EDA_RECT( const BOX2<T> aBox )
55  {
56  m_pos = (wxPoint) aBox.GetPosition();
57  m_size.x = aBox.GetWidth();
58  m_size.y = aBox.GetHeight();
59  m_init = true;
60  }
61 
62  virtual ~EDA_RECT() { };
63 
64  wxPoint Centre() const
65  {
66  return wxPoint( m_pos.x + ( m_size.x >> 1 ), m_pos.y + ( m_size.y >> 1 ) );
67  }
68 
74  void Move( const wxPoint& aMoveVector );
75 
79  void Normalize();
80 
85  bool Contains( const wxPoint& aPoint ) const;
86 
92  bool Contains( int x, int y ) const { return Contains( wxPoint( x, y ) ); }
93 
98  bool Contains( const EDA_RECT& aRect ) const;
99 
100  const wxSize GetSize() const { return m_size; }
101 
105  int GetSizeMax() const { return ( m_size.x > m_size.y ) ? m_size.x : m_size.y; }
106 
107  int GetX() const { return m_pos.x; }
108  int GetY() const { return m_pos.y; }
109 
110  const wxPoint GetOrigin() const { return m_pos; }
111  const wxPoint GetPosition() const { return m_pos; }
112  const wxPoint GetEnd() const { return wxPoint( m_pos.x + m_size.x, m_pos.y + m_size.y ); }
113  const wxPoint GetCenter() const
114  {
115  return wxPoint( m_pos.x + ( m_size.x / 2 ), m_pos.y + ( m_size.y / 2 ) );
116  }
117 
118  int GetWidth() const { return m_size.x; }
119  int GetHeight() const { return m_size.y; }
120  int GetRight() const { return m_pos.x + m_size.x; }
121  int GetLeft() const { return m_pos.x; }
122  int GetTop() const { return m_pos.y; }
123  int GetBottom() const { return m_pos.y + m_size.y; } // Y axis from top to bottom
124 
125  bool IsValid() const
126  {
127  return m_init;
128  }
129 
130  void SetOrigin( const wxPoint& pos )
131  {
132  m_pos = pos;
133  m_init = true;
134  }
135 
136  void SetOrigin( int x, int y )
137  {
138  m_pos.x = x;
139  m_pos.y = y;
140  m_init = true;
141  }
142 
143  void SetSize( const wxSize& size )
144  {
145  m_size = size;
146  m_init = true;
147  }
148 
149  void SetSize( int w, int h )
150  {
151  m_size.x = w;
152  m_size.y = h;
153  m_init = true;
154  }
155 
156  void Offset( int dx, int dy )
157  {
158  m_pos.x += dx;
159  m_pos.y += dy;
160  }
161 
162  void Offset( const wxPoint& offset )
163  {
164  m_pos += offset;
165  }
166 
167  void SetX( int val )
168  {
169  m_pos.x = val;
170  m_init = true;
171  }
172 
173  void SetY( int val )
174  {
175  m_pos.y = val;
176  m_init = true;
177  }
178 
179  void SetWidth( int val )
180  {
181  m_size.x = val;
182  m_init = true;
183  }
184 
185  void SetHeight( int val )
186  {
187  m_size.y = val;
188  m_init = true;
189  }
190 
191  void SetEnd( int x, int y )
192  {
193  SetEnd( wxPoint( x, y ) );
194  m_init = true;
195  }
196 
197  void SetEnd( const wxPoint& pos )
198  {
199  m_size.x = pos.x - m_pos.x;
200  m_size.y = pos.y - m_pos.y;
201  m_init = true;
202  }
203 
207  void RevertYAxis()
208  {
209  m_pos.y = -m_pos.y;
210  m_size.y = -m_size.y;
211  Normalize();
212  }
213 
221  bool Intersects( const EDA_RECT& aRect ) const;
222 
229  bool Intersects( const EDA_RECT& aRect, double aRot ) const;
230 
239  bool Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2 ) const;
240 
250  bool Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2,
251  wxPoint* aIntersection1, wxPoint* aIntersection2 ) const;
252 
256  const wxPoint ClosestPointTo( const wxPoint& aPoint ) const;
257 
261  const wxPoint FarthestPointTo( const wxPoint& aPoint ) const;
262 
269  bool IntersectsCircle( const wxPoint& aCenter, const int aRadius ) const;
270 
278  bool IntersectsCircleEdge( const wxPoint& aCenter, const int aRadius, const int aWidth ) const;
279 
285  operator wxRect() const
286  {
287  EDA_RECT rect( m_pos, m_size );
288  rect.Normalize();
289  return wxRect( rect.m_pos, rect.m_size );
290  }
291 
297  operator BOX2I() const
298  {
299  EDA_RECT rect( m_pos, m_size );
300  rect.Normalize();
301  return BOX2I( rect.GetOrigin(), rect.GetSize() );
302  }
303 
308  EDA_RECT& Inflate( wxCoord dx, wxCoord dy );
309 
314  EDA_RECT& Inflate( int aDelta );
315 
323  void Merge( const EDA_RECT& aRect );
324 
330  void Merge( const wxPoint& aPoint );
331 
337  double GetArea() const;
338 
345  EDA_RECT Common( const EDA_RECT& aRect ) const;
346 
354  const EDA_RECT GetBoundingBoxRotated( const wxPoint& aRotCenter, double aAngle ) const;
355 
356 private:
357  wxPoint m_pos; // Rectangle Origin
358  wxSize m_size; // Rectangle Size
359  bool m_init; // Is the rectangle initialized
360 };
361 
362 
363 #endif // EDA_RECT_H
void Offset(int dx, int dy)
Definition: eda_rect.h:156
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:107
int GetTop() const
Definition: eda_rect.h:122
int GetLeft() const
Definition: eda_rect.h:121
int GetWidth() const
Definition: eda_rect.h:118
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:130
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:207
A 2D bounding box built on top of an origin point and size vector.
Definition: box2.h:41
void SetSize(int w, int h)
Definition: eda_rect.h:149
bool Contains(const wxPoint &aPoint) const
Definition: eda_rect.cpp:57
int GetBottom() const
Definition: eda_rect.h:123
const wxPoint GetEnd() const
Definition: eda_rect.h:112
EDA_RECT(const wxPoint &aPos, const wxSize &aSize)
Definition: eda_rect.h:47
void SetHeight(int val)
Definition: eda_rect.h:185
const wxPoint GetOrigin() const
Definition: eda_rect.h:110
void SetEnd(int x, int y)
Definition: eda_rect.h:191
coord_type GetWidth() const
Definition: box2.h:180
const wxPoint GetPosition() const
Definition: eda_rect.h:111
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:120
void SetX(int val)
Definition: eda_rect.h:167
double GetArea() const
Return the area of the rectangle.
Definition: eda_rect.cpp:483
void SetWidth(int val)
Definition: eda_rect.h:179
int GetHeight() const
Definition: eda_rect.h:119
void SetY(int val)
Definition: eda_rect.h:173
const Vec & GetPosition() const
Definition: box2.h:177
bool Contains(int x, int y) const
Definition: eda_rect.h:92
virtual ~EDA_RECT()
Definition: eda_rect.h:62
void Normalize()
Ensures that the height ant width are positive.
Definition: eda_rect.cpp:35
void SetSize(const wxSize &size)
Definition: eda_rect.h:143
wxPoint m_pos
Definition: eda_rect.h:357
wxSize m_size
Definition: eda_rect.h:358
bool m_init
Definition: eda_rect.h:359
EDA_RECT(const BOX2< T > aBox)
Definition: eda_rect.h:54
Handle the component boundary box.
Definition: eda_rect.h:42
int GetY() const
Definition: eda_rect.h:108
coord_type GetHeight() const
Definition: box2.h:181
void SetEnd(const wxPoint &pos)
Definition: eda_rect.h:197
wxPoint Centre() const
Definition: eda_rect.h:64
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:125
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:113
void SetOrigin(int x, int y)
Definition: eda_rect.h:136
int GetSizeMax() const
Definition: eda_rect.h:105
void Offset(const wxPoint &offset)
Definition: eda_rect.h:162
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:100
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