KiCad PCB EDA Suite
Loading...
Searching...
No Matches
half_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
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include "geometry/half_line.h"
25
26#include <math/box2.h>
27
29
30
34bool VectorsInSameQuadrant( const VECTOR2I& aA, const VECTOR2I& aB )
35{
36 // The sign of the x and y components of the vectors must be the same
37 return ( ( aA.x >= 0 ) == ( aB.x >= 0 ) ) && ( ( aA.y >= 0 ) == ( aB.y >= 0 ) );
38}
39
40
41bool HALF_LINE::Contains( const VECTOR2I& aPoint ) const
42{
43 // Check that the point is on the right side of the ray from
44 // the start point
45 // This is quick, so we can do it first
46 if( !VectorsInSameQuadrant( m_seg.B - m_seg.A, aPoint - m_seg.A ) )
47 {
48 return false;
49 }
50
51 // Check that the point is within a distance of 1 from the
52 // infinite line of the ray
53 return m_seg.LineDistance( aPoint ) <= 1;
54}
55
56
58{
59 // Intsersection of two infinite lines
60 const SEG seg = GetContainedSeg();
61 OPT_VECTOR2I intersection = aSeg.Intersect( seg, false, true );
62
63 // Reject parallel lines
64 if( !intersection )
65 {
66 return std::nullopt;
67 }
68
69 // Check that the intersection is on the right side of the
70 // ray's start point (i.e. equal quadrants)
71 if( !VectorsInSameQuadrant( m_seg.B - m_seg.A, *intersection - m_seg.A ) )
72 {
73 return std::nullopt;
74 }
75
76 // Check that the intersection is not somewhere past the end
77 // of the segment
78 if( !aSeg.Contains( *intersection ) )
79 {
80 return std::nullopt;
81 }
82
83 return intersection;
84}
85
87{
88 // Intsersection of two infinite lines
89 const SEG otherSeg = aOther.GetContainedSeg();
90 OPT_VECTOR2I intersection = m_seg.Intersect( otherSeg, false, true );
91
92 // Reject parallel lines
93 if( !intersection )
94 {
95 return std::nullopt;
96 }
97
98 // Check that the intersection is on the right side of both
99 // rays' start points (i.e. equal quadrants)
100 if( !VectorsInSameQuadrant( m_seg.B - m_seg.A, *intersection - m_seg.A )
101 || !VectorsInSameQuadrant( aOther.m_seg.B - aOther.m_seg.A,
102 *intersection - aOther.m_seg.A ) )
103 {
104 return std::nullopt;
105 }
106
107 return intersection;
108}
109
111{
112 // Same as the SEG implementation, but without the early return
113 // if the point isn't on the segment.
114
115 // Inlined for performance reasons
116 VECTOR2L d( m_seg.B.x - m_seg.A.x, m_seg.B.y - m_seg.A.y );
117 SEG::ecoord l_squared( d.x * d.x + d.y * d.y );
118
119 if( l_squared == 0 )
120 return m_seg.A;
121
122 SEG::ecoord t = d.Dot( aPoint - m_seg.A );
123
124 if( t < 0 )
125 return m_seg.A;
126
127 SEG::ecoord xp = rescale( t, (SEG::ecoord) d.x, l_squared );
128 SEG::ecoord yp = rescale( t, (SEG::ecoord) d.y, l_squared );
129
130 return VECTOR2<SEG::ecoord>( m_seg.A.x + xp, m_seg.A.y + yp );
131}
OPT_VECTOR2I Intersect(const SEG &aSeg) const
Definition: half_line.cpp:57
const SEG & GetContainedSeg() const
Gets the (one of the infinite number of) segments that the ray passes through.
Definition: half_line.h:87
SEG m_seg
Internally, we can represent a just a segment that the ray passes through.
Definition: half_line.h:91
bool Contains(const VECTOR2I &aPoint) const
Definition: half_line.cpp:41
VECTOR2I NearestPoint(const VECTOR2I &aPoint) const
Get the nearest point on the ray to the given point.
Definition: half_line.cpp:110
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::extended_type ecoord
Definition: seg.h:44
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
bool VectorsInSameQuadrant(const VECTOR2I &aA, const VECTOR2I &aB)
Check if two vectors point into the same quadrant.
Definition: half_line.cpp:34
std::optional< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:39
Utility functions for working with shapes.
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition: util.h:153