KiCad PCB EDA Suite
Loading...
Searching...
No Matches
line.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) 2024 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <geometry/line.h>
21
22OPT_VECTOR2I LINE::Intersect( const SEG& aSeg ) const
23{
24 // intersect as two lines
25 OPT_VECTOR2I intersection = aSeg.Intersect( m_seg, false, true );
26
27 if( intersection )
28 {
29 // Not parallel.
30 // That was two lines, but we need to check if the intersection is on
31 // the requested segment
32 if( aSeg.Contains( *intersection ) )
33 {
34 return intersection;
35 }
36 }
37 return std::nullopt;
38}
39
40OPT_VECTOR2I LINE::Intersect( const LINE& aOther ) const
41{
42 // Defer to the SEG implementation
43 return aOther.m_seg.Intersect( m_seg, false, true );
44}
45
46int LINE::Distance( const VECTOR2I& aPoint ) const
47{
48 // Just defer to the SEG implementation
49 return m_seg.LineDistance( aPoint );
50}
51
52VECTOR2I LINE::NearestPoint( const VECTOR2I& aPoint ) const
53{
54 // Same as the SEG implementation, but without the early return
55 // if the point isn't on the segment.
56
57 // Inlined for performance reasons
58 VECTOR2L d( m_seg.B.x - m_seg.A.x, m_seg.B.y - m_seg.A.y );
59 ecoord l_squared( d.x * d.x + d.y * d.y );
60
61 if( l_squared == 0 )
62 return m_seg.A;
63
64 ecoord t = d.Dot( aPoint - m_seg.A );
65
66 ecoord xp = rescale( t, (ecoord) d.x, l_squared );
67 ecoord yp = rescale( t, (ecoord) d.y, l_squared );
68
69 return VECTOR2<ecoord>( m_seg.A.x + xp, m_seg.A.y + yp );
70}
Definition: line.h:36
VECTOR2I::extended_type ecoord
Definition: line.h:38
SEG m_seg
Internally, we can represent a just a segment that the line passes through.
Definition: line.h:66
int Distance(const VECTOR2I &aPoint) const
Gets the distance from the line to the given point.
Definition: line.cpp:46
VECTOR2I NearestPoint(const VECTOR2I &aPoint) const
Gets the nearest point on the line to the given point.
Definition: line.cpp:52
OPT_VECTOR2I Intersect(const SEG &aOther) const
Definition: line.cpp:22
Definition: seg.h:42
VECTOR2I A
Definition: seg.h:49
int LineDistance(const VECTOR2I &aP, bool aDetermineSide=false) const
Return the closest Euclidean distance between point aP and the line defined by the ends of segment (t...
Definition: seg.cpp:428
VECTOR2I B
Definition: seg.h:50
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Compute intersection point of segment (this) with segment aSeg.
Definition: seg.cpp:254
bool Contains(const SEG &aSeg) const
Definition: seg.h:314
constexpr extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition: vector2d.h:550
std::optional< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:39
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition: util.h:153