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 (C) 2018-2023 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#ifndef TRIGO_H
25#define TRIGO_H
26
27#include <cmath>
28#include <math/vector2d.h>
29#include <geometry/eda_angle.h>
30
43bool IsPointOnSegment( const VECTOR2I& aSegStart, const VECTOR2I& aSegEnd,
44 const VECTOR2I& aTestPoint );
45
57bool SegmentIntersectsSegment( const VECTOR2I& a_p1_l1, const VECTOR2I& a_p2_l1,
58 const VECTOR2I& a_p1_l2, const VECTOR2I& a_p2_l2,
59 VECTOR2I* aIntersectionPoint = nullptr );
60
64void RotatePoint( int *pX, int *pY, const EDA_ANGLE& aAngle );
65
69inline void RotatePoint( VECTOR2I& point, const EDA_ANGLE& aAngle )
70{
71 RotatePoint( &point.x, &point.y, aAngle );
72}
73
77inline VECTOR2I GetRotated( const VECTOR2I& aVector, const EDA_ANGLE& aAngle )
78{
79 VECTOR2I result = aVector;
80 RotatePoint( &result.x, &result.y, aAngle );
81 return result;
82}
83
87void RotatePoint( int *pX, int *pY, int cx, int cy, const EDA_ANGLE& aAngle );
88
92inline void RotatePoint( VECTOR2I& aPoint, const VECTOR2I& aCentre, const EDA_ANGLE& aAngle )
93{
94 RotatePoint( &aPoint.x, &aPoint.y, aCentre.x, aCentre.y, aAngle );
95}
96
100inline VECTOR2I GetRotated( const VECTOR2I& aVector, const VECTOR2I& aCentre,
101 const EDA_ANGLE& aAngle )
102{
103 VECTOR2I result = aVector;
104 RotatePoint( &result.x, &result.y, aCentre.x, aCentre.y, aAngle );
105 return result;
106}
107
108
112void RotatePoint( double* pX, double* pY, const EDA_ANGLE& aAngle );
113
114inline void RotatePoint( VECTOR2D& point, const EDA_ANGLE& aAngle )
115{
116 RotatePoint( &point.x, &point.y, aAngle );
117}
118
119void RotatePoint( double* pX, double* pY, double cx, double cy, const EDA_ANGLE& aAngle );
120
121inline void RotatePoint( VECTOR2D& point, const VECTOR2D& aCenter, const EDA_ANGLE& aAngle )
122{
123 RotatePoint( &point.x, &point.y, aCenter.x, aCenter.y, aAngle );
124}
125
134const VECTOR2I CalcArcCenter( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd );
135const VECTOR2D CalcArcCenter( const VECTOR2D& aStart, const VECTOR2D& aMid, const VECTOR2D& aEnd );
136const VECTOR2D CalcArcCenter( const VECTOR2D& aStart, const VECTOR2D& aEnd,
137 const EDA_ANGLE& aAngle );
138
151const VECTOR2I CalcArcMid( const VECTOR2I& aStart, const VECTOR2I& aEnd, const VECTOR2I& aCenter,
152 bool aMinArcAngle = true );
153
162bool TestSegmentHit( const VECTOR2I& aRefPoint, const VECTOR2I& aStart, const VECTOR2I& aEnd,
163 int aDist );
164
165// These are the usual degrees <-> radians conversion routines
166inline double DEG2RAD( double deg ) { return deg * M_PI / 180.0; }
167inline double RAD2DEG( double rad ) { return rad * 180.0 / M_PI; }
168
169// These are the same *but* work with the internal 'decidegrees' unit
170inline double RAD2DECIDEG( double rad ) { return rad * 1800.0 / M_PI; }
171
172/* These are templated over T (and not simply double) because Eeschema
173 is still using int for angles in some place */
174
178template <class T> inline T NormalizeAnglePos( T Angle )
179{
180 while( Angle < 0 )
181 Angle += 3600;
182 while( Angle >= 3600 )
183 Angle -= 3600;
184 return Angle;
185}
186
187template <class T> inline void NORMALIZE_ANGLE_POS( T& Angle )
188{
189 Angle = NormalizeAnglePos( Angle );
190}
191
192
196template <class T> inline T NormalizeAngle180( T Angle )
197{
198 while( Angle <= -1800 )
199 Angle += 3600;
200
201 while( Angle > 1800 )
202 Angle -= 3600;
203
204 return Angle;
205}
206
215inline bool InterceptsPositiveX( double aStartAngle, double aEndAngle )
216{
217 double end = aEndAngle;
218
219 if( aStartAngle > aEndAngle )
220 end += 360.0;
221
222 return aStartAngle < 360.0 && end > 360.0;
223}
224
233inline bool InterceptsNegativeX( double aStartAngle, double aEndAngle )
234{
235 double end = aEndAngle;
236
237 if( aStartAngle > aEndAngle )
238 end += 360.0;
239
240 return aStartAngle < 180.0 && end > 180.0;
241}
242
243#endif
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:209
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:77
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:175
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:229
bool InterceptsPositiveX(double aStartAngle, double aEndAngle)
Test if an arc from aStartAngle to aEndAngle crosses the positive X axis (0 degrees).
Definition: trigo.h:215
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:187
bool InterceptsNegativeX(double aStartAngle, double aEndAngle)
Test if an arc from aStartAngle to aEndAngle crosses the negative X axis (180 degrees).
Definition: trigo.h:233
T NormalizeAnglePos(T Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:178
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:107
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:89
double RAD2DEG(double rad)
Definition: trigo.h:167
double DEG2RAD(double deg)
Definition: trigo.h:166
T NormalizeAngle180(T Angle)
Normalize angle to be in the -180.0 .
Definition: trigo.h:196
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:521
double RAD2DECIDEG(double rad)
Definition: trigo.h:170