KiCad PCB EDA Suite
Loading...
Searching...
No Matches
trigo.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 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#ifndef TRIGO_H
21#define TRIGO_H
22
23#include <cmath>
24#include <math/vector2d.h>
25#include <geometry/eda_angle.h>
26
39bool IsPointOnSegment( const VECTOR2I& aSegStart, const VECTOR2I& aSegEnd,
40 const VECTOR2I& aTestPoint );
41
53bool SegmentIntersectsSegment( const VECTOR2I& a_p1_l1, const VECTOR2I& a_p2_l1,
54 const VECTOR2I& a_p1_l2, const VECTOR2I& a_p2_l2,
55 VECTOR2I* aIntersectionPoint = nullptr );
56
60void RotatePoint( int *pX, int *pY, const EDA_ANGLE& aAngle );
61
65inline void RotatePoint( VECTOR2I& point, const EDA_ANGLE& aAngle )
66{
67 RotatePoint( &point.x, &point.y, aAngle );
68}
69
73inline VECTOR2I GetRotated( const VECTOR2I& aVector, const EDA_ANGLE& aAngle )
74{
75 VECTOR2I result = aVector;
76 RotatePoint( &result.x, &result.y, aAngle );
77 return result;
78}
79
83void RotatePoint( int *pX, int *pY, int cx, int cy, const EDA_ANGLE& aAngle );
84
88inline void RotatePoint( VECTOR2I& aPoint, const VECTOR2I& aCentre, const EDA_ANGLE& aAngle )
89{
90 RotatePoint( &aPoint.x, &aPoint.y, aCentre.x, aCentre.y, aAngle );
91}
92
96inline VECTOR2I GetRotated( const VECTOR2I& aVector, const VECTOR2I& aCentre,
97 const EDA_ANGLE& aAngle )
98{
99 VECTOR2I result = aVector;
100 RotatePoint( &result.x, &result.y, aCentre.x, aCentre.y, aAngle );
101 return result;
102}
103
104
108void RotatePoint( double* pX, double* pY, const EDA_ANGLE& aAngle );
109
110inline void RotatePoint( VECTOR2D& point, const EDA_ANGLE& aAngle )
111{
112 RotatePoint( &point.x, &point.y, aAngle );
113}
114
115void RotatePoint( double* pX, double* pY, double cx, double cy, const EDA_ANGLE& aAngle );
116
117inline void RotatePoint( VECTOR2D& point, const VECTOR2D& aCenter, const EDA_ANGLE& aAngle )
118{
119 RotatePoint( &point.x, &point.y, aCenter.x, aCenter.y, aAngle );
120}
121
125inline VECTOR2D GetRotated( const VECTOR2D& aVector, const EDA_ANGLE& aAngle )
126{
127 VECTOR2D result = aVector;
128 RotatePoint( &result.x, &result.y, aAngle );
129 return result;
130}
131
140const VECTOR2I CalcArcCenter( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd );
141const VECTOR2D CalcArcCenter( const VECTOR2D& aStart, const VECTOR2D& aMid, const VECTOR2D& aEnd );
142const VECTOR2D CalcArcCenter( const VECTOR2D& aStart, const VECTOR2D& aEnd,
143 const EDA_ANGLE& aAngle );
144
157const VECTOR2I CalcArcMid( const VECTOR2I& aStart, const VECTOR2I& aEnd, const VECTOR2I& aCenter,
158 bool aMinArcAngle = true );
159
168bool TestSegmentHit( const VECTOR2I& aRefPoint, const VECTOR2I& aStart, const VECTOR2I& aEnd,
169 int aDist );
170
171// These are the usual degrees <-> radians conversion routines
172inline double DEG2RAD( double deg ) { return deg * M_PI / 180.0; }
173inline double RAD2DEG( double rad ) { return rad * 180.0 / M_PI; }
174
175// These are the same *but* work with the internal 'decidegrees' unit
176inline double RAD2DECIDEG( double rad ) { return rad * 1800.0 / M_PI; }
177
178/* These are templated over T (and not simply double) because Eeschema
179 is still using int for angles in some place */
180
184template <class T> inline T NormalizeAnglePos( T Angle )
185{
186 while( Angle < 0 )
187 Angle += 3600;
188 while( Angle >= 3600 )
189 Angle -= 3600;
190 return Angle;
191}
192
193template <class T> inline void NORMALIZE_ANGLE_POS( T& Angle )
194{
195 Angle = NormalizeAnglePos( Angle );
196}
197
198
202template <class T> inline T NormalizeAngle180( T Angle )
203{
204 while( Angle <= -1800 )
205 Angle += 3600;
206
207 while( Angle > 1800 )
208 Angle -= 3600;
209
210 return Angle;
211}
212
221inline bool InterceptsPositiveX( double aStartAngle, double aEndAngle )
222{
223 double end = aEndAngle;
224
225 if( aStartAngle > aEndAngle )
226 end += 360.0;
227
228 return aStartAngle < 360.0 && end > 360.0;
229}
230
239inline bool InterceptsNegativeX( double aStartAngle, double aEndAngle )
240{
241 double end = aEndAngle;
242
243 if( aStartAngle > aEndAngle )
244 end += 360.0;
245
246 return aStartAngle < 180.0 && end > 180.0;
247}
248
249#endif
VECTOR2I end
wxString result
Test unit parsing edge cases and error handling.
#define M_PI
const VECTOR2I CalcArcMid(const VECTOR2I &aStart, const VECTOR2I &aEnd, const VECTOR2I &aCenter, bool aMinArcAngle=true)
Return the middle point of an arc, half-way between aStart and aEnd.
Definition trigo.cpp:205
VECTOR2I GetRotated(const VECTOR2I &aVector, const EDA_ANGLE &aAngle)
Return a new VECTOR2I that is the result of rotating aVector by aAngle.
Definition trigo.h:73
bool TestSegmentHit(const VECTOR2I &aRefPoint, const VECTOR2I &aStart, const VECTOR2I &aEnd, int aDist)
Test if aRefPoint is with aDistance on the line defined by aStart and aEnd.
Definition trigo.cpp:171
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:225
bool InterceptsPositiveX(double aStartAngle, double aEndAngle)
Test if an arc from aStartAngle to aEndAngle crosses the positive X axis (0 degrees).
Definition trigo.h:221
void NORMALIZE_ANGLE_POS(T &Angle)
Definition trigo.h:193
bool InterceptsNegativeX(double aStartAngle, double aEndAngle)
Test if an arc from aStartAngle to aEndAngle crosses the negative X axis (180 degrees).
Definition trigo.h:239
T NormalizeAnglePos(T Angle)
Normalize angle to be in the 0.0 .
Definition trigo.h:184
bool SegmentIntersectsSegment(const VECTOR2I &a_p1_l1, const VECTOR2I &a_p2_l1, const VECTOR2I &a_p1_l2, const VECTOR2I &a_p2_l2, VECTOR2I *aIntersectionPoint=nullptr)
Test if two lines intersect.
Definition trigo.cpp:103
bool IsPointOnSegment(const VECTOR2I &aSegStart, const VECTOR2I &aSegEnd, const VECTOR2I &aTestPoint)
Test if aTestPoint is on line defined by aSegStart and aSegEnd.
Definition trigo.cpp:85
double RAD2DEG(double rad)
Definition trigo.h:173
double DEG2RAD(double deg)
Definition trigo.h:172
T NormalizeAngle180(T Angle)
Normalize angle to be in the -180.0 .
Definition trigo.h:202
const VECTOR2I CalcArcCenter(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Determine the center of an arc or circle given three points on its circumference.
Definition trigo.cpp:562
double RAD2DECIDEG(double rad)
Definition trigo.h:176
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682