KiCad PCB EDA Suite
shape_circle.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_CIRCLE_H
26 #define __SHAPE_CIRCLE_H
27 
28 #include <geometry/shape.h>
29 #include <math/box2.h>
30 #include <math/vector2d.h>
31 
32 #include <algorithm>
33 
34 class SHAPE_CIRCLE : public SHAPE
35 {
36 public:
38  SHAPE( SH_CIRCLE ), m_radius( 0 )
39  {}
40 
41  SHAPE_CIRCLE( const VECTOR2I& aCenter, int aRadius ) :
42  SHAPE( SH_CIRCLE ), m_radius( aRadius ), m_center( aCenter )
43  {}
44 
45  SHAPE_CIRCLE( const SHAPE_CIRCLE& aOther ) :
46  SHAPE( SH_CIRCLE ),
47  m_radius( aOther.m_radius ),
48  m_center( aOther.m_center )
49  {};
50 
52  {}
53 
54  SHAPE* Clone() const override
55  {
56  return new SHAPE_CIRCLE( *this );
57  }
58 
59  SHAPE_CIRCLE& operator=( const SHAPE_CIRCLE& ) = default;
60 
61  const BOX2I BBox( int aClearance = 0 ) const override
62  {
63  const VECTOR2I rc( m_radius + aClearance, m_radius + aClearance );
64 
65  return BOX2I( m_center - rc, rc * 2 );
66  }
67 
68  bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr ) const override
69  {
70  int minDist = aClearance + m_radius;
71  ecoord dist_sq = aSeg.SquaredDistance( m_center );
72 
73  if( dist_sq == 0 || dist_sq < (ecoord) minDist * minDist )
74  {
75  if( aActual )
76  *aActual = std::max( 0, (int) sqrt( dist_sq ) - m_radius );
77 
78  return true;
79  }
80 
81  return false;
82  }
83 
84  void SetRadius( int aRadius )
85  {
86  m_radius = aRadius;
87  }
88 
89  void SetCenter( const VECTOR2I& aCenter )
90  {
91  m_center = aCenter;
92  }
93 
94  int GetRadius() const
95  {
96  return m_radius;
97  }
98 
99  const VECTOR2I GetCenter() const
100  {
101  return m_center;
102  }
103 
104  void Move( const VECTOR2I& aVector ) override
105  {
106  m_center += aVector;
107  }
108 
109  void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
110  {
111  m_center -= aCenter;
112  m_center = m_center.Rotate( aAngle );
113  m_center += aCenter;
114  }
115 
116  bool IsSolid() const override
117  {
118  return true;
119  }
120 private:
121  int m_radius;
123 };
124 
125 #endif
bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr) const override
Function Collide()
Definition: shape_circle.h:68
void SetCenter(const VECTOR2I &aCenter)
Definition: shape_circle.h:89
BOX2< VECTOR2I > BOX2I
Definition: box2.h:522
int GetRadius() const
Definition: shape_circle.h:94
SHAPE_CIRCLE(const SHAPE_CIRCLE &aOther)
Definition: shape_circle.h:45
VECTOR2I m_center
Definition: shape_circle.h:122
void Rotate(double aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
Function Rotate.
Definition: shape_circle.h:109
const BOX2I BBox(int aClearance=0) const override
Function BBox()
Definition: shape_circle.h:61
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:37
const VECTOR2I GetCenter() const
Definition: shape_circle.h:99
void Move(const VECTOR2I &aVector) override
Definition: shape_circle.h:104
bool IsSolid() const override
Definition: shape_circle.h:116
SHAPE_CIRCLE & operator=(const SHAPE_CIRCLE &)=default
SHAPE.
Definition: shape.h:74
SHAPE * Clone() const override
Function Clone()
Definition: shape_circle.h:54
line chain (polyline)
Definition: shape.h:44
SHAPE_CIRCLE(const VECTOR2I &aCenter, int aRadius)
Definition: shape_circle.h:41
Definition: seg.h:39
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:377
void SetRadius(int aRadius)
Definition: shape_circle.h:84
VECTOR2I::extended_type ecoord
Definition: shape.h:77