KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eda_angle.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 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#ifndef EDA_ANGLE_H
21#define EDA_ANGLE_H
22
23#include <cassert>
24#include <cmath>
25#include <math/vector2d.h> // for VECTOR2I
26
27
29{
33};
34
35
37{
38public:
46 EDA_ANGLE( double aValue, EDA_ANGLE_T aAngleType )
47 {
48 switch( aAngleType )
49 {
50 case RADIANS_T:
51 m_value = aValue / DEGREES_TO_RADIANS;
52 break;
53
55 m_value = aValue / 10.0;
56 break;
57
58 default:
59 m_value = aValue;
60 }
61 }
62
68 constexpr explicit EDA_ANGLE( double aAngleInDegrees ) :
69 m_value( aAngleInDegrees )
70 {}
71
72 explicit EDA_ANGLE( const VECTOR2D& aVector )
73 {
74 if( aVector.x == 0.0 && aVector.y == 0.0 )
75 {
76 m_value = 0.0;
77 }
78 else if( aVector.y == 0.0 )
79 {
80 if( aVector.x >= 0 )
81 m_value = 0.0;
82 else
83 m_value = -180.0;
84 }
85 else if( aVector.x == 0.0 )
86 {
87 if( aVector.y >= 0.0 )
88 m_value = 90.0;
89 else
90 m_value = -90.0;
91 }
92 else if( aVector.x == aVector.y )
93 {
94 if( aVector.x >= 0.0 )
95 m_value = 45.0;
96 else
97 m_value = -180.0 + 45.0;
98 }
99 else if( aVector.x == -aVector.y )
100 {
101 if( aVector.x >= 0.0 )
102 m_value = -45.0;
103 else
104 m_value = 180.0 - 45.0;
105 }
106 else
107 {
108 *this = EDA_ANGLE( atan2( aVector.y, aVector.x ), RADIANS_T );
109 }
110 }
111
113 m_value( 0.0 )
114 {}
115
116 inline double AsDegrees() const { return m_value; }
117
118 inline int AsTenthsOfADegree() const { return KiROUND( m_value * 10.0 ); }
119
120 inline double AsRadians() const { return m_value * DEGREES_TO_RADIANS; }
121
122 static constexpr double DEGREES_TO_RADIANS = M_PI / 180.0;
123
128 bool IsCardinal() const;
129
134 bool IsCardinal90() const;
135
136 bool IsZero() const
137 {
138 return m_value == 0.0;
139 }
140
141 bool IsHorizontal() const
142 {
143 return m_value == 0.0 || m_value == 180.0;
144 }
145
146 bool IsVertical() const
147 {
148 return m_value == 90.0 || m_value == 270.0;
149 }
150
151 bool IsParallelTo( EDA_ANGLE aAngle ) const
152 {
153 EDA_ANGLE thisNormalized = *this;
154
155 // Normalize90 is inclusive on both ends [-90, +90]
156 // but we need it to be (-90, +90] for this test to work
157 thisNormalized.Normalize90();
158 if( thisNormalized.AsDegrees() == -90.0 )
159 thisNormalized = EDA_ANGLE( 90.0, DEGREES_T );
160
161 aAngle.Normalize90();
162 if( aAngle.AsDegrees() == -90.0 )
163 aAngle = EDA_ANGLE( 90.0, DEGREES_T );
164
165 return ( thisNormalized.AsDegrees() == aAngle.AsDegrees() );
166 }
167
169 {
170 return EDA_ANGLE( -AsDegrees(), DEGREES_T );
171 }
172
173 double Sin() const
174 {
175 EDA_ANGLE test = *this;
176 test.Normalize();
177
178 if( test.m_value == 0.0 || test.m_value == 180.0 )
179 return 0.0;
180 else if( test.m_value == 45.0 || test.m_value == 135.0 )
181 return M_SQRT1_2; // sqrt(2)/2
182 else if( test.m_value == 225.0 || test.m_value == 315.0 )
183 return -M_SQRT1_2;
184 else if( test.m_value == 90.0 )
185 return 1.0;
186 else if( test.m_value == 270.0 )
187 return -1.0;
188 else
189 return sin( AsRadians() );
190 }
191
192 double Cos() const
193 {
194 EDA_ANGLE test = *this;
195 test.Normalize();
196
197 if( test.m_value == 0.0 )
198 return 1.0;
199 else if( test.m_value == 180.0 )
200 return -1.0;
201 else if( test.m_value == 90.0 || test.m_value == 270.0 )
202 return 0.0;
203 else if( test.m_value == 45.0 || test.m_value == 315.0 )
204 return M_SQRT1_2; // sqrt(2)/2
205 else if( test.m_value == 135.0 || test.m_value == 225.0 )
206 return -M_SQRT1_2;
207 else
208 return cos( AsRadians() );
209 }
210
211 double Tan() const { return tan( AsRadians() ); }
212
213 static EDA_ANGLE Arccos( double x ) { return EDA_ANGLE( acos( x ), RADIANS_T ); }
214
215 static EDA_ANGLE Arcsin( double x ) { return EDA_ANGLE( asin( x ), RADIANS_T ); }
216
217 static EDA_ANGLE Arctan( double x ) { return EDA_ANGLE( atan( x ), RADIANS_T ); }
218
219 static EDA_ANGLE Arctan2( double y, double x )
220 {
221 return EDA_ANGLE( atan2( y, x ), RADIANS_T );
222 }
223
225 {
226 while( m_value < -0.0 )
227 m_value += 360.0;
228
229 while( m_value >= 360.0 )
230 m_value -= 360.0;
231
232 return *this;
233 }
234
236 {
237 EDA_ANGLE ret( *this );
238 return ret.Normalize();
239 }
240
242 {
243 while( m_value <= -360.0 )
244 m_value += 360.0;
245
246 while( m_value > 0.0 )
247 m_value -= 360.0;
248
249 return *this;
250 }
251
253 {
254 while( m_value < -90.0 )
255 m_value += 180.0;
256
257 while( m_value > 90.0 )
258 m_value -= 180.0;
259
260 return *this;
261 }
262
264 {
265 while( m_value <= -180.0 )
266 m_value += 360.0;
267
268 while( m_value > 180.0 )
269 m_value -= 360.0;
270
271 return *this;
272 }
273
275 {
276 while( m_value < -360.0 )
277 m_value += 360.0;
278
279 while( m_value >= 360.0 )
280 m_value -= 360.0;
281
282 return *this;
283 }
284
285 EDA_ANGLE KeepUpright() const;
286
287 EDA_ANGLE Round( int digits ) const
288 {
289 EDA_ANGLE angle( *this );
290 double rounded = KiROUND( angle.AsDegrees() * pow( 10.0, digits ) ) / pow( 10.0, digits );
291 angle = EDA_ANGLE( rounded, DEGREES_T );
292 return angle;
293 }
294
296 {
297 *this = EDA_ANGLE( AsDegrees() + aAngle.AsDegrees(), DEGREES_T );
298 return *this;
299 }
300
302 {
303 *this = EDA_ANGLE( AsDegrees() - aAngle.AsDegrees(), DEGREES_T );
304 return *this;
305 }
306
307private:
308 double m_value;
309
310};
311
312
313inline EDA_ANGLE operator-( const EDA_ANGLE& aAngle )
314{
315 return aAngle.Invert();
316}
317
318
319inline EDA_ANGLE operator-( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aAngleB )
320{
321 return EDA_ANGLE( aAngleA.AsDegrees() - aAngleB.AsDegrees(), DEGREES_T );
322}
323
324
325inline EDA_ANGLE operator+( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aAngleB )
326{
327 return EDA_ANGLE( aAngleA.AsDegrees() + aAngleB.AsDegrees(), DEGREES_T );
328}
329
330
331inline EDA_ANGLE operator*( const EDA_ANGLE& aAngleA, double aOperator )
332{
333 return EDA_ANGLE( aAngleA.AsDegrees() * aOperator, DEGREES_T );
334}
335
336
337inline EDA_ANGLE operator/( const EDA_ANGLE& aAngleA, double aOperator )
338{
339 return EDA_ANGLE( aAngleA.AsDegrees() / aOperator, DEGREES_T );
340}
341
342
343inline double operator/( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aOperator )
344{
345 return aAngleA.AsDegrees() / aOperator.AsDegrees();
346}
347
348
349inline bool operator==( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aAngleB )
350{
351 return aAngleA.AsDegrees() == aAngleB.AsDegrees();
352}
353
354
355inline bool operator!=( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aAngleB )
356{
357 return aAngleA.AsDegrees() != aAngleB.AsDegrees();
358}
359
360
361inline bool operator>( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aAngleB )
362{
363 return aAngleA.AsDegrees() > aAngleB.AsDegrees();
364}
365
366
367inline bool operator<( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aAngleB )
368{
369 return aAngleA.AsDegrees() < aAngleB.AsDegrees();
370}
371
372
373inline bool operator<=( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aAngleB )
374{
375 return aAngleA.AsDegrees() <= aAngleB.AsDegrees();
376}
377
378
379inline bool operator>=( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aAngleB )
380{
381 return aAngleA.AsDegrees() >= aAngleB.AsDegrees();
382}
383
384
385inline std::ostream& operator<<( std::ostream& aStream, const EDA_ANGLE& aAngle )
386{
387 return aStream << aAngle.AsDegrees();
388}
389
390
391namespace std
392{
393inline EDA_ANGLE abs( const EDA_ANGLE& aAngle )
394{
395 return EDA_ANGLE( std::abs( aAngle.AsDegrees() ), DEGREES_T );
396}
397}
398
399
400static constexpr EDA_ANGLE ANGLE_HORIZONTAL{ 0 };
401static constexpr EDA_ANGLE ANGLE_VERTICAL{ 90 };
402static constexpr EDA_ANGLE FULL_CIRCLE{ 360 };
403
404static constexpr EDA_ANGLE ANGLE_0{ 0 };
405static constexpr EDA_ANGLE ANGLE_45{ 45 };
406static constexpr EDA_ANGLE ANGLE_90{ 90 };
407static constexpr EDA_ANGLE ANGLE_135{ 135 };
408static constexpr EDA_ANGLE ANGLE_180{ 180 };
409static constexpr EDA_ANGLE ANGLE_270{ 270 };
410static constexpr EDA_ANGLE ANGLE_360{ 360 };
411
412
413#endif // EDA_ANGLE_H
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
EDA_ANGLE Normalize()
Definition: eda_angle.h:224
EDA_ANGLE NormalizeNegative()
Definition: eda_angle.h:241
double Sin() const
Definition: eda_angle.h:173
EDA_ANGLE Normalize90()
Definition: eda_angle.h:252
double Tan() const
Definition: eda_angle.h:211
constexpr EDA_ANGLE(double aAngleInDegrees)
Construct an EDA_ANGLE in degrees.
Definition: eda_angle.h:68
int AsTenthsOfADegree() const
Definition: eda_angle.h:118
bool IsParallelTo(EDA_ANGLE aAngle) const
Definition: eda_angle.h:151
double AsDegrees() const
Definition: eda_angle.h:116
EDA_ANGLE Invert() const
Definition: eda_angle.h:168
bool IsZero() const
Definition: eda_angle.h:136
bool IsHorizontal() const
Definition: eda_angle.h:141
bool IsCardinal() const
Definition: eda_angle.cpp:40
EDA_ANGLE Normalize180()
Definition: eda_angle.h:263
static EDA_ANGLE Arctan2(double y, double x)
Definition: eda_angle.h:219
EDA_ANGLE KeepUpright() const
Definition: eda_angle.cpp:23
EDA_ANGLE Normalize720()
Definition: eda_angle.h:274
double AsRadians() const
Definition: eda_angle.h:120
bool IsVertical() const
Definition: eda_angle.h:146
EDA_ANGLE & operator-=(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:301
EDA_ANGLE Round(int digits) const
Definition: eda_angle.h:287
EDA_ANGLE & operator+=(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:295
EDA_ANGLE(const VECTOR2D &aVector)
Definition: eda_angle.h:72
EDA_ANGLE(double aValue, EDA_ANGLE_T aAngleType)
Angles can be created in degrees, 1/10ths of a degree, or radians, and read as any of the angle types...
Definition: eda_angle.h:46
static constexpr double DEGREES_TO_RADIANS
Definition: eda_angle.h:122
bool IsCardinal90() const
Definition: eda_angle.cpp:54
double Cos() const
Definition: eda_angle.h:192
double m_value
value in degrees
Definition: eda_angle.h:308
static EDA_ANGLE Arctan(double x)
Definition: eda_angle.h:217
static EDA_ANGLE Arcsin(double x)
Definition: eda_angle.h:215
EDA_ANGLE Normalized() const
Definition: eda_angle.h:235
static EDA_ANGLE Arccos(double x)
Definition: eda_angle.h:213
EDA_ANGLE operator/(const EDA_ANGLE &aAngleA, double aOperator)
Definition: eda_angle.h:337
bool operator>(const EDA_ANGLE &aAngleA, const EDA_ANGLE &aAngleB)
Definition: eda_angle.h:361
bool operator>=(const EDA_ANGLE &aAngleA, const EDA_ANGLE &aAngleB)
Definition: eda_angle.h:379
EDA_ANGLE operator*(const EDA_ANGLE &aAngleA, double aOperator)
Definition: eda_angle.h:331
static constexpr EDA_ANGLE ANGLE_0
Definition: eda_angle.h:404
static constexpr EDA_ANGLE ANGLE_90
Definition: eda_angle.h:406
bool operator<=(const EDA_ANGLE &aAngleA, const EDA_ANGLE &aAngleB)
Definition: eda_angle.h:373
EDA_ANGLE_T
Definition: eda_angle.h:29
@ TENTHS_OF_A_DEGREE_T
Definition: eda_angle.h:30
@ RADIANS_T
Definition: eda_angle.h:32
@ DEGREES_T
Definition: eda_angle.h:31
EDA_ANGLE operator-(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:313
bool operator<(const EDA_ANGLE &aAngleA, const EDA_ANGLE &aAngleB)
Definition: eda_angle.h:367
static constexpr EDA_ANGLE ANGLE_VERTICAL
Definition: eda_angle.h:401
bool operator==(const EDA_ANGLE &aAngleA, const EDA_ANGLE &aAngleB)
Definition: eda_angle.h:349
static constexpr EDA_ANGLE ANGLE_HORIZONTAL
Definition: eda_angle.h:400
static constexpr EDA_ANGLE ANGLE_45
Definition: eda_angle.h:405
static constexpr EDA_ANGLE ANGLE_270
Definition: eda_angle.h:409
static constexpr EDA_ANGLE FULL_CIRCLE
Definition: eda_angle.h:402
static constexpr EDA_ANGLE ANGLE_360
Definition: eda_angle.h:410
static constexpr EDA_ANGLE ANGLE_180
Definition: eda_angle.h:408
EDA_ANGLE operator+(const EDA_ANGLE &aAngleA, const EDA_ANGLE &aAngleB)
Definition: eda_angle.h:325
static constexpr EDA_ANGLE ANGLE_135
Definition: eda_angle.h:407
bool operator!=(const EDA_ANGLE &aAngleA, const EDA_ANGLE &aAngleB)
Definition: eda_angle.h:355
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
Definition: eda_text.cpp:1308
STL namespace.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:393