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 
50 public:
51  EDA_RECT() { };
52 
53  EDA_RECT( const wxPoint& aPos, const wxSize& aSize ) :
54  m_Pos( aPos ),
55  m_Size( aSize )
56  { }
57 
58  virtual ~EDA_RECT() { };
59 
60  wxPoint Centre() const
61  {
62  return wxPoint( m_Pos.x + ( m_Size.x >> 1 ),
63  m_Pos.y + ( m_Size.y >> 1 ) );
64  }
65 
71  void Move( const wxPoint& aMoveVector );
72 
77  void Normalize();
78 
84  bool Contains( const wxPoint& aPoint ) const;
85 
92  bool Contains( int x, int y ) const { return Contains( wxPoint( x, y ) ); }
93 
99  bool Contains( const EDA_RECT& aRect ) const;
100 
101  const wxSize GetSize() const { return m_Size; }
102 
107  int GetSizeMax() const { return ( m_Size.x > m_Size.y )?m_Size.x:m_Size.y; }
108 
109  int GetX() const { return m_Pos.x; }
110  int GetY() const { return m_Pos.y; }
111 
112  const wxPoint GetOrigin() const { return m_Pos; }
113  const wxPoint GetPosition() const { return m_Pos; }
114  const wxPoint GetEnd() const { return wxPoint( m_Pos.x + m_Size.x, m_Pos.y + m_Size.y ); }
115  const wxPoint GetCenter() const { return wxPoint( m_Pos.x + ( m_Size.x / 2 ), m_Pos.y + ( m_Size.y / 2 ) ); }
116 
117  int GetWidth() const { return m_Size.x; }
118  int GetHeight() const { return m_Size.y; }
119  int GetRight() const { return m_Pos.x + m_Size.x; }
120  int GetLeft() const { return m_Pos.x; }
121  int GetTop() const { return m_Pos.y; }
122  int GetBottom() const { return m_Pos.y + m_Size.y; } // Y axis from top to bottom
123 
124  void SetOrigin( const wxPoint& pos ) { m_Pos = pos; }
125  void SetOrigin( int x, int y ) { m_Pos.x = x; m_Pos.y = y; }
126  void SetSize( const wxSize& size ) { m_Size = size; }
127  void SetSize( int w, int h ) { m_Size.x = w; m_Size.y = h; }
128  void Offset( int dx, int dy ) { m_Pos.x += dx; m_Pos.y += dy; }
129  void Offset( const wxPoint& offset ) { m_Pos += offset; }
130  void SetX( int val ) { m_Pos.x = val; }
131  void SetY( int val ) { m_Pos.y = val; }
132  void SetWidth( int val ) { m_Size.x = val; }
133  void SetHeight( int val ) { m_Size.y = val; }
134  void SetEnd( int x, int y ) { SetEnd( wxPoint( x, y ) ); }
135  void SetEnd( const wxPoint& pos )
136  {
137  m_Size.x = pos.x - m_Pos.x; m_Size.y = pos.y - m_Pos.y;
138  }
139 
144  void RevertYAxis()
145  {
146  m_Pos.y = -m_Pos.y;
147  m_Size.y = -m_Size.y;
148  Normalize();
149  }
150 
159  bool Intersects( const EDA_RECT& aRect ) const;
160 
168  bool Intersects( const EDA_RECT& aRect, double aRot ) const;
169 
179  bool Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2 ) const;
180 
184  const wxPoint ClosestPointTo( const wxPoint& aPoint ) const;
185 
189  const wxPoint FarthestPointTo( const wxPoint& aPoint ) const;
190 
198  bool IntersectsCircle( const wxPoint& aCenter, const int aRadius ) const;
199 
208  bool IntersectsCircleEdge( const wxPoint& aCenter, const int aRadius, const int aWidth ) const;
209 
216  operator wxRect() const
217  {
218  EDA_RECT rect( m_Pos, m_Size );
219  rect.Normalize();
220  return wxRect( rect.m_Pos, rect.m_Size );
221  }
222 
228  operator BOX2I() const
229  {
230  EDA_RECT rect( m_Pos, m_Size );
231  rect.Normalize();
232  return BOX2I( rect.GetOrigin(), rect.GetSize() );
233  }
234 
240  EDA_RECT& Inflate( wxCoord dx, wxCoord dy );
241 
247  EDA_RECT& Inflate( int aDelta );
248 
255  void Merge( const EDA_RECT& aRect );
256 
262  void Merge( const wxPoint& aPoint );
263 
269  double GetArea() const;
270 
277  EDA_RECT Common( const EDA_RECT& aRect ) const;
278 
287  const EDA_RECT GetBoundingBoxRotated( wxPoint aRotCenter, double aAngle );
288 };
289 
290 
291 #endif // EDA_RECT_H
void Offset(int dx, int dy)
Definition: eda_rect.h:128
BOX2< VECTOR2I > BOX2I
Definition: box2.h:520
void Move(const wxPoint &aMoveVector)
Function Move moves the rectangle by the aMoveVector.
const wxPoint GetOrigin() const
Definition: eda_rect.h:112
void Merge(const EDA_RECT &aRect)
Function Merge modifies the position and size of the rectangle in order to contain aRect...
int GetLeft() const
Definition: eda_rect.h:120
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...
const wxSize GetSize() const
Definition: eda_rect.h:101
bool Contains(const wxPoint &aPoint) const
Function Contains.
bool IntersectsCircle(const wxPoint &aCenter, const int aRadius) const
Function IntersectsCircle tests for a common area between a circle and this rectangle.
EDA_RECT Common(const EDA_RECT &aRect) const
Function Common returns the area that is common with another rectangle.
int GetHeight() const
Definition: eda_rect.h:118
bool Contains(int x, int y) const
Function Contains.
Definition: eda_rect.h:92
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:124
const wxPoint ClosestPointTo(const wxPoint &aPoint) const
Return the point in this rect that is closest to the provided point.
void RevertYAxis()
Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size) ...
Definition: eda_rect.h:144
void SetSize(int w, int h)
Definition: eda_rect.h:127
EDA_RECT(const wxPoint &aPos, const wxSize &aSize)
Definition: eda_rect.h:53
wxSize m_Size
Definition: eda_rect.h:48
double GetArea() const
Function GetArea returns the area of the rectangle.
void SetHeight(int val)
Definition: eda_rect.h:133
void SetEnd(int x, int y)
Definition: eda_rect.h:134
const EDA_RECT GetBoundingBoxRotated(wxPoint aRotCenter, double aAngle)
Function GetBoundingBoxRotated.
int GetSizeMax() const
GetSizeMax.
Definition: eda_rect.h:107
void SetX(int val)
Definition: eda_rect.h:130
int GetBottom() const
Definition: eda_rect.h:122
wxPoint Centre() const
Definition: eda_rect.h:60
int GetRight() const
Definition: eda_rect.h:119
void SetWidth(int val)
Definition: eda_rect.h:132
const wxPoint GetPosition() const
Definition: eda_rect.h:113
void SetY(int val)
Definition: eda_rect.h:131
const wxPoint GetEnd() const
Definition: eda_rect.h:114
virtual ~EDA_RECT()
Definition: eda_rect.h:58
void Normalize()
Function Normalize ensures that the height ant width are positive.
void SetSize(const wxSize &size)
Definition: eda_rect.h:126
bool Intersects(const EDA_RECT &aRect) const
Function Intersects tests for a common area between rectangles.
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
wxPoint m_Pos
Definition: eda_rect.h:47
int GetX() const
Definition: eda_rect.h:109
int GetWidth() const
Definition: eda_rect.h:117
void SetEnd(const wxPoint &pos)
Definition: eda_rect.h:135
int GetY() const
Definition: eda_rect.h:110
EDA_RECT()
Definition: eda_rect.h:51
void SetOrigin(int x, int y)
Definition: eda_rect.h:125
void Offset(const wxPoint &offset)
Definition: eda_rect.h:129
const wxPoint FarthestPointTo(const wxPoint &aPoint) const
Return the point in this rect that is farthest from the provided point.
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
const wxPoint GetCenter() const
Definition: eda_rect.h:115
int GetTop() const
Definition: eda_rect.h:121