KiCad PCB EDA Suite
Loading...
Searching...
No Matches
shape_rect.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 * Copyright (C) 2021-2022 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>
33#include <math/box2.h>
34#include <math/vector2d.h>
35#include <trigo.h>
36
37class SHAPE_RECT : public SHAPE
38{
39public:
44 SHAPE( SH_RECT ),
45 m_w( 0 ),
46 m_h( 0 )
47 {}
48
52 SHAPE_RECT( const BOX2I& aBox ) :
53 SHAPE( SH_RECT ),
54 m_p0( aBox.GetPosition() ),
55 m_w( aBox.GetWidth() ),
56 m_h( aBox.GetHeight() )
57 {}
58
62 SHAPE_RECT( int aX0, int aY0, int aW, int aH ) :
63 SHAPE( SH_RECT ),
64 m_p0( aX0, aY0 ),
65 m_w( aW ),
66 m_h( aH )
67 {}
68
72 SHAPE_RECT( const VECTOR2I& aP0, int aW, int aH ) :
73 SHAPE( SH_RECT ),
74 m_p0( aP0 ),
75 m_w( aW ),
76 m_h( aH )
77 {}
78
79 SHAPE_RECT( const SHAPE_RECT& aOther ) :
80 SHAPE( SH_RECT ),
81 m_p0( aOther.m_p0 ),
82 m_w( aOther.m_w ),
83 m_h( aOther.m_h )
84 {};
85
86 SHAPE* Clone() const override
87 {
88 return new SHAPE_RECT( *this );
89 }
90
92 const BOX2I BBox( int aClearance = 0 ) const override
93 {
94 BOX2I bbox( VECTOR2I( m_p0.x - aClearance, m_p0.y - aClearance ),
95 VECTOR2I( m_w + 2 * aClearance, m_h + 2 * aClearance ) );
96 return bbox;
97 }
98
104 int Diagonal() const
105 {
106 return VECTOR2I( m_w, m_h ).EuclideanNorm();
107 }
108
109 bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const override
110 {
111 return SHAPE::Collide( aShape, aClearance, aMTV );
112 }
113
114 bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
115 VECTOR2I* aLocation = nullptr ) const override
116 {
117 return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
118 }
119
121 bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
122 VECTOR2I* aLocation = nullptr ) const override;
123
127 const VECTOR2I& GetPosition() const
128 {
129 return m_p0;
130 }
131
135 const VECTOR2I GetSize() const
136 {
137 return VECTOR2I( m_w, m_h );
138 }
139
143 int GetWidth() const
144 {
145 return m_w;
146 }
147
151 int GetHeight() const
152 {
153 return m_h;
154 }
155
156 void Move( const VECTOR2I& aVector ) override
157 {
158 m_p0 += aVector;
159 }
160
166 void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
167 {
168 RotatePoint( m_p0, aCenter, aAngle );
169
170 if( abs( aAngle.Sin() ) == 1 )
171 std::swap( m_h, m_w );
172 }
173
174 bool IsSolid() const override
175 {
176 return true;
177 }
178
180 {
182 rv.Append( m_p0 );
183 rv.Append( m_p0.x, m_p0.y + m_h );
184 rv.Append( m_p0.x + m_w, m_p0.y + m_h );
185 rv.Append( m_p0.x + m_w, m_p0.y );
186 rv.Append( m_p0 );
187 rv.SetClosed( true );
188 return rv;
189 }
190
191 virtual const std::string Format( bool aCplusPlus = true ) const override;
192
193 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError,
194 ERROR_LOC aErrorLoc ) const override;
195
196private:
198 int m_w;
199 int m_h;
200};
201
202#endif // __SHAPE_RECT_H
double Sin() const
Definition: eda_angle.h:212
Definition: seg.h:42
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void SetClosed(bool aClosed)
Mark the line chain as closed (i.e.
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
Represent a set of closed polygons.
void Move(const VECTOR2I &aVector) override
Definition: shape_rect.h:156
int m_h
Height.
Definition: shape_rect.h:199
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.
Definition: shape_rect.cpp:123
virtual const std::string Format(bool aCplusPlus=true) const override
Definition: shape_rect.cpp:105
bool Collide(const SHAPE *aShape, int aClearance, VECTOR2I *aMTV) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
Definition: shape_rect.h:109
VECTOR2I m_p0
Top-left corner.
Definition: shape_rect.h:197
const SHAPE_LINE_CHAIN Outline() const
Definition: shape_rect.h:179
int Diagonal() const
Return length of the diagonal of the rectangle.
Definition: shape_rect.h:104
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
Definition: shape_rect.h:86
int m_w
Width.
Definition: shape_rect.h:198
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Definition: shape_rect.h:92
SHAPE_RECT(const VECTOR2I &aP0, int aW, int aH)
Create a rectangle defined by top-left corner aP0, width aW and height aH.
Definition: shape_rect.h:72
void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
This function has limited utility for SHAPE_RECT as non-cartesian rotations will distort the rectangl...
Definition: shape_rect.h:166
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Definition: shape_rect.h:114
SHAPE_RECT(const BOX2I &aBox)
Create a rectangle defined by a BOX2.
Definition: shape_rect.h:52
const VECTOR2I & GetPosition() const
Definition: shape_rect.h:127
const VECTOR2I GetSize() const
Definition: shape_rect.h:135
int GetWidth() const
Definition: shape_rect.h:143
int GetHeight() const
Definition: shape_rect.h:151
SHAPE_RECT(int aX0, int aY0, int aW, int aH)
Create a rectangle defined by top-left corner (aX0, aY0), width aW and height aH.
Definition: shape_rect.h:62
SHAPE_RECT()
Create an empty (0-sized) rectangle.
Definition: shape_rect.h:43
SHAPE_RECT(const SHAPE_RECT &aOther)
Definition: shape_rect.h:79
bool IsSolid() const override
Definition: shape_rect.h:174
An abstract shape on 2D plane.
Definition: shape.h:126
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
Definition: shape.h:181
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:265
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
@ SH_RECT
axis-aligned rectangle
Definition: shape.h:47
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:228
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588