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 
96  bool DoCollide( const SEG& aSeg, int aClearance, int* aActualDist ) const;
97 
103  const VECTOR2I& GetPosition() const
104  {
105  return m_p0;
106  }
107 
113  const VECTOR2I GetSize() const
114  {
115  return VECTOR2I( m_w, m_h );
116  }
117 
123  const int GetWidth() const
124  {
125  return m_w;
126  }
127 
133  const int GetHeight() const
134  {
135  return m_h;
136  }
137 
138  void Move( const VECTOR2I& aVector ) override
139  {
140  m_p0 += aVector;
141  }
142 
143  bool IsSolid() const override
144  {
145  return true;
146  }
147 
148  const SHAPE_LINE_CHAIN Outline() const
149  {
150  SHAPE_LINE_CHAIN rv;
151  rv.Append( m_p0 );
152  rv.Append( m_p0.x, m_p0.y + m_h );
153  rv.Append( m_p0.x + m_w, m_p0.y + m_h );
154  rv.Append( m_p0.x + m_w, m_p0.y );
155  rv.Append( m_p0 );
156  rv.SetClosed( true );
157  return rv;
158  }
159 
160 private:
163 
165  int m_w;
166 
168  int m_h;
169 };
170 
171 #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:148
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:113
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:162
const VECTOR2I & GetPosition() const
Function GetPosition()
Definition: shape_rect.h:103
int m_h
Height
Definition: shape_rect.h:168
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:165
void Move(const VECTOR2I &aVector) override
Definition: shape_rect.h:138
Definition: seg.h:39
SHAPE_LINE_CHAIN.
bool IsSolid() const override
Definition: shape_rect.h:143
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:133
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:299
Definition: shape.h:45
const int GetWidth() const
Function GetWidth()
Definition: shape_rect.h:123
bool DoCollide(const SEG &aSeg, int aClearance, int *aActualDist) const