trigo.h File Reference
`#include <cmath>`
`#include <math/vector2d.h>`
`#include <wx/gdicmn.h>`

Go to the source code of this file.

## Functions

bool IsPointOnSegment (const wxPoint &aSegStart, const wxPoint &aSegEnd, const wxPoint &aTestPoint)
Test if aTestPoint is on line defined by aSegStart and aSegEnd. More...

bool SegmentIntersectsSegment (const wxPoint &a_p1_l1, const wxPoint &a_p2_l1, const wxPoint &a_p1_l2, const wxPoint &a_p2_l2, wxPoint *aIntersectionPoint=nullptr)
Test if two lines intersect. More...

void RotatePoint (int *pX, int *pY, double angle)

void RotatePoint (int *pX, int *pY, int cx, int cy, double angle)

void RotatePoint (wxPoint *point, double angle)

void RotatePoint (VECTOR2I &point, double angle)

void RotatePoint (VECTOR2I &point, const VECTOR2I &centre, double angle)

void RotatePoint (wxPoint *point, const wxPoint &centre, double angle)

void RotatePoint (double *pX, double *pY, double angle)

void RotatePoint (double *pX, double *pY, double cx, double cy, double angle)

const VECTOR2I GetArcCenter (const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Determine the center of an arc or circle given three points on its circumference. More...

const VECTOR2D GetArcCenter (const VECTOR2D &aStart, const VECTOR2D &aMid, const VECTOR2D &aEnd)

const wxPoint GetArcCenter (const wxPoint &aStart, const wxPoint &aMid, const wxPoint &aEnd)

const wxPoint GetArcCenter (VECTOR2I aStart, VECTOR2I aEnd, double aAngle)

double GetArcAngle (const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Returns the subtended angle for a given arc. More...

double ArcTangente (int dy, int dx)

double EuclideanNorm (const wxPoint &vector)
Euclidean norm of a 2D vector. More...

double EuclideanNorm (const wxSize &vector)

double DistanceLinePoint (const wxPoint &linePointA, const wxPoint &linePointB, const wxPoint &referencePoint)
Compute the distance between a line and a reference point Reference: http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html. More...

bool HitTestPoints (const wxPoint &pointA, const wxPoint &pointB, double threshold)
Test, if two points are near each other. More...

double CrossProduct (const wxPoint &vectorA, const wxPoint &vectorB)
Determine the cross product. More...

bool TestSegmentHit (const wxPoint &aRefPoint, wxPoint aStart, wxPoint aEnd, int aDist)
Test if aRefPoint is with aDistance on the line defined by aStart and aEnd. More...

double GetLineLength (const wxPoint &aPointA, const wxPoint &aPointB)
Return the length of a line segment defined by aPointA and aPointB. More...

template<class T >
NormalizeAngle360Max (T Angle)
Normalize angle to be >=-360.0 and <= 360.0 Angle can be equal to -360 or +360. More...

template<class T >
NormalizeAngle360Min (T Angle)
Normalize angle to be > -360.0 and < 360.0 Angle equal to -360 or +360 are set to 0. More...

template<class T >
NormalizeAngleNeg (T Angle)
Normalize angle to be in the 0.0 . More...

template<class T >
NormalizeAnglePos (T Angle)
Normalize angle to be in the 0.0 . More...

template<class T >
void NORMALIZE_ANGLE_POS (T &Angle)

double NormalizeAngleDegreesPos (double Angle)
Normalize angle to be in the 0.0 . More...

void NORMALIZE_ANGLE_DEGREES_POS (double &Angle)

double NormalizeAngleDegrees (double Angle, double aMin, double aMax)
Normalize angle to be aMin < angle <= aMax angle is in degrees. More...

template<class T , class T2 >
Add two angles (keeping the result normalized). T2 is here. More...

template<class T >
NegateAndNormalizeAnglePos (T Angle)

template<class T >
void NEGATE_AND_NORMALIZE_ANGLE_POS (T &Angle)

template<class T >
NormalizeAngle90 (T Angle)
Normalize angle to be in the -90.0 .. 90.0 range. More...

template<class T >
void NORMALIZE_ANGLE_90 (T &Angle)

template<class T >
NormalizeAngle180 (T Angle)
Normalize angle to be in the -180.0 .. 180.0 range. More...

template<class T >
void NORMALIZE_ANGLE_180 (T &Angle)

bool InterceptsPositiveX (double aStartAngle, double aEndAngle)
Test if an arc from aStartAngle to aEndAngle crosses the positive X axis (0 degrees). More...

bool InterceptsNegativeX (double aStartAngle, double aEndAngle)
Test if an arc from aStartAngle to aEndAngle crosses the negative X axis (180 degrees). More...

double sindecideg (double r, double a)
Circle generation utility: computes r * sin(a) Where a is in decidegrees, not in radians. More...

double cosdecideg (double r, double a)
Circle generation utility: computes r * cos(a) Where a is in decidegrees, not in radians. More...

## Function Documentation

template<class T , class T2 >
 T AddAngles ( T a1, T2 a2 )
inline

Add two angles (keeping the result normalized). T2 is here.

Definition at line 324 of file trigo.h.

325 {
326  a1 += a2;
327  NORMALIZE_ANGLE_POS( a1 );
328  return a1;
329 }
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:277

References NORMALIZE_ANGLE_POS().

## ◆ ArcTangente()

 double ArcTangente ( int dy, int dx )

Definition at line 162 of file trigo.cpp.

163 {
164
165  /* gcc is surprisingly smart in optimizing these conditions in
166  a tree! */
167
168  if( dx == 0 && dy == 0 )
169  return 0;
170
171  if( dy == 0 )
172  {
173  if( dx >= 0 )
174  return 0;
175  else
176  return -1800;
177  }
178
179  if( dx == 0 )
180  {
181  if( dy >= 0 )
182  return 900;
183  else
184  return -900;
185  }
186
187  if( dx == dy )
188  {
189  if( dx >= 0 )
190  return 450;
191  else
192  return -1800 + 450;
193  }
194
195  if( dx == -dy )
196  {
197  if( dx >= 0 )
198  return -450;
199  else
200  return 1800 - 450;
201  }
202
203  // Of course dy and dx are treated as double
204  return RAD2DECIDEG( std::atan2( (double) dy, (double) dx ) );
205 }
Definition: trigo.h:225

## ◆ cosdecideg()

 double cosdecideg ( double r, double a )
inline

Circle generation utility: computes r * cos(a) Where a is in decidegrees, not in radians.

Definition at line 428 of file trigo.h.

429 {
430  return r * cos( DECIDEG2RAD( a ) );
431 }
Definition: trigo.h:224

## ◆ CrossProduct()

 double CrossProduct ( const wxPoint & vectorA, const wxPoint & vectorB )
inline

Determine the cross product.

Parameters
 vectorA Two-dimensional vector vectorB Two-dimensional vector

Definition at line 188 of file trigo.h.

189 {
190  // As before the cast is to avoid int overflow
191  return (double)vectorA.x * vectorB.y - (double)vectorA.y * vectorB.x;
192 }

Referenced by LIB_ARC::HitTest().

 double DECIDEG2RAD ( double deg )
inline

Definition at line 224 of file trigo.h.

224 { return deg * M_PI / 1800.0; }

 double DEG2RAD ( double deg )
inline

## ◆ DistanceLinePoint()

 double DistanceLinePoint ( const wxPoint & linePointA, const wxPoint & linePointB, const wxPoint & referencePoint )
inline

Compute the distance between a line and a reference point Reference: http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html.

Parameters
 linePointA Point on line linePointB Point on line referencePoint Reference point

Definition at line 151 of file trigo.h.

154 {
155  // Some of the multiple double casts are redundant. However in the previous
156  // definition the cast was (implicitly) done too late, just before
157  // the division (EuclideanNorm gives a double so from int it would
158  // be promoted); that means that the whole expression were
159  // vulnerable to overflow during int multiplications
160  return fabs( ( static_cast<double>( linePointB.x - linePointA.x ) *
161  static_cast<double>( linePointA.y - referencePoint.y ) -
162  static_cast<double>( linePointA.x - referencePoint.x ) *
163  static_cast<double>( linePointB.y - linePointA.y) )
164  / EuclideanNorm( linePointB - linePointA ) );
165 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:134

References EuclideanNorm().

## ◆ EuclideanNorm() [1/2]

 double EuclideanNorm ( const wxPoint & vector )
inline

## ◆ EuclideanNorm() [2/2]

 double EuclideanNorm ( const wxSize & vector )
inline

Definition at line 140 of file trigo.h.

141 {
142  // this is working with doubles, too
143  return hypot( vector.x, vector.y );
144 }

## ◆ GetArcAngle()

 double GetArcAngle ( const VECTOR2I & aStart, const VECTOR2I & aMid, const VECTOR2I & aEnd )

Returns the subtended angle for a given arc.

Definition at line 472 of file trigo.cpp.

473 {
474  VECTOR2I center = GetArcCenter( aStart, aMid, aEnd );
475
476  // Check if the new arc is CW or CCW
477  VECTOR2D startLine = aStart - center;
478  VECTOR2D endLine = aEnd - center;
479  double angle = RAD2DECIDEG( endLine.Angle() - startLine.Angle() );
480
481  VECTOR2D v1, v2;
482  v1 = aStart - aMid;
483  v2 = aEnd - aMid;
484  double theta = RAD2DECIDEG( v1.Angle() );
485
486  RotatePoint( &( v1.x ), &( v1.y ), theta );
487  RotatePoint( &( v2.x ), &( v2.y ), theta );
488
489  bool clockwise = ( ( v1.Angle() - v2.Angle() ) > 0 );
490
491  // Normalize the angle
492  if( clockwise && angle < 0.0 )
493  angle += 3600.0;
494  else if( !clockwise && angle > 0.0 )
495  angle -= 3600.0;
496
497  return angle;
498 }
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
Definition: trigo.h:225
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
double Angle() const
Function Angle computes the angle of the vector.
Definition: vector2d.h:313
const wxPoint GetArcCenter(VECTOR2I aStart, VECTOR2I aEnd, double aAngle)
Definition: trigo.cpp:341
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)

References VECTOR2< T >::Angle(), PNS::angle(), GetArcCenter(), RAD2DECIDEG(), and RotatePoint().

Referenced by CONVERT_TOOL::SegmentToArc().

## ◆ GetArcCenter() [1/4]

 const VECTOR2I GetArcCenter ( const VECTOR2I & aStart, const VECTOR2I & aMid, const VECTOR2I & aEnd )

Determine the center of an arc or circle given three points on its circumference.

Parameters
 aStart The starting point of the circle (equivalent to aEnd) aMid The point on the arc, half-way between aStart and aEnd aEnd The ending point of the circle (equivalent to aStart)
Returns
The center of the circle

Definition at line 430 of file trigo.cpp.

431 {
432  VECTOR2D dStart( static_cast<double>( aStart.x ), static_cast<double>( aStart.y ) );
433  VECTOR2D dMid( static_cast<double>( aMid.x ), static_cast<double>( aMid.y ) );
434  VECTOR2D dEnd( static_cast<double>( aEnd.x ), static_cast<double>( aEnd.y ) );
435  VECTOR2D dCenter = GetArcCenter( dStart, dMid, dEnd );
436
437  VECTOR2I iCenter;
438
439  iCenter.x = KiROUND( Clamp<double>( double( std::numeric_limits<int>::min() / 2.0 ),
440  dCenter.x,
441  double( std::numeric_limits<int>::max() / 2.0 ) ) );
442
443  iCenter.y = KiROUND( Clamp<double>( double( std::numeric_limits<int>::min() / 2.0 ),
444  dCenter.y,
445  double( std::numeric_limits<int>::max() / 2.0 ) ) );
446
447  return iCenter;
448 }
const wxPoint GetArcCenter(VECTOR2I aStart, VECTOR2I aEnd, double aAngle)
Definition: trigo.cpp:341
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:68

References GetArcCenter(), KiROUND(), VECTOR2< T >::x, and VECTOR2< T >::y.

## ◆ GetArcCenter() [2/4]

 const VECTOR2D GetArcCenter ( const VECTOR2D & aStart, const VECTOR2D & aMid, const VECTOR2D & aEnd )

Definition at line 366 of file trigo.cpp.

367 {
368  VECTOR2D center;
369  double yDelta_21 = aMid.y - aStart.y;
370  double xDelta_21 = aMid.x - aStart.x;
371  double yDelta_32 = aEnd.y - aMid.y;
372  double xDelta_32 = aEnd.x - aMid.x;
373
374  // This is a special case for aMid as the half-way point when aSlope = 0 and bSlope = inf
375  // or the other way around. In that case, the center lies in a straight line between
376  // aStart and aEnd
377  if( ( ( xDelta_21 == 0.0 ) && ( yDelta_32 == 0.0 ) ) ||
378  ( ( yDelta_21 == 0.0 ) && ( xDelta_32 == 0.0 ) ) )
379  {
380  center.x = ( aStart.x + aEnd.x ) / 2.0;
381  center.y = ( aStart.y + aEnd.y ) / 2.0 ;
382  return center;
383  }
384
385  // Prevent div=0 errors
386  if( xDelta_21 == 0.0 )
387  xDelta_21 = std::numeric_limits<double>::epsilon();
388
389  if( xDelta_32 == 0.0 )
390  xDelta_32 = -std::numeric_limits<double>::epsilon();
391
392  double aSlope = yDelta_21 / xDelta_21;
393  double bSlope = yDelta_32 / xDelta_32;
394
395  if( aSlope == bSlope )
396  {
397  if( aStart == aEnd )
398  {
399  // This is a special case for a 360Â° arc. In this case, the center is halfway between
400  // the midpoint and either end point
401  center.x = ( aStart.x + aMid.x ) / 2.0;
402  center.y = ( aStart.y + aMid.y ) / 2.0 ;
403  return center;
404  }
405  else
406  {
407  // If the points are colinear, the center is at infinity, so offset
408  // the slope by a minimal amount
409  // Warning: This will induce a small error in the center location
410  aSlope += std::numeric_limits<double>::epsilon();
411  bSlope -= std::numeric_limits<double>::epsilon();
412  }
413  }
414
415  // Prevent divide by zero error
416  if( aSlope == 0.0 )
417  aSlope = std::numeric_limits<double>::epsilon();
418
419  center.x = ( aSlope * bSlope * ( aStart.y - aEnd.y ) +
420  bSlope * ( aStart.x + aMid.x ) -
421  aSlope * ( aMid.x + aEnd.x ) ) / ( 2 * ( bSlope - aSlope ) );
422
423  center.y = ( ( ( aStart.x + aMid.x ) / 2.0 - center.x ) / aSlope +
424  ( aStart.y + aMid.y ) / 2.0 );
425
426  return center;
427 }

References VECTOR2< T >::x, and VECTOR2< T >::y.

## ◆ GetArcCenter() [3/4]

 const wxPoint GetArcCenter ( const wxPoint & aStart, const wxPoint & aMid, const wxPoint & aEnd )

Definition at line 451 of file trigo.cpp.

452 {
453  VECTOR2D dStart( static_cast<double>( aStart.x ), static_cast<double>( aStart.y ) );
454  VECTOR2D dMid( static_cast<double>( aMid.x ), static_cast<double>( aMid.y ) );
455  VECTOR2D dEnd( static_cast<double>( aEnd.x ), static_cast<double>( aEnd.y ) );
456  VECTOR2D dCenter = GetArcCenter( dStart, dMid, dEnd );
457
458  wxPoint iCenter;
459
460  iCenter.x = KiROUND( Clamp<double>( double( std::numeric_limits<int>::min() / 2.0 ),
461  dCenter.x,
462  double( std::numeric_limits<int>::max() / 2.0 ) ) );
463
464  iCenter.y = KiROUND( Clamp<double>( double( std::numeric_limits<int>::min() / 2.0 ),
465  dCenter.y,
466  double( std::numeric_limits<int>::max() / 2.0 ) ) );
467
468  return iCenter;
469 }
const wxPoint GetArcCenter(VECTOR2I aStart, VECTOR2I aEnd, double aAngle)
Definition: trigo.cpp:341
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:68

References GetArcCenter(), KiROUND(), VECTOR2< T >::x, and VECTOR2< T >::y.

## ◆ GetArcCenter() [4/4]

 const wxPoint GetArcCenter ( VECTOR2I aStart, VECTOR2I aEnd, double aAngle )

Definition at line 341 of file trigo.cpp.

342 {
343  if( aAngle < 0 )
344  {
345  std::swap( aStart, aEnd );
346  aAngle = abs( aAngle );
347  }
348
349  if( aAngle > 180 )
350  {
351  std::swap( aStart, aEnd );
352  aAngle = 360 - aAngle;
353  }
354
355  int chord = ( aStart - aEnd ).EuclideanNorm();
356  int r = ( chord / 2 ) / sin( aAngle * M_PI / 360.0 );
357
358  VECTOR2I vec = aEnd - aStart;
359  vec = vec.Resize( r );
360  vec = vec.Rotate( ( 180.0 - aAngle ) * M_PI / 360.0 );
361
362  return (wxPoint) ( aStart + vec );
363 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:134
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
VECTOR2< T > Resize(T aNewLength) const
Function Resize returns a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:392
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:377

References EuclideanNorm(), VECTOR2< T >::Resize(), and VECTOR2< T >::Rotate().

Referenced by GetArcAngle(), and GetArcCenter().

## ◆ GetLineLength()

 double GetLineLength ( const wxPoint & aPointA, const wxPoint & aPointB )
inline

Return the length of a line segment defined by aPointA and aPointB.

See also EuclideanNorm and Distance for the single vector or four scalar versions.

Returns
Length of a line (as double)

Definition at line 212 of file trigo.h.

213 {
214  // Implicitly casted to double
215  return hypot( aPointA.x - aPointB.x,
216  aPointA.y - aPointB.y );
217 }

## ◆ HitTestPoints()

 bool HitTestPoints ( const wxPoint & pointA, const wxPoint & pointB, double threshold )
inline

Test, if two points are near each other.

Parameters
 pointA First point pointB Second point threshold The maximum distance
Returns
True or false

Definition at line 172 of file trigo.h.

174 {
175  wxPoint vectorAB = pointB - pointA;
176
177  // Compare the distances squared. The double is needed to avoid
178  // overflow during int multiplication
179  double sqdistance = (double)vectorAB.x * vectorAB.x +
180  (double)vectorAB.y * vectorAB.y;
181
182  return sqdistance < threshold * threshold;
183 }

## ◆ InterceptsNegativeX()

 bool InterceptsNegativeX ( double aStartAngle, double aEndAngle )
inline

Test if an arc from aStartAngle to aEndAngle crosses the negative X axis (180 degrees).

Testing is performed in the quadrant 1 to quadrant 4 direction (counter-clockwise).

Parameters
 aStartAngle The arc start angle in degrees. aEndAngle The arc end angle in degrees.

Definition at line 405 of file trigo.h.

406 {
407  double end = aEndAngle;
408
409  if( aStartAngle > aEndAngle )
410  end += 360.0;
411
412  return aStartAngle < 180.0 && end > 180.0;
413 }

Referenced by BOOST_AUTO_TEST_CASE().

## ◆ InterceptsPositiveX()

 bool InterceptsPositiveX ( double aStartAngle, double aEndAngle )
inline

Test if an arc from aStartAngle to aEndAngle crosses the positive X axis (0 degrees).

Testing is performed in the quadrant 1 to quadrant 4 direction (counter-clockwise).

Parameters
 aStartAngle The arc start angle in degrees. aEndAngle The arc end angle in degrees.

Definition at line 387 of file trigo.h.

388 {
389  double end = aEndAngle;
390
391  if( aStartAngle > aEndAngle )
392  end += 360.0;
393
394  return aStartAngle < 360.0 && end > 360.0;
395 }

Referenced by BOOST_AUTO_TEST_CASE().

## ◆ IsPointOnSegment()

 bool IsPointOnSegment ( const wxPoint & aSegStart, const wxPoint & aSegEnd, const wxPoint & aTestPoint )

Test if aTestPoint is on line defined by aSegStart and aSegEnd.

This function is faster than TestSegmentHit() because aTestPoint should be exactly on the line. This works fine only for H, V and 45 degree line segments.

Parameters
 aSegStart The first point of the line segment. aSegEnd The second point of the line segment. aTestPoint The point to test.
Returns
true if the point is on the line segment.

Definition at line 42 of file trigo.cpp.

44 {
45  wxPoint vectSeg = aSegEnd - aSegStart; // Vector from S1 to S2
46  wxPoint vectPoint = aTestPoint - aSegStart; // Vector from S1 to P
47
48  // Use long long here to avoid overflow in calculations
49  if( (long long) vectSeg.x * vectPoint.y - (long long) vectSeg.y * vectPoint.x )
50  return false; /* Cross product non-zero, vectors not parallel */
51
52  if( ( (long long) vectSeg.x * vectPoint.x + (long long) vectSeg.y * vectPoint.y ) <
53  ( (long long) vectPoint.x * vectPoint.x + (long long) vectPoint.y * vectPoint.y ) )
54  return false; /* Point not on segment */
55
56  return true;
57 }

## ◆ NEGATE_AND_NORMALIZE_ANGLE_POS()

template<class T >
 void NEGATE_AND_NORMALIZE_ANGLE_POS ( T & Angle )
inline

Definition at line 342 of file trigo.h.

343 {
344  Angle = NegateAndNormalizeAnglePos( Angle );
345 }
T NegateAndNormalizeAnglePos(T Angle)
Definition: trigo.h:332

References NegateAndNormalizeAnglePos().

Referenced by CreateComponentsSection().

## ◆ NegateAndNormalizeAnglePos()

template<class T >
 T NegateAndNormalizeAnglePos ( T Angle )
inline

Definition at line 332 of file trigo.h.

333 {
334  Angle = -Angle;
335  while( Angle < 0 )
336  Angle += 3600;
337  while( Angle >= 3600 )
338  Angle -= 3600;
339  return Angle;
340 }

Referenced by NEGATE_AND_NORMALIZE_ANGLE_POS().

## ◆ NORMALIZE_ANGLE_180()

template<class T >
 void NORMALIZE_ANGLE_180 ( T & Angle )
inline

Definition at line 374 of file trigo.h.

375 {
376  Angle = NormalizeAngle180( Angle );
377 }
T NormalizeAngle180(T Angle)
Normalize angle to be in the -180.0 .. 180.0 range.
Definition: trigo.h:365

References NormalizeAngle180().

## ◆ NORMALIZE_ANGLE_90()

template<class T >
 void NORMALIZE_ANGLE_90 ( T & Angle )
inline

Definition at line 358 of file trigo.h.

359 {
360  Angle = NormalizeAngle90( Angle );
361 }
T NormalizeAngle90(T Angle)
Normalize angle to be in the -90.0 .. 90.0 range.
Definition: trigo.h:349

References NormalizeAngle90().

Referenced by GERBER_DRAW_ITEM::GetTextD_CodePrms().

## ◆ NORMALIZE_ANGLE_DEGREES_POS()

 void NORMALIZE_ANGLE_DEGREES_POS ( double & Angle )
inline

Definition at line 295 of file trigo.h.

296 {
297  Angle = NormalizeAngleDegreesPos( Angle );
298 }
double NormalizeAngleDegreesPos(double Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:285

References NormalizeAngleDegreesPos().

Referenced by DSN::SPECCTRA_DB::FromBOARD(), and DSN::SPECCTRA_DB::makeIMAGE().

## ◆ NORMALIZE_ANGLE_POS()

template<class T >
 void NORMALIZE_ANGLE_POS ( T & Angle )
inline

Definition at line 277 of file trigo.h.

278 {
279  Angle = NormalizeAnglePos( Angle );
280 }
T NormalizeAnglePos(T Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:268

References NormalizeAnglePos().

## ◆ NormalizeAngle180()

template<class T >
 T NormalizeAngle180 ( T Angle )
inline

Normalize angle to be in the -180.0 .. 180.0 range.

Definition at line 365 of file trigo.h.

366 {
367  while( Angle <= -1800 )
368  Angle += 3600;
369  while( Angle > 1800 )
370  Angle -= 3600;
371  return Angle;
372 }

## ◆ NormalizeAngle360Max()

template<class T >
 T NormalizeAngle360Max ( T Angle )
inline

Normalize angle to be >=-360.0 and <= 360.0 Angle can be equal to -360 or +360.

Definition at line 232 of file trigo.h.

233 {
234  while( Angle < -3600 )
235  Angle += 3600;
236  while( Angle > 3600 )
237  Angle -= 3600;
238  return Angle;
239 }

Referenced by FP_SHAPE::SetAngle(), and PCB_SHAPE::SetAngle().

## ◆ NormalizeAngle360Min()

template<class T >
 T NormalizeAngle360Min ( T Angle )
inline

Normalize angle to be > -360.0 and < 360.0 Angle equal to -360 or +360 are set to 0.

Definition at line 243 of file trigo.h.

244 {
245  while( Angle <= -3600 )
246  Angle += 3600;
247  while( Angle >= 3600 )
248  Angle -= 3600;
249  return Angle;
250 }

## ◆ NormalizeAngle90()

template<class T >
 T NormalizeAngle90 ( T Angle )
inline

Normalize angle to be in the -90.0 .. 90.0 range.

Definition at line 349 of file trigo.h.

350 {
351  while( Angle < -900 )
352  Angle += 1800;
353  while( Angle > 900 )
354  Angle -= 1800;
355  return Angle;
356 }

Referenced by NORMALIZE_ANGLE_90().

## ◆ NormalizeAngleDegrees()

 double NormalizeAngleDegrees ( double Angle, double aMin, double aMax )
inline

Normalize angle to be aMin < angle <= aMax angle is in degrees.

Definition at line 312 of file trigo.h.

313 {
314  while( Angle < aMin )
315  Angle += 360.0;
316  while( Angle >= aMax )
317  Angle -= 360.0;
318  return Angle;
319 }

## ◆ NormalizeAngleDegreesPos()

 double NormalizeAngleDegreesPos ( double Angle )
inline

Normalize angle to be in the 0.0 .

. 360.0 range: angle is in degrees

Definition at line 285 of file trigo.h.

286 {
287  while( Angle < 0 )
288  Angle += 360.0;
289  while( Angle >= 360.0 )
290  Angle -= 360.0;
291  return Angle;
292 }

## ◆ NormalizeAngleNeg()

template<class T >
 T NormalizeAngleNeg ( T Angle )
inline

Normalize angle to be in the 0.0 .

. -360.0 range: angle is in 1/10 degrees

Definition at line 256 of file trigo.h.

257 {
258  while( Angle <= -3600 )
259  Angle += 3600;
260  while( Angle > 0 )
261  Angle -= 3600;
262  return Angle;
263 }

## ◆ NormalizeAnglePos()

template<class T >
 T NormalizeAnglePos ( T Angle )
inline

Normalize angle to be in the 0.0 .

. 360.0 range: angle is in 1/10 degrees

Definition at line 268 of file trigo.h.

269 {
270  while( Angle < 0 )
271  Angle += 3600;
272  while( Angle >= 3600 )
273  Angle -= 3600;
274  return Angle;
275 }

 double NormalizeAngleRadiansPos ( double Angle )
inline

Definition at line 301 of file trigo.h.

302 {
303  while( Angle < 0 )
304  Angle += (2 * M_PI );
305  while( Angle >= ( 2 * M_PI ) )
306  Angle -= ( 2 * M_PI );
307  return Angle;
308 }

Referenced by GERBER_DRAW_ITEM::HitTest().

inline

Definition at line 225 of file trigo.h.

225 { return rad * 1800.0 / M_PI; }

inline

Definition at line 221 of file trigo.h.

221 { return rad * 180.0 / M_PI; }

## ◆ RotatePoint() [1/8]

 void RotatePoint ( int * pX, int * pY, double angle )

Definition at line 208 of file trigo.cpp.

209 {
210  int tmp;
211
213
214  // Cheap and dirty optimizations for 0, 90, 180, and 270 degrees.
215  if( angle == 0 )
216  return;
217
218  if( angle == 900 ) /* sin = 1, cos = 0 */
219  {
220  tmp = *pX;
221  *pX = *pY;
222  *pY = -tmp;
223  }
224  else if( angle == 1800 ) /* sin = 0, cos = -1 */
225  {
226  *pX = -*pX;
227  *pY = -*pY;
228  }
229  else if( angle == 2700 ) /* sin = -1, cos = 0 */
230  {
231  tmp = *pX;
232  *pX = -*pY;
233  *pY = tmp;
234  }
235  else
236  {
237  double fangle = DECIDEG2RAD( angle );
238  double sinus = sin( fangle );
239  double cosinus = cos( fangle );
240  double fpx = (*pY * sinus ) + (*pX * cosinus );
241  double fpy = (*pY * cosinus ) - (*pX * sinus );
242  *pX = KiROUND( fpx );
243  *pY = KiROUND( fpy );
244  }
245 }
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:277
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
Definition: trigo.h:224
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:68

References PNS::angle(), DECIDEG2RAD(), KiROUND(), and NORMALIZE_ANGLE_POS().

## ◆ RotatePoint() [2/8]

 void RotatePoint ( int * pX, int * pY, int cx, int cy, double angle )

Definition at line 248 of file trigo.cpp.

249 {
250  int ox, oy;
251
252  ox = *pX - cx;
253  oy = *pY - cy;
254
255  RotatePoint( &ox, &oy, angle );
256
257  *pX = ox + cx;
258  *pY = oy + cy;
259 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)

References PNS::angle(), and RotatePoint().

## ◆ RotatePoint() [3/8]

 void RotatePoint ( wxPoint * point, double angle )
inline

Definition at line 81 of file trigo.h.

82 {
83  RotatePoint( &point->x, &point->y, angle );
84 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)

References PNS::angle(), and RotatePoint().

## ◆ RotatePoint() [4/8]

 void RotatePoint ( VECTOR2I & point, double angle )
inline

Definition at line 86 of file trigo.h.

87 {
88  RotatePoint( &point.x, &point.y, angle );
89 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)

References PNS::angle(), RotatePoint(), VECTOR2< T >::x, and VECTOR2< T >::y.

## ◆ RotatePoint() [5/8]

 void RotatePoint ( VECTOR2I & point, const VECTOR2I & centre, double angle )

Definition at line 274 of file trigo.cpp.

275 {
276  wxPoint c( centre.x, centre.y );
277  wxPoint p( point.x, point.y );
278
279  RotatePoint(&p, c, angle);
280
281  point.x = p.x;
282  point.y = p.y;
283 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)

References PNS::angle(), RotatePoint(), VECTOR2< T >::x, and VECTOR2< T >::y.

## ◆ RotatePoint() [6/8]

 void RotatePoint ( wxPoint * point, const wxPoint & centre, double angle )

Definition at line 262 of file trigo.cpp.

263 {
264  int ox, oy;
265
266  ox = point->x - centre.x;
267  oy = point->y - centre.y;
268
269  RotatePoint( &ox, &oy, angle );
270  point->x = ox + centre.x;
271  point->y = oy + centre.y;
272 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)

References PNS::angle(), and RotatePoint().

## ◆ RotatePoint() [7/8]

 void RotatePoint ( double * pX, double * pY, double angle )

Definition at line 300 of file trigo.cpp.

301 {
302  double tmp;
303
305
306  // Cheap and dirty optimizations for 0, 90, 180, and 270 degrees.
307  if( angle == 0 )
308  return;
309
310  if( angle == 900 ) /* sin = 1, cos = 0 */
311  {
312  tmp = *pX;
313  *pX = *pY;
314  *pY = -tmp;
315  }
316  else if( angle == 1800 ) /* sin = 0, cos = -1 */
317  {
318  *pX = -*pX;
319  *pY = -*pY;
320  }
321  else if( angle == 2700 ) /* sin = -1, cos = 0 */
322  {
323  tmp = *pX;
324  *pX = -*pY;
325  *pY = tmp;
326  }
327  else
328  {
329  double fangle = DECIDEG2RAD( angle );
330  double sinus = sin( fangle );
331  double cosinus = cos( fangle );
332
333  double fpx = (*pY * sinus ) + (*pX * cosinus );
334  double fpy = (*pY * cosinus ) - (*pX * sinus );
335  *pX = fpx;
336  *pY = fpy;
337  }
338 }
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:277
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
Definition: trigo.h:224

## ◆ RotatePoint() [8/8]

 void RotatePoint ( double * pX, double * pY, double cx, double cy, double angle )

Definition at line 286 of file trigo.cpp.

287 {
288  double ox, oy;
289
290  ox = *pX - cx;
291  oy = *pY - cy;
292
293  RotatePoint( &ox, &oy, angle );
294
295  *pX = ox + cx;
296  *pY = oy + cy;
297 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)

References PNS::angle(), and RotatePoint().

## ◆ SegmentIntersectsSegment()

 bool SegmentIntersectsSegment ( const wxPoint & a_p1_l1, const wxPoint & a_p2_l1, const wxPoint & a_p1_l2, const wxPoint & a_p2_l2, wxPoint * aIntersectionPoint = `nullptr` )

Test if two lines intersect.

Parameters
 a_p1_l1 The first point of the first line. a_p2_l1 The second point of the first line. a_p1_l2 The first point of the second line. a_p2_l2 The second point of the second line. aIntersectionPoint is filled with the intersection point if it exists
Returns
bool - true if the two segments defined by four points intersect. (i.e. if the 2 segments have at least a common point)

Definition at line 61 of file trigo.cpp.

64 {
65
66  //We are forced to use 64bit ints because the internal units can overflow 32bit ints when
67  // multiplied with each other, the alternative would be to scale the units down (i.e. divide
68  // by a fixed number).
69  int64_t dX_a, dY_a, dX_b, dY_b, dX_ab, dY_ab;
70  int64_t num_a, num_b, den;
71
72  //Test for intersection within the bounds of both line segments using line equations of the
73  // form:
74  // x_k(u_k) = u_k * dX_k + x_k(0)
75  // y_k(u_k) = u_k * dY_k + y_k(0)
76  // with 0 <= u_k <= 1 and k = [ a, b ]
77
78  dX_a = int64_t{ a_p2_l1.x } - a_p1_l1.x;
79  dY_a = int64_t{ a_p2_l1.y } - a_p1_l1.y;
80  dX_b = int64_t{ a_p2_l2.x } - a_p1_l2.x;
81  dY_b = int64_t{ a_p2_l2.y } - a_p1_l2.y;
82  dX_ab = int64_t{ a_p1_l2.x } - a_p1_l1.x;
83  dY_ab = int64_t{ a_p1_l2.y } - a_p1_l1.y;
84
85  den = dY_a * dX_b - dY_b * dX_a ;
86
87  //Check if lines are parallel
88  if( den == 0 )
89  return false;
90
91  num_a = dY_ab * dX_b - dY_b * dX_ab;
92  num_b = dY_ab * dX_a - dY_a * dX_ab;
93
94  // Only compute the intersection point if requested
95  if( aIntersectionPoint )
96  {
97  *aIntersectionPoint = a_p1_l1;
98  aIntersectionPoint->x += KiROUND( dX_a * ( double )num_a / ( double )den );
99  aIntersectionPoint->y += KiROUND( dY_a * ( double )num_b / ( double )den );
100  }
101
102  if( den < 0 )
103  {
104  den = -den;
105  num_a = -num_a;
106  num_b = -num_b;
107  }
108
109  //Test sign( u_a ) and return false if negative
110  if( num_a < 0 )
111  return false;
112
113  //Test sign( u_b ) and return false if negative
114  if( num_b < 0 )
115  return false;
116
117  //Test to ensure (u_a <= 1)
118  if( num_a > den )
119  return false;
120
121  //Test to ensure (u_b <= 1)
122  if( num_b > den )
123  return false;
124
125  return true;
126 }
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:68

References KiROUND().

Referenced by EDA_RECT::Intersects().

## ◆ sindecideg()

 double sindecideg ( double r, double a )
inline

Circle generation utility: computes r * sin(a) Where a is in decidegrees, not in radians.

Definition at line 419 of file trigo.h.

420 {
421  return r * sin( DECIDEG2RAD( a ) );
422 }
Definition: trigo.h:224

## ◆ TestSegmentHit()

 bool TestSegmentHit ( const wxPoint & aRefPoint, wxPoint aStart, wxPoint aEnd, int aDist )

Test if aRefPoint is with aDistance on the line defined by aStart and aEnd.

Parameters
 aRefPoint = reference point to test aStart is the first end-point of the line segment aEnd is the second end-point of the line segment aDist = maximum distance for hit

Definition at line 129 of file trigo.cpp.

130 {
131  int xmin = aStart.x;
132  int xmax = aEnd.x;
133  int ymin = aStart.y;
134  int ymax = aEnd.y;
135  wxPoint delta = aStart - aRefPoint;
136
137  if( xmax < xmin )
138  std::swap( xmax, xmin );
139
140  if( ymax < ymin )
141  std::swap( ymax, ymin );
142
143  // First, check if we are outside of the bounding box
144  if( ( ymin - aRefPoint.y > aDist ) || ( aRefPoint.y - ymax > aDist ) )
145  return false;
146
147  if( ( xmin - aRefPoint.x > aDist ) || ( aRefPoint.x - xmax > aDist ) )
148  return false;
149
150  // Next, eliminate easy cases
151  if( aStart.x == aEnd.x && aRefPoint.y > ymin && aRefPoint.y < ymax )
152  return std::abs( delta.x ) <= aDist;
153
154  if( aStart.y == aEnd.y && aRefPoint.x > xmin && aRefPoint.x < xmax )
155  return std::abs( delta.y ) <= aDist;
156
157  SEG segment( aStart, aEnd );
158  return segment.SquaredDistance( aRefPoint ) < SEG::Square( aDist + 1 );
159 }
static SEG::ecoord Square(int a)
Definition: seg.h:116
Definition: seg.h:39

References SEG::Square(), and SEG::SquaredDistance().