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,
69  VECTOR2I* aLocation = nullptr ) const override
70  {
71  int minDist = aClearance + m_radius;
72  VECTOR2I pn = aSeg.NearestPoint( m_center );
73  ecoord dist_sq = ( pn - m_center ).SquaredEuclideanNorm();
74 
75  if( dist_sq == 0 || dist_sq < SEG::Square( minDist ) )
76  {
77  if( aLocation )
78  *aLocation = pn;
79 
80  if( aActual )
81  *aActual = std::max( 0, (int) sqrt( dist_sq ) - m_radius );
82 
83  return true;
84  }
85 
86  return false;
87  }
88 
89  void SetRadius( int aRadius )
90  {
91  m_radius = aRadius;
92  }
93 
94  void SetCenter( const VECTOR2I& aCenter )
95  {
96  m_center = aCenter;
97  }
98 
99  int GetRadius() const
100  {
101  return m_radius;
102  }
103 
104  const VECTOR2I GetCenter() const
105  {
106  return m_center;
107  }
108 
109  void Move( const VECTOR2I& aVector ) override
110  {
111  m_center += aVector;
112  }
113 
114  void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
115  {
116  m_center -= aCenter;
117  m_center = m_center.Rotate( aAngle );
118  m_center += aCenter;
119  }
120 
121  bool IsSolid() const override
122  {
123  return true;
124  }
125 private:
126  int m_radius;
128 };
129 
130 #endif
void SetCenter(const VECTOR2I &aCenter)
Definition: shape_circle.h:94
BOX2< VECTOR2I > BOX2I
Definition: box2.h:522
int GetRadius() const
Definition: shape_circle.h:99
SHAPE_CIRCLE(const SHAPE_CIRCLE &aOther)
Definition: shape_circle.h:45
VECTOR2I m_center
Definition: shape_circle.h:127
void Rotate(double aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
Function Rotate.
Definition: shape_circle.h:114
const BOX2I BBox(int aClearance=0) const override
Function BBox()
Definition: shape_circle.h:61
const VECTOR2I GetCenter() const
Definition: shape_circle.h:104
static SEG::ecoord Square(int a)
Definition: seg.h:116
void Move(const VECTOR2I &aVector) override
Definition: shape_circle.h:109
bool IsSolid() const override
Definition: shape_circle.h:121
bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Function Collide()
Definition: shape_circle.h:68
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
Definition: seg.h:395
SHAPE_CIRCLE & operator=(const SHAPE_CIRCLE &)=default
SHAPE.
Definition: shape.h:122
SHAPE * Clone() const override
Function Clone()
Definition: shape_circle.h:54
line chain (polyline)
Definition: shape.h:46
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:89
VECTOR2I::extended_type ecoord
Definition: shape.h:125