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/shape.h>
30 #include <geometry/shape_circle.h>
31 #include <geometry/seg.h>
32 
33 class SHAPE_RECT : public SHAPE
34 {
35 public:
41  SHAPE( SH_RECT ), m_w( 0 ), m_h( 0 )
42  {}
43 
48  SHAPE_RECT( int aX0, int aY0, int aW, int aH ) :
49  SHAPE( SH_RECT ), m_p0( aX0, aY0 ), m_w( aW ), m_h( aH )
50  {}
51 
56  SHAPE_RECT( const VECTOR2I& aP0, int aW, int aH ) :
57  SHAPE( SH_RECT ), m_p0( aP0 ), m_w( aW ), m_h( aH )
58  {}
59 
60  SHAPE_RECT( const SHAPE_RECT& aOther ) :
61  SHAPE( SH_RECT ),
62  m_p0( aOther.m_p0 ),
63  m_w( aOther.m_w ),
64  m_h( aOther.m_h )
65  {};
66 
67  SHAPE* Clone() const override
68  {
69  return new SHAPE_RECT( *this );
70  }
71 
73  const BOX2I BBox( int aClearance = 0 ) const override
74  {
75  BOX2I bbox( VECTOR2I( m_p0.x - aClearance, m_p0.y - aClearance ),
76  VECTOR2I( m_w + 2 * aClearance, m_h + 2 * aClearance ) );
77  //printf("bb : %s\n",bbox.Format().c_str());
78  return bbox;
79  }
80 
87  int Diagonal() const
88  {
89  return VECTOR2I( m_w, m_h ).EuclideanNorm();
90  }
91 
93  bool Collide( const SEG& aSeg, int aClearance = 0 ) const override;
94 
100  const VECTOR2I& GetPosition() const
101  {
102  return m_p0;
103  }
104 
110  const VECTOR2I GetSize() const
111  {
112  return VECTOR2I( m_w, m_h );
113  }
114 
120  const int GetWidth() const
121  {
122  return m_w;
123  }
124 
130  const int GetHeight() const
131  {
132  return m_h;
133  }
134 
135  void Move( const VECTOR2I& aVector ) override
136  {
137  m_p0 += aVector;
138  }
139 
140  bool IsSolid() const override
141  {
142  return true;
143  }
144 
145  const SHAPE_LINE_CHAIN Outline() const
146  {
147  SHAPE_LINE_CHAIN rv;
148  rv.Append( m_p0 );
149  rv.Append( m_p0.x, m_p0.y + m_h );
150  rv.Append( m_p0.x + m_w, m_p0.y + m_h );
151  rv.Append( m_p0.x + m_w, m_p0.y );
152  rv.Append( m_p0 );
153  rv.SetClosed( true );
154  return rv;
155  }
156 
157 private:
160 
162  int m_w;
163 
165  int m_h;
166 };
167 
168 #endif // __SHAPE_RECT_H
const SHAPE_LINE_CHAIN Outline() const
Definition: shape_rect.h:145
SHAPE_RECT()
Constructor Creates an empty (0-sized) rectangle.
Definition: shape_rect.h:40
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:56
int Diagonal() const
Function Diagonal()
Definition: shape_rect.h:87
bool Collide(const SEG &aSeg, int aClearance=0) const override
Function Collide()
VECTOR2< int > VECTOR2I
Definition: vector2d.h:590
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
const int GetWidth() const
Function GetWidth()
Definition: shape_rect.h:120
void SetClosed(bool aClosed)
Function SetClosed()
SHAPE_RECT(const SHAPE_RECT &aOther)
Definition: shape_rect.h:60
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:295
VECTOR2I m_p0
Top-left corner
Definition: shape_rect.h:159
const VECTOR2I & GetPosition() const
Function GetPosition()
Definition: shape_rect.h:100
int m_h
Height
Definition: shape_rect.h:165
Class SHAPE.
Definition: shape.h:57
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:48
int m_w
Width
Definition: shape_rect.h:162
void Move(const VECTOR2I &aVector) override
Definition: shape_rect.h:135
Definition: seg.h:37
Class SHAPE_LINE_CHAIN.
bool IsSolid() const override
Definition: shape_rect.h:140
const BOX2I BBox(int aClearance=0) const override
Function BBox()
Definition: shape_rect.h:73
SHAPE * Clone() const override
Function Clone()
Definition: shape_rect.h:67
Definition: shape.h:43
const int GetHeight() const
Function GetHeight()
Definition: shape_rect.h:130
const VECTOR2I GetSize() const
Function GetSize()
Definition: shape_rect.h:110