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 
94  bool Collide( const SEG& aSeg, int aClearance = 0 ) const override;
95 
101  const VECTOR2I& GetPosition() const
102  {
103  return m_p0;
104  }
105 
111  const VECTOR2I GetSize() const
112  {
113  return VECTOR2I( m_w, m_h );
114  }
115 
121  const int GetWidth() const
122  {
123  return m_w;
124  }
125 
131  const int GetHeight() const
132  {
133  return m_h;
134  }
135 
136  void Move( const VECTOR2I& aVector ) override
137  {
138  m_p0 += aVector;
139  }
140 
141  bool IsSolid() const override
142  {
143  return true;
144  }
145 
146  const SHAPE_LINE_CHAIN Outline() const
147  {
148  SHAPE_LINE_CHAIN rv;
149  rv.Append( m_p0 );
150  rv.Append( m_p0.x, m_p0.y + m_h );
151  rv.Append( m_p0.x + m_w, m_p0.y + m_h );
152  rv.Append( m_p0.x + m_w, m_p0.y );
153  rv.Append( m_p0 );
154  rv.SetClosed( true );
155  return rv;
156  }
157 
158 private:
161 
163  int m_w;
164 
166  int m_h;
167 };
168 
169 #endif // __SHAPE_RECT_H
SHAPE_RECT()
Constructor Creates an empty (0-sized) rectangle.
Definition: shape_rect.h:41
const SHAPE_LINE_CHAIN Outline() const
Definition: shape_rect.h:146
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
bool Collide(const SEG &aSeg, int aClearance=0) const override
Function Collide()
int Diagonal() const
Function Diagonal()
Definition: shape_rect.h:88
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:111
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:160
const VECTOR2I & GetPosition() const
Function GetPosition()
Definition: shape_rect.h:101
int m_h
Height
Definition: shape_rect.h:166
SHAPE.
Definition: shape.h:60
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:163
void Move(const VECTOR2I &aVector) override
Definition: shape_rect.h:136
Definition: seg.h:39
SHAPE_LINE_CHAIN.
bool IsSolid() const override
Definition: shape_rect.h:141
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:131
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:299
Definition: shape.h:45
const int GetWidth() const
Function GetWidth()
Definition: shape_rect.h:121