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 The 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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "geometry/half_line.h"
21
22#include <math/box2.h>
23
25
26
30bool VectorsInSameQuadrant( const VECTOR2I& aA, const VECTOR2I& aB )
31{
32 // The sign of the x and y components of the vectors must be the same
33 return ( ( aA.x >= 0 ) == ( aB.x >= 0 ) ) && ( ( aA.y >= 0 ) == ( aB.y >= 0 ) );
34}
35
36
37bool HALF_LINE::Contains( const VECTOR2I& aPoint ) const
38{
39 // Check that the point is on the right side of the ray from
40 // the start point
41 // This is quick, so we can do it first
42 if( !VectorsInSameQuadrant( m_seg.B - m_seg.A, aPoint - m_seg.A ) )
43 {
44 return false;
45 }
46
47 // Check that the point is within a distance of 1 from the
48 // infinite line of the ray
49 return m_seg.LineDistance( aPoint ) <= 1;
50}
51
52
54{
55 // Intsersection of two infinite lines
56 const SEG seg = GetContainedSeg();
57 OPT_VECTOR2I intersection = aSeg.Intersect( seg, false, true );
58
59 // Reject parallel lines
60 if( !intersection )
61 {
62 return std::nullopt;
63 }
64
65 // Check that the intersection is on the right side of the
66 // ray's start point (i.e. equal quadrants)
67 if( !VectorsInSameQuadrant( m_seg.B - m_seg.A, *intersection - m_seg.A ) )
68 {
69 return std::nullopt;
70 }
71
72 // Check that the intersection is not somewhere past the end
73 // of the segment
74 if( !aSeg.Contains( *intersection ) )
75 {
76 return std::nullopt;
77 }
78
79 return intersection;
80}
81
83{
84 // Intsersection of two infinite lines
85 const SEG otherSeg = aOther.GetContainedSeg();
86 OPT_VECTOR2I intersection = m_seg.Intersect( otherSeg, false, true );
87
88 // Reject parallel lines
89 if( !intersection )
90 {
91 return std::nullopt;
92 }
93
94 // Check that the intersection is on the right side of both
95 // rays' start points (i.e. equal quadrants)
96 if( !VectorsInSameQuadrant( m_seg.B - m_seg.A, *intersection - m_seg.A )
97 || !VectorsInSameQuadrant( aOther.m_seg.B - aOther.m_seg.A,
98 *intersection - aOther.m_seg.A ) )
99 {
100 return std::nullopt;
101 }
102
103 return intersection;
104}
105
107{
108 // Same as the SEG implementation, but without the early return
109 // if the point isn't on the segment.
110
111 // Inlined for performance reasons
112 VECTOR2L d( m_seg.B.x - m_seg.A.x, m_seg.B.y - m_seg.A.y );
113 SEG::ecoord l_squared( d.x * d.x + d.y * d.y );
114
115 if( l_squared == 0 )
116 return m_seg.A;
117
118 SEG::ecoord t = d.Dot( aPoint - m_seg.A );
119
120 if( t < 0 )
121 return m_seg.A;
122
123 SEG::ecoord xp = rescale( t, (SEG::ecoord) d.x, l_squared );
124 SEG::ecoord yp = rescale( t, (SEG::ecoord) d.y, l_squared );
125
126 return VECTOR2<SEG::ecoord>( m_seg.A.x + xp, m_seg.A.y + yp );
127}
OPT_VECTOR2I Intersect(const SEG &aSeg) const
Definition half_line.cpp:53
const SEG & GetContainedSeg() const
Gets the (one of the infinite number of) segments that the ray passes through.
Definition half_line.h:83
HALF_LINE(const SEG &aSeg)
Construct a ray from a segment - the ray will start at the segment's A point and extend infinitely in...
Definition half_line.h:43
SEG m_seg
Internally, we can represent a just a segment that the ray passes through.
Definition half_line.h:87
bool Contains(const VECTOR2I &aPoint) const
Definition half_line.cpp:37
VECTOR2I NearestPoint(const VECTOR2I &aPoint) const
Get the nearest point on the ray to the given point.
Definition seg.h:38
VECTOR2I A
Definition seg.h:45
VECTOR2I::extended_type ecoord
Definition seg.h:40
VECTOR2I B
Definition seg.h:46
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:442
bool Contains(const SEG &aSeg) const
Definition seg.h:320
Define a general 2D-vector/point.
Definition vector2d.h:67
constexpr extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition vector2d.h:542
bool VectorsInSameQuadrant(const VECTOR2I &aA, const VECTOR2I &aB)
Check if two vectors point into the same quadrant.
Definition half_line.cpp:30
std::optional< VECTOR2I > OPT_VECTOR2I
Definition seg.h:35
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:135
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< int64_t > VECTOR2L
Definition vector2d.h:684