| 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 (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: |
| 39 |
SHAPE_CIRCLE() : |
| 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 |
|
| 59 |
~SHAPE_CIRCLE() |
| 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: |
| 140 |
CIRCLE m_circle; |
| 141 |
}; |
| 142 |
|
| 143 |
#endif |