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  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #ifndef __SHAPE_CIRCLE_H
27 #define __SHAPE_CIRCLE_H
28 
29 #include <geometry/shape.h>
30 #include <geometry/circle.h>
31 #include <math/box2.h>
32 #include <math/vector2d.h>
33 
34 #include <algorithm>
35 
36 class SHAPE_CIRCLE : public SHAPE
37 {
38 public:
40  SHAPE( SH_CIRCLE ),
41  m_circle()
42  {}
43 
44  SHAPE_CIRCLE( const VECTOR2I& aCenter, int aRadius ) :
45  SHAPE( SH_CIRCLE ),
46  m_circle( aCenter, aRadius )
47  {}
48 
49  SHAPE_CIRCLE( const CIRCLE& aCircle ) :
50  SHAPE( SH_CIRCLE ),
51  m_circle( aCircle )
52  {}
53 
54  SHAPE_CIRCLE( const SHAPE_CIRCLE& aOther ) :
55  SHAPE( SH_CIRCLE ),
56  m_circle( aOther.m_circle )
57  {};
58 
60  {}
61 
62  SHAPE* Clone() const override
63  {
64  return new SHAPE_CIRCLE( *this );
65  }
66 
67  SHAPE_CIRCLE& operator=( const SHAPE_CIRCLE& ) = default;
68 
69  const BOX2I BBox( int aClearance = 0 ) const override
70  {
71  const VECTOR2I rc( m_circle.Radius + aClearance, m_circle.Radius + aClearance );
72 
73  return BOX2I( m_circle.Center - rc, rc * 2 );
74  }
75 
76  bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
77  VECTOR2I* aLocation = nullptr ) const override
78  {
79  int minDist = aClearance + m_circle.Radius;
80  VECTOR2I pn = aSeg.NearestPoint( m_circle.Center );
81  ecoord dist_sq = ( pn - m_circle.Center ).SquaredEuclideanNorm();
82 
83  if( dist_sq == 0 || dist_sq < SEG::Square( minDist ) )
84  {
85  if( aLocation )
86  *aLocation = pn;
87 
88  if( aActual )
89  *aActual = std::max( 0, (int) sqrt( dist_sq ) - m_circle.Radius );
90 
91  return true;
92  }
93 
94  return false;
95  }
96 
97  void SetRadius( int aRadius )
98  {
99  m_circle.Radius = aRadius;
100  }
101 
102  void SetCenter( const VECTOR2I& aCenter )
103  {
104  m_circle.Center = aCenter;
105  }
106 
107  int GetRadius() const
108  {
109  return m_circle.Radius;
110  }
111 
112  const VECTOR2I GetCenter() const
113  {
114  return m_circle.Center;
115  }
116 
117  const CIRCLE GetCircle() const
118  {
119  return m_circle;
120  }
121 
122  void Move( const VECTOR2I& aVector ) override
123  {
124  m_circle.Center += aVector;
125  }
126 
127  void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
128  {
129  m_circle.Center -= aCenter;
130  m_circle.Center = m_circle.Center.Rotate( aAngle );
131  m_circle.Center += aCenter;
132  }
133 
134  bool IsSolid() const override
135  {
136  return true;
137  }
138 
139 private:
141 };
142 
143 #endif
void SetCenter(const VECTOR2I &aCenter)
Definition: shape_circle.h:102
BOX2< VECTOR2I > BOX2I
Definition: box2.h:512
int GetRadius() const
Definition: shape_circle.h:107
SHAPE_CIRCLE(const SHAPE_CIRCLE &aOther)
Definition: shape_circle.h:54
CIRCLE m_circle
Definition: shape_circle.h:140
void Rotate(double aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
Definition: shape_circle.h:127
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Definition: shape_circle.h:69
const VECTOR2I GetCenter() const
Definition: shape_circle.h:112
static SEG::ecoord Square(int a)
Definition: seg.h:122
void Move(const VECTOR2I &aVector) override
Definition: shape_circle.h:122
bool IsSolid() const override
Definition: shape_circle.h:134
bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the segment aSeg than aClearance,...
Definition: shape_circle.h:76
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition: seg.cpp:227
SHAPE_CIRCLE & operator=(const SHAPE_CIRCLE &)=default
int Radius
Public to make access simpler.
Definition: circle.h:36
An abstract shape on 2D plane.
Definition: shape.h:116
Class Circle Represents basic circle geometry with utility geometry functions.
Definition: circle.h:33
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
Definition: shape_circle.h:62
circle
Definition: shape.h:46
SHAPE_CIRCLE(const VECTOR2I &aCenter, int aRadius)
Definition: shape_circle.h:44
Definition: seg.h:40
VECTOR2< T > Rotate(double aAngle) const
Rotate the vector by a given angle.
Definition: vector2d.h:371
SHAPE_CIRCLE(const CIRCLE &aCircle)
Definition: shape_circle.h:49
void SetRadius(int aRadius)
Definition: shape_circle.h:97
VECTOR2I::extended_type ecoord
Definition: shape.h:236
VECTOR2I Center
Public to make access simpler.
Definition: circle.h:37
const CIRCLE GetCircle() const
Definition: shape_circle.h:117