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 SHAPE_CIRCLE& aOther ) :
50  SHAPE( SH_CIRCLE ),
51  m_circle( aOther.m_circle )
52  {};
53 
55  {}
56 
57  SHAPE* Clone() const override
58  {
59  return new SHAPE_CIRCLE( *this );
60  }
61 
62  SHAPE_CIRCLE& operator=( const SHAPE_CIRCLE& ) = default;
63 
64  const BOX2I BBox( int aClearance = 0 ) const override
65  {
66  const VECTOR2I rc( m_circle.Radius + aClearance, m_circle.Radius + aClearance );
67 
68  return BOX2I( m_circle.Center - rc, rc * 2 );
69  }
70 
71  bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
72  VECTOR2I* aLocation = nullptr ) const override
73  {
74  int minDist = aClearance + m_circle.Radius;
75  VECTOR2I pn = aSeg.NearestPoint( m_circle.Center );
76  ecoord dist_sq = ( pn - m_circle.Center ).SquaredEuclideanNorm();
77 
78  if( dist_sq == 0 || dist_sq < SEG::Square( minDist ) )
79  {
80  if( aLocation )
81  *aLocation = pn;
82 
83  if( aActual )
84  *aActual = std::max( 0, (int) sqrt( dist_sq ) - m_circle.Radius );
85 
86  return true;
87  }
88 
89  return false;
90  }
91 
92  void SetRadius( int aRadius )
93  {
94  m_circle.Radius = aRadius;
95  }
96 
97  void SetCenter( const VECTOR2I& aCenter )
98  {
99  m_circle.Center = aCenter;
100  }
101 
102  int GetRadius() const
103  {
104  return m_circle.Radius;
105  }
106 
107  const VECTOR2I GetCenter() const
108  {
109  return m_circle.Center;
110  }
111 
112  const CIRCLE GetCircle() const
113  {
114  return m_circle;
115  }
116 
117  void Move( const VECTOR2I& aVector ) override
118  {
119  m_circle.Center += aVector;
120  }
121 
122  void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
123  {
124  m_circle.Center -= aCenter;
125  m_circle.Center = m_circle.Center.Rotate( aAngle );
126  m_circle.Center += aCenter;
127  }
128 
129  bool IsSolid() const override
130  {
131  return true;
132  }
133 
134 private:
136 };
137 
138 #endif
void SetCenter(const VECTOR2I &aCenter)
Definition: shape_circle.h:97
BOX2< VECTOR2I > BOX2I
Definition: box2.h:522
int GetRadius() const
Definition: shape_circle.h:102
SHAPE_CIRCLE(const SHAPE_CIRCLE &aOther)
Definition: shape_circle.h:49
CIRCLE m_circle
Definition: shape_circle.h:135
void Rotate(double aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
Definition: shape_circle.h:122
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:64
const VECTOR2I GetCenter() const
Definition: shape_circle.h:107
static SEG::ecoord Square(int a)
Definition: seg.h:123
void Move(const VECTOR2I &aVector) override
Definition: shape_circle.h:117
bool IsSolid() const override
Definition: shape_circle.h:129
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:71
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition: seg.h:422
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:57
circle
Definition: shape.h:46
SHAPE_CIRCLE(const VECTOR2I &aCenter, int aRadius)
Definition: shape_circle.h:44
Definition: seg.h:41
VECTOR2< T > Rotate(double aAngle) const
Rotate the vector by a given angle.
Definition: vector2d.h:371
void SetRadius(int aRadius)
Definition: shape_circle.h:92
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:112