KiCad PCB EDA Suite
shape_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) 2013 CERN
5  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #ifndef __SHAPE_RECT_H
28 #define __SHAPE_RECT_H
29 
30 #include <geometry/seg.h>
31 #include <geometry/shape.h>
33 #include <math/box2.h>
34 #include <math/vector2d.h>
35 
36 class SHAPE_RECT : public SHAPE
37 {
38 public:
43  SHAPE( SH_RECT ),
44  m_w( 0 ),
45  m_h( 0 )
46  {}
47 
51  SHAPE_RECT( int aX0, int aY0, int aW, int aH ) :
52  SHAPE( SH_RECT ),
53  m_p0( aX0, aY0 ),
54  m_w( aW ),
55  m_h( aH )
56  {}
57 
61  SHAPE_RECT( const VECTOR2I& aP0, int aW, int aH ) :
62  SHAPE( SH_RECT ),
63  m_p0( aP0 ),
64  m_w( aW ),
65  m_h( aH )
66  {}
67 
68  SHAPE_RECT( const SHAPE_RECT& aOther ) :
69  SHAPE( SH_RECT ),
70  m_p0( aOther.m_p0 ),
71  m_w( aOther.m_w ),
72  m_h( aOther.m_h )
73  {};
74 
75  SHAPE* Clone() const override
76  {
77  return new SHAPE_RECT( *this );
78  }
79 
81  const BOX2I BBox( int aClearance = 0 ) const override
82  {
83  BOX2I bbox( VECTOR2I( m_p0.x - aClearance, m_p0.y - aClearance ),
84  VECTOR2I( m_w + 2 * aClearance, m_h + 2 * aClearance ) );
85  return bbox;
86  }
87 
93  int Diagonal() const
94  {
95  return VECTOR2I( m_w, m_h ).EuclideanNorm();
96  }
97 
98  bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const override
99  {
100  return SHAPE::Collide( aShape, aClearance, aMTV );
101  }
102 
103  bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
104  VECTOR2I* aLocation = nullptr ) const override
105  {
106  return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
107  }
108 
110  bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
111  VECTOR2I* aLocation = nullptr ) const override;
112 
116  const VECTOR2I& GetPosition() const
117  {
118  return m_p0;
119  }
120 
124  const VECTOR2I GetSize() const
125  {
126  return VECTOR2I( m_w, m_h );
127  }
128 
132  const int GetWidth() const
133  {
134  return m_w;
135  }
136 
140  const int GetHeight() const
141  {
142  return m_h;
143  }
144 
145  void Move( const VECTOR2I& aVector ) override
146  {
147  m_p0 += aVector;
148  }
149 
155  void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
156  {
157  m_p0 -= aCenter;
158  m_p0 = m_p0.Rotate( aAngle );
159  m_p0 += aCenter;
160 
161  if( abs( sin( aAngle ) ) == 1 )
162  std::swap( m_h, m_w );
163  }
164 
165  bool IsSolid() const override
166  {
167  return true;
168  }
169 
170  const SHAPE_LINE_CHAIN Outline() const
171  {
172  SHAPE_LINE_CHAIN rv;
173  rv.Append( m_p0 );
174  rv.Append( m_p0.x, m_p0.y + m_h );
175  rv.Append( m_p0.x + m_w, m_p0.y + m_h );
176  rv.Append( m_p0.x + m_w, m_p0.y );
177  rv.Append( m_p0 );
178  rv.SetClosed( true );
179  return rv;
180  }
181 
182  virtual const std::string Format( ) const override;
183 
184 private:
186  int m_w;
187  int m_h;
188 };
189 
190 #endif // __SHAPE_RECT_H
SHAPE_RECT()
Create an empty (0-sized) rectangle.
Definition: shape_rect.h:42
bool Collide(const SHAPE *aShape, int aClearance, VECTOR2I *aMTV) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
Definition: shape_rect.h:98
const SHAPE_LINE_CHAIN Outline() const
Definition: shape_rect.h:170
SHAPE_RECT(const VECTOR2I &aP0, int aW, int aH)
Create a rectangle defined by top-left corner aP0, width aW and height aH.
Definition: shape_rect.h:61
int Diagonal() const
Return length of the diagonal of the rectangle.
Definition: shape_rect.h:93
void Rotate(double aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
This function has limited utility for SHAPE_RECT as non-cartesian rotations will distort the rectangl...
Definition: shape_rect.h:155
VECTOR2< int > VECTOR2I
Definition: vector2d.h:623
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
Definition: shape.h:165
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
const VECTOR2I GetSize() const
Definition: shape_rect.h:124
void SetClosed(bool aClosed)
Function SetClosed()
SHAPE_RECT(const SHAPE_RECT &aOther)
Definition: shape_rect.h:68
VECTOR2I m_p0
Top-left corner.
Definition: shape_rect.h:185
virtual const std::string Format() const override
Definition: shape_rect.cpp:92
const VECTOR2I & GetPosition() const
Definition: shape_rect.h:116
int m_h
Height.
Definition: shape_rect.h:187
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Definition: shape_rect.h:103
An abstract shape on 2D plane.
Definition: shape.h:116
SHAPE_RECT(int aX0, int aY0, int aW, int aH)
Create a rectangle defined by top-left corner (aX0, aY0), width aW and height aH.
Definition: shape_rect.h:51
int m_w
Width.
Definition: shape_rect.h:186
void Move(const VECTOR2I &aVector) override
Definition: shape_rect.h:145
Definition: seg.h:41
VECTOR2< T > Rotate(double aAngle) const
Rotate the vector by a given angle.
Definition: vector2d.h:371
SHAPE_LINE_CHAIN.
bool IsSolid() const override
Definition: shape_rect.h:165
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Definition: shape_rect.h:81
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
Definition: shape_rect.h:75
const int GetHeight() const
Definition: shape_rect.h:140
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:293
axis-aligned rectangle
Definition: shape.h:43
const int GetWidth() const
Definition: shape_rect.h:132