KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[email protected]>
6 * Copyright The 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, see <https://www.gnu.org/licenses/>.
20 */
21
22#ifndef __SHAPE_CIRCLE_H
23#define __SHAPE_CIRCLE_H
24
25#include <geometry/shape.h>
26#include <geometry/circle.h>
27#include <math/box2.h>
28#include <math/vector2d.h>
29#include <trigo.h>
30
31#include <algorithm>
32
33class SHAPE_CIRCLE : public SHAPE
34{
35public:
38 m_circle()
39 {}
40
41 SHAPE_CIRCLE( const VECTOR2I& aCenter, int aRadius ) :
43 m_circle( aCenter, aRadius )
44 {}
45
46 SHAPE_CIRCLE( const CIRCLE& aCircle ) :
48 m_circle( aCircle )
49 {}
50
51 SHAPE_CIRCLE( const SHAPE_CIRCLE& aOther ) :
53 m_circle( aOther.m_circle )
54 {};
55
58
59 SHAPE* Clone() const override
60 {
61 return new SHAPE_CIRCLE( *this );
62 }
63
64 SHAPE_CIRCLE& operator=( const SHAPE_CIRCLE& ) = default;
65
66 const BOX2I BBox( int aClearance = 0 ) const override
67 {
68 const VECTOR2I rc( m_circle.Radius + aClearance, m_circle.Radius + aClearance );
69
70 return BOX2I( m_circle.Center - rc, rc * 2 );
71 }
72
73 bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
74 VECTOR2I* aLocation = nullptr ) const override
75 {
76 int minDist = aClearance + m_circle.Radius;
77 VECTOR2I pn = aSeg.NearestPoint( m_circle.Center );
78 ecoord dist_sq = ( pn - m_circle.Center ).SquaredEuclideanNorm();
79
80 if( dist_sq == 0 || dist_sq < SEG::Square( minDist ) )
81 {
82 if( aLocation )
83 {
84 if( std::vector<VECTOR2I> pts = m_circle.Intersect( aSeg );
85 !pts.empty() && dist_sq == 0 )
86 {
87 *aLocation = m_circle.Intersect( aSeg )[0];
88 }
89 else
90 {
91 *aLocation = pn;
92 }
93 }
94
95 if( aActual )
96 *aActual = std::max( 0, (int) sqrt( dist_sq ) - m_circle.Radius );
97
98 return true;
99 }
100
101 return false;
102 }
103
104 void SetRadius( int aRadius )
105 {
106 m_circle.Radius = aRadius;
107 }
108
109 void SetCenter( const VECTOR2I& aCenter )
110 {
111 m_circle.Center = aCenter;
112 }
113
114 int GetRadius() const
115 {
116 return m_circle.Radius;
117 }
118
119 const VECTOR2I GetCenter() const
120 {
121 return m_circle.Center;
122 }
123
124 const CIRCLE GetCircle() const
125 {
126 return m_circle;
127 }
128
129 void Move( const VECTOR2I& aVector ) override
130 {
131 m_circle.Center += aVector;
132 }
133
134 void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
135 {
136 RotatePoint( m_circle.Center, aCenter, aAngle );
137 }
138
139 bool IsSolid() const override
140 {
141 return true;
142 }
143
144 virtual const std::string Format( bool aCplusPlus = true ) const override;
145
146 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError,
147 ERROR_LOC aErrorLoc ) const override;
148
149private:
151};
152
153#endif
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
Represent basic circle geometry with utility geometry functions.
Definition circle.h:33
VECTOR2I Center
Public to make access simpler.
Definition circle.h:150
Definition seg.h:38
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition seg.cpp:629
static SEG::ecoord Square(int a)
Definition seg.h:119
bool IsSolid() const override
void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
const CIRCLE GetCircle() const
SHAPE_CIRCLE(const SHAPE_CIRCLE &aOther)
SHAPE_CIRCLE(const VECTOR2I &aCenter, int aRadius)
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,...
int GetRadius() const
const VECTOR2I GetCenter() const
void TransformToPolygon(SHAPE_POLY_SET &aBuffer, int aError, ERROR_LOC aErrorLoc) const override
Fills a SHAPE_POLY_SET with a polygon representation of this shape.
SHAPE_CIRCLE(const CIRCLE &aCircle)
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
void SetRadius(int aRadius)
virtual const std::string Format(bool aCplusPlus=true) const override
SHAPE_CIRCLE & operator=(const SHAPE_CIRCLE &)=default
void Move(const VECTOR2I &aVector) override
void SetCenter(const VECTOR2I &aCenter)
Represent a set of closed polygons.
VECTOR2I::extended_type ecoord
Definition shape.h:299
SHAPE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition shape.h:134
@ SH_CIRCLE
circle
Definition shape.h:46
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:225
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683