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 ),
43  m_w( 0 ),
44  m_h( 0 )
45  {}
46 
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 
62  SHAPE_RECT( const VECTOR2I& aP0, int aW, int aH ) :
63  SHAPE( SH_RECT ),
64  m_p0( aP0 ),
65  m_w( aW ),
66  m_h( aH )
67  {}
68 
69  SHAPE_RECT( const SHAPE_RECT& aOther ) :
70  SHAPE( SH_RECT ),
71  m_p0( aOther.m_p0 ),
72  m_w( aOther.m_w ),
73  m_h( aOther.m_h )
74  {};
75 
76  SHAPE* Clone() const override
77  {
78  return new SHAPE_RECT( *this );
79  }
80 
82  const BOX2I BBox( int aClearance = 0 ) const override
83  {
84  BOX2I bbox( VECTOR2I( m_p0.x - aClearance, m_p0.y - aClearance ),
85  VECTOR2I( m_w + 2 * aClearance, m_h + 2 * aClearance ) );
86  //printf("bb : %s\n",bbox.Format().c_str());
87  return bbox;
88  }
89 
96  int Diagonal() const
97  {
98  return VECTOR2I( m_w, m_h ).EuclideanNorm();
99  }
100 
101  bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const override
102  {
103  return SHAPE::Collide( aShape, aClearance, aMTV );
104  }
105 
106  bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
107  VECTOR2I* aLocation = nullptr ) const override
108  {
109  return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
110  }
111 
113  bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
114  VECTOR2I* aLocation = nullptr ) const override;
115 
121  const VECTOR2I& GetPosition() const
122  {
123  return m_p0;
124  }
125 
131  const VECTOR2I GetSize() const
132  {
133  return VECTOR2I( m_w, m_h );
134  }
135 
141  const int GetWidth() const
142  {
143  return m_w;
144  }
145 
151  const int GetHeight() const
152  {
153  return m_h;
154  }
155 
156  void Move( const VECTOR2I& aVector ) override
157  {
158  m_p0 += aVector;
159  }
160 
169  void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
170  {
171  m_p0 -= aCenter;
172  m_p0 = m_p0.Rotate( aAngle );
173  m_p0 += aCenter;
174 
175  if( abs( sin( aAngle ) ) == 1 )
176  std::swap( m_h, m_w );
177  }
178 
179  bool IsSolid() const override
180  {
181  return true;
182  }
183 
184  const SHAPE_LINE_CHAIN Outline() const
185  {
186  SHAPE_LINE_CHAIN rv;
187  rv.Append( m_p0 );
188  rv.Append( m_p0.x, m_p0.y + m_h );
189  rv.Append( m_p0.x + m_w, m_p0.y + m_h );
190  rv.Append( m_p0.x + m_w, m_p0.y );
191  rv.Append( m_p0 );
192  rv.SetClosed( true );
193  return rv;
194  }
195 
196  virtual const std::string Format( ) const override;
197 
198 private:
200  int m_w;
201  int m_h;
202 };
203 
204 #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:101
const SHAPE_LINE_CHAIN Outline() const
Definition: shape_rect.h:184
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:62
int Diagonal() const
Function Diagonal()
Definition: shape_rect.h:96
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:169
VECTOR2< int > VECTOR2I
Definition: vector2d.h:630
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
Function Collide()
Definition: shape.h:173
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
const VECTOR2I GetSize() const
Function GetSize()
Definition: shape_rect.h:131
void SetClosed(bool aClosed)
Function SetClosed()
SHAPE_RECT(const SHAPE_RECT &aOther)
Definition: shape_rect.h:69
VECTOR2I m_p0
Definition: shape_rect.h:199
virtual const std::string Format() const override
Definition: shape_rect.cpp:92
const VECTOR2I & GetPosition() const
Function GetPosition()
Definition: shape_rect.h:121
int m_h
Width
Definition: shape_rect.h:201
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Definition: shape_rect.h:106
SHAPE.
Definition: shape.h:123
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:51
int m_w
Top-left corner
Definition: shape_rect.h:200
void Move(const VECTOR2I &aVector) override
Definition: shape_rect.h:156
Definition: seg.h:39
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:378
SHAPE_LINE_CHAIN.
bool IsSolid() const override
Definition: shape_rect.h:179
const BOX2I BBox(int aClearance=0) const override
Function BBox()
Definition: shape_rect.h:82
SHAPE * Clone() const override
Function Clone()
Definition: shape_rect.h:76
const int GetHeight() const
Function GetHeight()
Definition: shape_rect.h:151
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:300
Definition: shape.h:43
const int GetWidth() const
Function GetWidth()
Definition: shape_rect.h:141