| 1 |
/* |
| 2 |
* This program source code file is part of KiCad, a free EDA CAD application. |
| 3 |
* |
| 4 |
* Copyright (C) 2013 CERN |
| 5 |
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors. |
| 6 |
* |
| 7 |
* @author Tomasz Wlostowski <[email protected]> |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms of the GNU General Public License |
| 11 |
* as published by the Free Software Foundation; either version 2 |
| 12 |
* of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program; if not, you may find one here: |
| 21 |
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 22 |
* or you may search the http://www.gnu.org website for the version 2 license, |
| 23 |
* or you may write to the Free Software Foundation, Inc., |
| 24 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 25 |
*/ |
| 26 |
|
| 27 |
#ifndef __SHAPE_RECT_H |
| 28 |
#define __SHAPE_RECT_H |
| 29 |
|
| 30 |
#include <geometry/seg.h> |
| 31 |
#include <geometry/shape.h> |
| 32 |
#include <geometry/shape_line_chain.h> |
| 33 |
#include <math/box2.h> |
| 34 |
#include <math/vector2d.h> |
| 35 |
|
| 36 |
class SHAPE_RECT : public SHAPE |
| 37 |
{ |
| 38 |
public: |
| 39 |
/** |
| 40 |
* Create an empty (0-sized) rectangle. |
| 41 |
*/ |
| 42 |
SHAPE_RECT() : |
| 43 |
SHAPE( SH_RECT ), |
| 44 |
m_w( 0 ), |
| 45 |
m_h( 0 ) |
| 46 |
{} |
| 47 |
|
| 48 |
/** |
| 49 |
* Create a rectangle defined by top-left corner (aX0, aY0), width aW and height aH. |
| 50 |
*/ |
| 51 |
SHAPE_RECT( int aX0, int aY0, int aW, int aH ) : |
| 52 |
SHAPE( SH_RECT ), |
| 53 |
m_p0( aX0, aY0 ), |
| 54 |
m_w( aW ), |
| 55 |
m_h( aH ) |
| 56 |
{} |
| 57 |
|
| 58 |
/** |
| 59 |
* Create a rectangle defined by top-left corner aP0, width aW and height aH. |
| 60 |
*/ |
| 61 |
SHAPE_RECT( const VECTOR2I& aP0, int aW, int aH ) : |
| 62 |
SHAPE( SH_RECT ), |
| 63 |
m_p0( aP0 ), |
| 64 |
m_w( aW ), |
| 65 |
m_h( aH ) |
| 66 |
{} |
| 67 |
|
| 68 |
SHAPE_RECT( const SHAPE_RECT& aOther ) : |
| 69 |
SHAPE( SH_RECT ), |
| 70 |
m_p0( aOther.m_p0 ), |
| 71 |
m_w( aOther.m_w ), |
| 72 |
m_h( aOther.m_h ) |
| 73 |
{}; |
| 74 |
|
| 75 |
SHAPE* Clone() const override |
| 76 |
{ |
| 77 |
return new SHAPE_RECT( *this ); |
| 78 |
} |
| 79 |
|
| 80 |
/// @copydoc SHAPE::BBox() |
| 81 |
const BOX2I BBox( int aClearance = 0 ) const override |
| 82 |
{ |
| 83 |
BOX2I bbox( VECTOR2I( m_p0.x - aClearance, m_p0.y - aClearance ), |
| 84 |
VECTOR2I( m_w + 2 * aClearance, m_h + 2 * aClearance ) ); |
| 85 |
return bbox; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Return length of the diagonal of the rectangle. |
| 90 |
* |
| 91 |
* @return diagonal length |
| 92 |
*/ |
| 93 |
int Diagonal() const |
| 94 |
{ |
| 95 |
return VECTOR2I( m_w, m_h ).EuclideanNorm(); |
| 96 |
} |
| 97 |
|
| 98 |
bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const override |
| 99 |
{ |
| 100 |
return SHAPE::Collide( aShape, aClearance, aMTV ); |
| 101 |
} |
| 102 |
|
| 103 |
bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr, |
| 104 |
VECTOR2I* aLocation = nullptr ) const override |
| 105 |
{ |
| 106 |
return SHAPE::Collide( aShape, aClearance, aActual, aLocation ); |
| 107 |
} |
| 108 |
|
| 109 |
/// @copydoc SHAPE::Collide() |
| 110 |
bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr, |
| 111 |
VECTOR2I* aLocation = nullptr ) const override; |
| 112 |
|
| 113 |
/** |
| 114 |
* @return the top left corner of the rectangle. |
| 115 |
*/ |
| 116 |
const VECTOR2I& GetPosition() const |
| 117 |
{ |
| 118 |
return m_p0; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @return the size of the rectangle. |
| 123 |
*/ |
| 124 |
const VECTOR2I GetSize() const |
| 125 |
{ |
| 126 |
return VECTOR2I( m_w, m_h ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* @return the width of the rectangle. |
| 131 |
*/ |
| 132 |
const int GetWidth() const |
| 133 |
{ |
| 134 |
return m_w; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* @return the height of the rectangle. |
| 139 |
*/ |
| 140 |
const int GetHeight() const |
| 141 |
{ |
| 142 |
return m_h; |
| 143 |
} |
| 144 |
|
| 145 |
void Move( const VECTOR2I& aVector ) override |
| 146 |
{ |
| 147 |
m_p0 += aVector; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* This function has limited utility for SHAPE_RECT as non-cartesian rotations will distort |
| 152 |
* the rectangle. If you might need to handle non-90ยบ rotations then the SHAPE_RECT should |
| 153 |
* first be converted to a SHAPE_SIMPLE which can then be free-rotated. |
| 154 |
*/ |
| 155 |
void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override |
| 156 |
{ |
| 157 |
m_p0 -= aCenter; |
| 158 |
m_p0 = m_p0.Rotate( aAngle ); |
| 159 |
m_p0 += aCenter; |
| 160 |
|
| 161 |
if( abs( sin( aAngle ) ) == 1 ) |
| 162 |
std::swap( m_h, m_w ); |
| 163 |
} |
| 164 |
|
| 165 |
bool IsSolid() const override |
| 166 |
{ |
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
const SHAPE_LINE_CHAIN Outline() const |
| 171 |
{ |
| 172 |
SHAPE_LINE_CHAIN rv; |
| 173 |
rv.Append( m_p0 ); |
| 174 |
rv.Append( m_p0.x, m_p0.y + m_h ); |
| 175 |
rv.Append( m_p0.x + m_w, m_p0.y + m_h ); |
| 176 |
rv.Append( m_p0.x + m_w, m_p0.y ); |
| 177 |
rv.Append( m_p0 ); |
| 178 |
rv.SetClosed( true ); |
| 179 |
return rv; |
| 180 |
} |
| 181 |
|
| 182 |
virtual const std::string Format( ) const override; |
| 183 |
|
| 184 |
private: |
| 185 |
VECTOR2I m_p0; ///< Top-left corner |
| 186 |
int m_w; ///< Width |
| 187 |
int m_h; ///< Height |
| 188 |
}; |
| 189 |
|
| 190 |
#endif // __SHAPE_RECT_H |