KiCad PCB EDA Suite
Loading...
Searching...
No Matches
shape_rect.cpp
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) 2015 CERN
5 * Copyright (C) 2015-2022 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Tomasz Wlostowski <[email protected]>
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
27#include <geometry/shape_rect.h>
28
29bool SHAPE_RECT::Collide( const SEG& aSeg, int aClearance, int* aActual, VECTOR2I* aLocation ) const
30{
31 BOX2I bbox( BBox() );
32
33 if( bbox.Contains( aSeg.A ) )
34 {
35 if( aLocation )
36 *aLocation = aSeg.A;
37
38 if( aActual )
39 *aActual = 0;
40
41 return true;
42 }
43
44 if( bbox.Contains( aSeg.B ) )
45 {
46 if( aLocation )
47 *aLocation = aSeg.B;
48
49 if( aActual )
50 *aActual = 0;
51
52 return true;
53 }
54
55 VECTOR2I corners[] = { VECTOR2I( m_p0.x, m_p0.y ),
56 VECTOR2I( m_p0.x, m_p0.y + m_h ),
57 VECTOR2I( m_p0.x + m_w, m_p0.y + m_h ),
58 VECTOR2I( m_p0.x + m_w, m_p0.y ),
59 VECTOR2I( m_p0.x, m_p0.y ) };
60
61 SEG::ecoord closest_dist_sq = VECTOR2I::ECOORD_MAX;
62 VECTOR2I nearest;
63
64 for( int i = 0; i < 4; i++ )
65 {
66 SEG side( corners[i], corners[ i + 1] );
67 SEG::ecoord dist_sq = side.SquaredDistance( aSeg );
68
69 if( dist_sq < closest_dist_sq )
70 {
71 if ( aLocation )
72 {
73 nearest = side.NearestPoint( aSeg );
74 }
75
76 closest_dist_sq = dist_sq;
77 }
78 }
79
80 if( closest_dist_sq == 0 || closest_dist_sq < SEG::Square( aClearance ) )
81 {
82 if( aActual )
83 *aActual = sqrt( closest_dist_sq );
84
85 if( aLocation )
86 *aLocation = nearest;
87
88 return true;
89 }
90
91 return false;
92}
93
94const std::string SHAPE_RECT::Format( bool aCplusPlus ) const
95{
96 std::stringstream ss;
97
98 ss << "SHAPE_RECT( ";
99 ss << m_p0.x;
100 ss << ", ";
101 ss << m_p0.y;
102 ss << ", ";
103 ss << m_w;
104 ss << ", ";
105 ss << m_h;
106 ss << ");";
107
108 return ss.str();
109}
bool Contains(const Vec &aPoint) const
Definition: box2.h:142
Definition: seg.h:42
VECTOR2I A
Definition: seg.h:49
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:75
VECTOR2I::extended_type ecoord
Definition: seg.h:44
VECTOR2I B
Definition: seg.h:50
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition: seg.cpp:269
static SEG::ecoord Square(int a)
Definition: seg.h:123
int m_h
Height.
Definition: shape_rect.h:196
virtual const std::string Format(bool aCplusPlus=true) const override
Definition: shape_rect.cpp:94
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:194
int m_w
Width.
Definition: shape_rect.h:195
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
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:75
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588