2013-09-10 11:43:09 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
2021-01-06 01:40:23 +00:00
|
|
|
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-09-10 11:43:09 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __SHAPE_CIRCLE_H
|
|
|
|
#define __SHAPE_CIRCLE_H
|
|
|
|
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <geometry/shape.h>
|
2021-01-03 01:21:20 +00:00
|
|
|
#include <geometry/circle.h>
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <math/box2.h>
|
|
|
|
#include <math/vector2d.h>
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2020-07-11 09:37:22 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
class SHAPE_CIRCLE : public SHAPE
|
|
|
|
{
|
2013-09-10 11:43:09 +00:00
|
|
|
public:
|
2013-10-14 14:13:35 +00:00
|
|
|
SHAPE_CIRCLE() :
|
2020-12-12 03:43:16 +00:00
|
|
|
SHAPE( SH_CIRCLE ),
|
2021-01-03 01:21:20 +00:00
|
|
|
m_circle()
|
2013-10-14 18:40:36 +00:00
|
|
|
{}
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
SHAPE_CIRCLE( const VECTOR2I& aCenter, int aRadius ) :
|
2020-12-12 03:43:16 +00:00
|
|
|
SHAPE( SH_CIRCLE ),
|
2021-01-03 01:21:20 +00:00
|
|
|
m_circle( aCenter, aRadius )
|
2013-10-14 18:40:36 +00:00
|
|
|
{}
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2014-11-14 19:19:00 +00:00
|
|
|
SHAPE_CIRCLE( const SHAPE_CIRCLE& aOther ) :
|
2014-05-14 09:45:01 +00:00
|
|
|
SHAPE( SH_CIRCLE ),
|
2021-01-03 01:21:20 +00:00
|
|
|
m_circle( aOther.m_circle )
|
2014-05-14 09:45:01 +00:00
|
|
|
{};
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
~SHAPE_CIRCLE()
|
|
|
|
{}
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2016-09-25 17:06:49 +00:00
|
|
|
SHAPE* Clone() const override
|
2014-05-14 09:45:01 +00:00
|
|
|
{
|
|
|
|
return new SHAPE_CIRCLE( *this );
|
|
|
|
}
|
|
|
|
|
2020-01-11 21:27:38 +00:00
|
|
|
SHAPE_CIRCLE& operator=( const SHAPE_CIRCLE& ) = default;
|
|
|
|
|
2016-09-25 17:06:49 +00:00
|
|
|
const BOX2I BBox( int aClearance = 0 ) const override
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2021-01-03 01:21:20 +00:00
|
|
|
const VECTOR2I rc( m_circle.Radius + aClearance, m_circle.Radius + aClearance );
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2021-01-03 01:21:20 +00:00
|
|
|
return BOX2I( m_circle.Center - rc, rc * 2 );
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2020-09-28 22:27:33 +00:00
|
|
|
bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
|
|
|
|
VECTOR2I* aLocation = nullptr ) const override
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2021-01-03 01:21:20 +00:00
|
|
|
int minDist = aClearance + m_circle.Radius;
|
|
|
|
VECTOR2I pn = aSeg.NearestPoint( m_circle.Center );
|
|
|
|
ecoord dist_sq = ( pn - m_circle.Center ).SquaredEuclideanNorm();
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2020-09-28 22:27:33 +00:00
|
|
|
if( dist_sq == 0 || dist_sq < SEG::Square( minDist ) )
|
2020-07-02 16:06:09 +00:00
|
|
|
{
|
2020-09-28 22:27:33 +00:00
|
|
|
if( aLocation )
|
|
|
|
*aLocation = pn;
|
|
|
|
|
2020-07-02 16:06:09 +00:00
|
|
|
if( aActual )
|
2021-01-03 01:21:20 +00:00
|
|
|
*aActual = std::max( 0, (int) sqrt( dist_sq ) - m_circle.Radius );
|
2020-07-02 16:06:09 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
void SetRadius( int aRadius )
|
|
|
|
{
|
2021-01-03 01:21:20 +00:00
|
|
|
m_circle.Radius = aRadius;
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
void SetCenter( const VECTOR2I& aCenter )
|
|
|
|
{
|
2021-01-03 01:21:20 +00:00
|
|
|
m_circle.Center = aCenter;
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
int GetRadius() const
|
|
|
|
{
|
2021-01-03 01:21:20 +00:00
|
|
|
return m_circle.Radius;
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const VECTOR2I GetCenter() const
|
|
|
|
{
|
2021-01-03 01:21:20 +00:00
|
|
|
return m_circle.Center;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CIRCLE GetCircle() const
|
|
|
|
{
|
|
|
|
return m_circle;
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2016-09-25 17:06:49 +00:00
|
|
|
void Move( const VECTOR2I& aVector ) override
|
2014-11-14 18:18:31 +00:00
|
|
|
{
|
2021-01-03 01:21:20 +00:00
|
|
|
m_circle.Center += aVector;
|
2014-11-14 18:18:31 +00:00
|
|
|
}
|
|
|
|
|
2020-07-02 16:06:09 +00:00
|
|
|
void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
|
|
|
|
{
|
2021-01-03 01:21:20 +00:00
|
|
|
m_circle.Center -= aCenter;
|
|
|
|
m_circle.Center = m_circle.Center.Rotate( aAngle );
|
|
|
|
m_circle.Center += aCenter;
|
2020-07-02 16:06:09 +00:00
|
|
|
}
|
|
|
|
|
2016-09-25 17:06:49 +00:00
|
|
|
bool IsSolid() const override
|
2014-11-14 18:18:31 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2020-12-12 03:43:16 +00:00
|
|
|
|
2013-09-10 11:43:09 +00:00
|
|
|
private:
|
2021-01-03 01:21:20 +00:00
|
|
|
CIRCLE m_circle;
|
2013-09-10 11:43:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|