KiCad PCB EDA Suite
Loading...
Searching...
No Matches
vector_utils.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) 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 <math/vector2d.h>
21
22class SEG;
23
24namespace KIGEOM
25{
26
34/*
35 * Determine if a point is in a given direction from another point.
36 *
37 * This returns true if the vector from aFrom to aPoint is within
38 * 90 degrees of aDirection.
39 *
40 * ------> aDirection
41 *
42 * /-- aFrom
43 * O /-- aPoint
44 * O
45 *
46 * If the point is perpendicular to the direction, it is considered
47 * to NOT be in the direction (e.g. both the direction and the
48 * reversed direction would return false).
49 *
50 * @param aPoint The point to test.
51 * @param aDirection The direction vector.
52 * @param aFrom The point to test from.
53 *
54 * @return true if the point is in the direction.
55 */
56template <typename T>
57bool PointIsInDirection( const VECTOR2<T>& aPoint, const VECTOR2<T>& aDirection,
58 const VECTOR2<T>& aFrom )
59{
60 return ( aPoint - aFrom ).Dot( aDirection ) > 0;
61}
62
63
69template <typename T>
70bool PointsAreInSameDirection( const VECTOR2<T>& aPointA, const VECTOR2<T>& aPointB,
71 const VECTOR2<T>& aFrom )
72{
73 return PointIsInDirection( aPointB, aPointA - aFrom, aFrom );
74}
75
76
80bool SegIsInDirection( const SEG& aSeg, const VECTOR2I& aDirection );
81
82
92bool PointProjectsOntoSegment( const VECTOR2I& aPoint, const SEG& aSeg );
93
94
106double GetLengthRatioFromStart( const VECTOR2I& aPoint, const SEG& aSeg );
107
122double GetProjectedPointLengthRatio( const VECTOR2I& aPoint, const SEG& aSeg );
123
124
130const VECTOR2I& GetNearestEndpoint( const SEG& aSeg, const VECTOR2I& aPoint );
131
135VECTOR2I RoundGrid( const VECTOR2I& aVec, int aGridSize );
136
142VECTOR2I RoundNW( const VECTOR2I& aVec, int aGridSize );
143
149VECTOR2I RoundSE( const VECTOR2I& aVec, int aGridSize );
150
151} // namespace KIGEOM
Definition: seg.h:42
Define a general 2D-vector/point.
Definition: vector2d.h:71
Definition: oval.h:105
bool PointProjectsOntoSegment(const VECTOR2I &aPoint, const SEG &aSeg)
Determine if a point projects onto a segment.
double GetLengthRatioFromStart(const VECTOR2I &aPoint, const SEG &aSeg)
Get the ratio of the vector to a point from the segment's start, compared to the segment's length.
bool SegIsInDirection(const SEG &aSeg, const VECTOR2I &aDirection)
Determine if a segment's vector is within 90 degrees of a given direction.
bool PointsAreInSameDirection(const VECTOR2< T > &aPointA, const VECTOR2< T > &aPointB, const VECTOR2< T > &aFrom)
Check that the two given points are in the same direction from some other point.
Definition: vector_utils.h:70
VECTOR2I RoundNW(const VECTOR2I &aVec, int aGridSize)
Round a vector to the nearest grid point in the NW direction.
VECTOR2I RoundSE(const VECTOR2I &aVec, int aGridSize)
Round a vector to the nearest grid point in the SE direction.
const VECTOR2I & GetNearestEndpoint(const SEG &aSeg, const VECTOR2I &aPoint)
Get the nearest end of a segment to a point.
double GetProjectedPointLengthRatio(const VECTOR2I &aPoint, const SEG &aSeg)
Get the ratio of the vector to a point projected onto a segment from the start, relative to the segme...
VECTOR2I RoundGrid(const VECTOR2I &aVec, int aGridSize)
Round a vector to the nearest grid point in any direction.
bool PointIsInDirection(const VECTOR2< T > &aPoint, const VECTOR2< T > &aDirection, const VECTOR2< T > &aFrom)
Definition: vector_utils.h:57