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  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
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 
25 #ifndef __SHAPE_RECT_H
26 #define __SHAPE_RECT_H
27 
28 #include <geometry/seg.h>
29 #include <geometry/shape.h>
31 #include <math/box2.h>
32 #include <math/vector2d.h>
33 
34 class SHAPE_RECT : public SHAPE
35 {
36 public:
42  SHAPE( SH_RECT ), m_w( 0 ), m_h( 0 )
43  {}
44 
49  SHAPE_RECT( int aX0, int aY0, int aW, int aH ) :
50  SHAPE( SH_RECT ), m_p0( aX0, aY0 ), m_w( aW ), m_h( aH )
51  {}
52 
57  SHAPE_RECT( const VECTOR2I& aP0, int aW, int aH ) :
58  SHAPE( SH_RECT ), m_p0( aP0 ), m_w( aW ), m_h( aH )
59  {}
60 
61  SHAPE_RECT( const SHAPE_RECT& aOther ) :
62  SHAPE( SH_RECT ),
63  m_p0( aOther.m_p0 ),
64  m_w( aOther.m_w ),
65  m_h( aOther.m_h )
66  {};
67 
68  SHAPE* Clone() const override
69  {
70  return new SHAPE_RECT( *this );
71  }
72 
74  const BOX2I BBox( int aClearance = 0 ) const override
75  {
76  BOX2I bbox( VECTOR2I( m_p0.x - aClearance, m_p0.y - aClearance ),
77  VECTOR2I( m_w + 2 * aClearance, m_h + 2 * aClearance ) );
78  //printf("bb : %s\n",bbox.Format().c_str());
79  return bbox;
80  }
81 
88  int Diagonal() const
89  {
90  return VECTOR2I( m_w, m_h ).EuclideanNorm();
91  }
92 
93  bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const override
94  {
95  return SHAPE::Collide( aShape, aClearance, aMTV );
96  }
97 
98  bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr ) const override
99  {
100  return SHAPE::Collide( aShape, aClearance, aActual );
101  }
102 
104  bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr ) const override;
105 
111  const VECTOR2I& GetPosition() const
112  {
113  return m_p0;
114  }
115 
121  const VECTOR2I GetSize() const
122  {
123  return VECTOR2I( m_w, m_h );
124  }
125 
131  const int GetWidth() const
132  {
133  return m_w;
134  }
135 
141  const int GetHeight() const
142  {
143  return m_h;
144  }
145 
146  void Move( const VECTOR2I& aVector ) override
147  {
148  m_p0 += aVector;
149  }
150 
159  void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
160  {
161  m_p0 -= aCenter;
162  m_p0 = m_p0.Rotate( aAngle );
163  m_p0 += aCenter;
164 
165  if( abs( sin( aAngle ) ) == 1 )
166  std::swap( m_h, m_w );
167  }
168 
169  bool IsSolid() const override
170  {
171  return true;
172  }
173 
174  const SHAPE_LINE_CHAIN Outline() const
175  {
176  SHAPE_LINE_CHAIN rv;
177  rv.Append( m_p0 );
178  rv.Append( m_p0.x, m_p0.y + m_h );
179  rv.Append( m_p0.x + m_w, m_p0.y + m_h );
180  rv.Append( m_p0.x + m_w, m_p0.y );
181  rv.Append( m_p0 );
182  rv.SetClosed( true );
183  return rv;
184  }
185 
186 private:
189 
191  int m_w;
192 
194  int m_h;
195 };
196 
197 #endif // __SHAPE_RECT_H
SHAPE_RECT()
Constructor Creates an empty (0-sized) rectangle.
Definition: shape_rect.h:41
bool Collide(const SHAPE *aShape, int aClearance, VECTOR2I *aMTV) const override
Function Collide()
Definition: shape_rect.h:93
const SHAPE_LINE_CHAIN Outline() const
Definition: shape_rect.h:174
SHAPE_RECT(const VECTOR2I &aP0, int aW, int aH)
Constructor Creates a rectangle defined by top-left corner aP0, width aW and height aH.
Definition: shape_rect.h:57
int Diagonal() const
Function Diagonal()
Definition: shape_rect.h:88
void Rotate(double aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
Function Rotate() This function has limited utility for SHAPE_RECT as non-cartesian rotations will di...
Definition: shape_rect.h:159
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
const VECTOR2I GetSize() const
Function GetSize()
Definition: shape_rect.h:121
void SetClosed(bool aClosed)
Function SetClosed()
SHAPE_RECT(const SHAPE_RECT &aOther)
Definition: shape_rect.h:61
VECTOR2I m_p0
Top-left corner
Definition: shape_rect.h:188
const VECTOR2I & GetPosition() const
Function GetPosition()
Definition: shape_rect.h:111
int m_h
Height
Definition: shape_rect.h:194
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr) const override
Definition: shape_rect.h:98
SHAPE.
Definition: shape.h:74
SHAPE_RECT(int aX0, int aY0, int aW, int aH)
Constructor Creates a rectangle defined by top-left corner (aX0, aY0), width aW and height aH.
Definition: shape_rect.h:49
int m_w
Width
Definition: shape_rect.h:191
void Move(const VECTOR2I &aVector) override
Definition: shape_rect.h:146
Definition: seg.h:39
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:377
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr) const
Function Collide()
Definition: shape.h:125
SHAPE_LINE_CHAIN.
bool IsSolid() const override
Definition: shape_rect.h:169
const BOX2I BBox(int aClearance=0) const override
Function BBox()
Definition: shape_rect.h:74
SHAPE * Clone() const override
Function Clone()
Definition: shape_rect.h:68
const int GetHeight() const
Function GetHeight()
Definition: shape_rect.h:141
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:299
Definition: shape.h:41
const int GetWidth() const
Function GetWidth()
Definition: shape_rect.h:131