KiCad PCB EDA Suite
trigo.h File Reference
#include <math.h>
#include <wx/gdicmn.h>
#include <math/vector2d.h>

Go to the source code of this file.

Functions

bool IsPointOnSegment (const wxPoint &aSegStart, const wxPoint &aSegEnd, const wxPoint &aTestPoint)
 Function IsPointOnSegment. More...
 
bool SegmentIntersectsSegment (const wxPoint &a_p1_l1, const wxPoint &a_p2_l1, const wxPoint &a_p1_l2, const wxPoint &a_p2_l2)
 Function SegmentIntersectsSegment. 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/circle, given three points on its circumference. 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)
 Function TestSegmentHit test for hit on line segment i.e. More...
 
double GetLineLength (const wxPoint &aPointA, const wxPoint &aPointB)
 Function GetLineLength returns the length of a line segment defined by aPointA and aPointB. More...
 
double DEG2RAD (double deg)
 
double RAD2DEG (double rad)
 
double DECIDEG2RAD (double deg)
 
double RAD2DECIDEG (double rad)
 
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 >
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 NormalizeAngleRadiansPos (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 >
AddAngles (T a1, T2 a2)
 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)
 
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

◆ AddAngles()

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

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

Definition at line 297 of file trigo.h.

298 {
299  a1 += a2;
300  NORMALIZE_ANGLE_POS( a1 );
301  return a1;
302 }
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:250

References NORMALIZE_ANGLE_POS().

Referenced by convertOblong2Segment(), CreateThermalReliefPadPolygon(), HPGL_PLOTTER::FlashPadOval(), PSLIKE_PLOTTER::FlashPadOval(), DXF_PLOTTER::FlashPadOval(), and PLOTTER::sketchOval().

◆ ArcTangente()

double ArcTangente ( int  dy,
int  dx 
)

Definition at line 170 of file trigo.cpp.

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

References RAD2DECIDEG().

Referenced by BuildCornersList_S_Shape(), LIB_ARC::CalcEdit(), LIB_ARC::CalcRadiusAngles(), DRC::checkClearancePadToPad(), AM_PRIMITIVE::ConvertShapeToPolygon(), DRC::doTrackDrc(), EDGE_MODULE::Draw(), DRAWSEGMENT::Draw(), DrawSegmentQcq(), AR_MATRIX::drawSegmentQcq(), fillArcPOLY(), DRAWSEGMENT::GetArcAngleStart(), GRCSegm(), DRAWSEGMENT::HitTest(), PCAD2KICAD::PCB_ARC::Parse(), BRDITEMS_PLOTTER::Plot_1_EdgeModule(), BRDITEMS_PLOTTER::PlotDrawSegment(), PLOTTER::segmentAsOval(), ShowBoundingBoxMicroWaveInductor(), TraceArc(), AR_MATRIX::traceArc(), TransformRoundedEndsSegmentToPolygon(), and PCB_BASE_FRAME::UpdateStatusBar().

◆ 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 363 of file trigo.h.

364 {
365  return r * cos( DECIDEG2RAD( a ) );
366 }
double DECIDEG2RAD(double deg)
Definition: trigo.h:212

References DECIDEG2RAD().

Referenced by PLOTTER::Arc(), HPGL_PLOTTER::Arc(), PDF_PLOTTER::Arc(), GERBER_PLOTTER::Arc(), PCAD2KICAD::PCB_ARC::Parse(), TraceArc(), AR_MATRIX::traceArc(), TraceCircle(), and AR_MATRIX::traceCircle().

◆ CrossProduct()

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

Determine the cross product.

Parameters
vectorATwo-dimensional vector
vectorBTwo-dimensional vector

Definition at line 175 of file trigo.h.

176 {
177  // As before the cast is to avoid int overflow
178  return (double)vectorA.x * vectorB.y - (double)vectorA.y * vectorB.x;
179 }

Referenced by LIB_ARC::BeginEdit(), and LIB_ARC::HitTest().

◆ DECIDEG2RAD()

◆ DEG2RAD()

◆ 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
linePointAPoint on line
linePointBPoint on line
referencePointReference point

Definition at line 138 of file trigo.h.

141 {
142  // Some of the multiple double casts are redundant. However in the previous
143  // definition the cast was (implicitly) done too late, just before
144  // the division (EuclideanNorm gives a double so from int it would
145  // be promoted); that means that the whole expression were
146  // vulnerable to overflow during int multiplications
147  return fabs( ( double(linePointB.x - linePointA.x) *
148  double(linePointA.y - referencePoint.y) -
149  double(linePointA.x - referencePoint.x ) *
150  double(linePointB.y - linePointA.y) )
151  / EuclideanNorm( linePointB - linePointA ) );
152 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:121

References EuclideanNorm().

Referenced by LIB_POLYLINE::AddCorner().

◆ EuclideanNorm() [1/2]

double EuclideanNorm ( const wxPoint &  vector)
inline

Euclidean norm of a 2D vector.

Parameters
vectorTwo-dimensional vector
Returns
Euclidean norm of the vector

Definition at line 121 of file trigo.h.

122 {
123  // this is working with doubles
124  return hypot( vector.x, vector.y );
125 }

Referenced by GRID_HELPER::AlignToSegment(), LIB_ARC::BeginEdit(), GRID_HELPER::BestSnapAnchor(), D_PAD::boundingRadius(), BuildCornersList_S_Shape(), PNS::DP_GATEWAYS::buildDpContinuation(), PNS::DP_GATEWAYS::BuildFromPrimitivePair(), PNS::DP_GATEWAYS::BuildGeneric(), PNS::DP_GATEWAYS::BuildOrthoProjections(), LIB_ARC::CalcRadiusAngles(), DRC::checkClearancePadToPad(), DRC::checkMarginToCircle(), Collect_TrackSegmentsToDrag(), Collide(), SHAPE_ARC::Collide(), GRID_HELPER::computeAnchors(), AM_PRIMITIVE::ConvertShapeToPolygon(), MWAVE::CreateMicrowaveInductor(), PNS::DP_PRIMITIVE_PAIR::CursorOrientation(), DistanceLinePoint(), DRC::doTrackDrc(), KIGFX::CAIRO_GAL_BASE::DrawSegment(), MODULE_EDITOR_TOOLS::EnumeratePads(), PNS::findCoupledVertices(), PNS::DIFF_PAIR_PLACER::findDpPrimitivePair(), D_PAD::GetBestAnchorPosition(), DRAWSEGMENT::HitTest(), D_PAD::HitTest(), PNS::makeGapVector(), PNS::MEANDERED_LINE::MeanderSegment(), PNS::NODE::NearestObstacle(), PNS::TOPOLOGY::NearestUnconnectedItem(), CONNECTIVITY_DATA::NearestUnconnectedTargets(), compareOriginDistance::operator()(), SHAPE_LINE_CHAIN::compareOriginDistance::operator()(), GPCB_FPL_CACHE::parseMODULE(), SHAPE_LINE_CHAIN::PathLength(), pushoutForce(), POSITION_RELATIVE_TOOL::RelativeItemSelectionMove(), PNS::LINE_PLACER::rhMarkObstacles(), PNS::DIFF_PAIR_PLACER::routeHead(), PLOTTER::segmentAsOval(), ShowBoundingBoxMicroWaveInductor(), PNS::LINE::snapDraggedCorner(), PNS::DRAGGER::startDragSegment(), TransformOvalClearanceToPolygon(), and TransformRoundedEndsSegmentToPolygon().

◆ EuclideanNorm() [2/2]

double EuclideanNorm ( const wxSize &  vector)
inline

Definition at line 127 of file trigo.h.

128 {
129  // this is working with doubles, too
130  return hypot( vector.x, vector.y );
131 }

◆ GetArcCenter()

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

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

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

Definition at line 349 of file trigo.cpp.

350 {
351  VECTOR2I center;
352  double yDelta_21 = aMid.y - aStart.y;
353  double xDelta_21 = aMid.x - aStart.x;
354  double yDelta_32 = aEnd.y - aMid.y;
355  double xDelta_32 = aEnd.x - aMid.x;
356 
357  // This is a special case for aMid as the half-way point when aSlope = 0 and bSlope = inf
358  // or the other way around. In that case, the center lies in a straight line between
359  // aStart and aEnd
360  if( ( ( xDelta_21 == 0.0 ) && ( yDelta_32 == 0.0 ) ) ||
361  ( ( yDelta_21 == 0.0 ) && ( xDelta_32 == 0.0 ) ) )
362  {
363  center.x = KiROUND( ( aStart.x + aEnd.x ) / 2.0 );
364  center.y = KiROUND( ( aStart.y + aEnd.y ) / 2.0 );
365  return center;
366  }
367 
368  // Prevent div=0 errors
369  if( xDelta_21 == 0.0 )
370  xDelta_21 = std::numeric_limits<double>::epsilon();
371 
372  if( xDelta_32 == 0.0 )
373  xDelta_32 = -std::numeric_limits<double>::epsilon();
374 
375  double aSlope = yDelta_21 / xDelta_21;
376  double bSlope = yDelta_32 / xDelta_32;
377 
378  // If the points are colinear, the center is at infinity, so offset
379  // the slope by a minimal amount
380  // Warning: This will induce a small error in the center location
381  if( yDelta_32 * xDelta_21 == yDelta_21 * xDelta_32 )
382  {
383  aSlope += std::numeric_limits<double>::epsilon();
384  bSlope -= std::numeric_limits<double>::epsilon();
385  }
386 
387  if( aSlope == 0.0 )
388  aSlope = std::numeric_limits<double>::epsilon();
389 
390  if( bSlope == 0.0 )
391  bSlope = -std::numeric_limits<double>::epsilon();
392 
393 
394  double result = ( aSlope * bSlope * ( aStart.y - aEnd.y ) +
395  bSlope * ( aStart.x + aMid.x ) -
396  aSlope * ( aMid.x + aEnd.x ) ) / ( 2 * ( bSlope - aSlope ) );
397 
398  center.x = KiROUND( Clamp<double>( double( std::numeric_limits<int>::min() / 2 ),
399  result,
400  double( std::numeric_limits<int>::max() / 2 ) ) );
401 
402  result = ( ( ( aStart.x + aMid.x ) / 2.0 - center.x ) / aSlope +
403  ( aStart.y + aMid.y ) / 2.0 );
404 
405  center.y = KiROUND( Clamp<double>( double( std::numeric_limits<int>::min() / 2 ),
406  result,
407  double( std::numeric_limits<int>::max() / 2 ) ) );
408 
409  return center;
410 }
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:118
#define max(a, b)
Definition: auxiliary.h:86
#define min(a, b)
Definition: auxiliary.h:85

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

Referenced by POINT_EDITOR::updateItem().

◆ GetLineLength()

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

Function GetLineLength returns 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 200 of file trigo.h.

201 {
202  // Implicitly casted to double
203  return hypot( aPointA.x - aPointB.x,
204  aPointA.y - aPointB.y );
205 }

Referenced by BOARD::chainMarkedSegments(), KIGFX::GERBVIEW_PAINTER::draw(), GERBER_DRAW_ITEM::Draw(), TRACK::DrawShortNetname(), FootprintWriteShape(), GERBER_DRAW_ITEM::GetBoundingBox(), SCH_LINE::GetLength(), TRACK::GetLength(), DRAWSEGMENT::GetLength(), DRAWSEGMENT::GetMsgPanelInfo(), LIB_CIRCLE::GetRadius(), DRAWSEGMENT::GetRadius(), LIB_CIRCLE::HitTest(), LIB_ARC::HitTest(), GERBER_DRAW_ITEM::HitTest(), TRACK::IsPointOnEnds(), Magnetize(), DSN::SPECCTRA_DB::makeIMAGE(), DRC_MARKER_FACTORY::NewMarker(), BRDITEMS_PLOTTER::Plot_1_EdgeModule(), BRDITEMS_PLOTTER::PlotDrawSegment(), DRC::testDrilledHoles(), and GERBER_DRAW_ITEM::ViewGetLOD().

◆ HitTestPoints()

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

Test, if two points are near each other.

Parameters
pointAFirst point
pointBSecond point
thresholdThe maximum distance
Returns
True or false

Definition at line 159 of file trigo.h.

161 {
162  wxPoint vectorAB = pointB - pointA;
163 
164  // Compare the distances squared. The double is needed to avoid
165  // overflow during int multiplication
166  double sqdistance = (double)vectorAB.x * vectorAB.x +
167  (double)vectorAB.y * vectorAB.y;
168 
169  return sqdistance < threshold * threshold;
170 }

Referenced by LIB_ARC::BeginEdit(), LIB_ARC::HitTest(), and GERBER_DRAW_ITEM::HitTest().

◆ IsPointOnSegment()

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

Function IsPointOnSegment.

Parameters
aSegStartThe first point of the segment S.
aSegEndThe second point of the segment S.
aTestPointThe point P to test.
Returns
true if the point P is on the segment S. faster than TestSegmentHit() because P should be exactly on S therefore works fine only for H, V and 45 deg segm. suitable for busses and wires in eeschema, otherwise use TestSegmentHit()

Definition at line 39 of file trigo.cpp.

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

Referenced by SCH_MOVE_TOOL::addJunctionsIfNeeded(), SCH_EDIT_FRAME::BreakSegment(), SCH_WIRE_BUS_TOOL::finishSegments(), removeBacktracks(), NETLIST_OBJECT_LIST::segmentToPointConnect(), SCH_EDIT_FRAME::TrimWire(), SCH_BUS_WIRE_ENTRY::UpdateDanglingState(), SCH_TEXT::UpdateDanglingState(), and SCH_BUS_BUS_ENTRY::UpdateDanglingState().

◆ NEGATE_AND_NORMALIZE_ANGLE_POS()

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

Definition at line 314 of file trigo.h.

315 {
316  Angle = NegateAndNormalizeAnglePos( Angle );
317 }
T NegateAndNormalizeAnglePos(T Angle)
Definition: trigo.h:305

References NegateAndNormalizeAnglePos().

Referenced by CreateComponentsSection().

◆ NegateAndNormalizeAnglePos()

template<class T >
T NegateAndNormalizeAnglePos ( Angle)
inline

Definition at line 305 of file trigo.h.

306 {
307  Angle = -Angle;
308  while( Angle < 0 )
309  Angle += 3600;
310  while( Angle >= 3600 )
311  Angle -= 3600;
312  return Angle;
313 }

Referenced by NEGATE_AND_NORMALIZE_ANGLE_POS().

◆ NORMALIZE_ANGLE_180()

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

Definition at line 344 of file trigo.h.

345 {
346  Angle = NormalizeAngle180( Angle );
347 }
T NormalizeAngle180(T Angle)
Normalize angle to be in the -180.0 .. 180.0 range.
Definition: trigo.h:336

References NormalizeAngle180().

Referenced by HPGL_PLOTTER::Arc(), and DIALOG_PAD_PROPERTIES::initValues().

◆ NORMALIZE_ANGLE_90()

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

Definition at line 329 of file trigo.h.

330 {
331  Angle = NormalizeAngle90( Angle );
332 }
T NormalizeAngle90(T Angle)
Normalize angle to be in the -90.0 .. 90.0 range.
Definition: trigo.h:321

References NormalizeAngle90().

Referenced by KIGFX::PCB_PAINTER::draw(), D_PAD::DrawShape(), and GERBER_DRAW_ITEM::GetTextD_CodePrms().

◆ NORMALIZE_ANGLE_DEGREES_POS()

void NORMALIZE_ANGLE_DEGREES_POS ( double &  Angle)
inline

Definition at line 268 of file trigo.h.

269 {
270  Angle = NormalizeAngleDegreesPos( Angle );
271 }
double NormalizeAngleDegreesPos(double Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:258

References NormalizeAngleDegreesPos().

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

◆ NORMALIZE_ANGLE_POS()

◆ NormalizeAngle180()

template<class T >
T NormalizeAngle180 ( Angle)
inline

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

Definition at line 336 of file trigo.h.

337 {
338  while( Angle <= -1800 )
339  Angle += 3600;
340  while( Angle > 1800 )
341  Angle -= 3600;
342  return Angle;
343 }

Referenced by NORMALIZE_ANGLE_180().

◆ NormalizeAngle360Max()

template<class T >
T NormalizeAngle360Max ( Angle)
inline

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

Definition at line 220 of file trigo.h.

221 {
222  while( Angle < -3600 )
223  Angle += 3600;
224  while( Angle > 3600 )
225  Angle -= 3600;
226  return Angle;
227 }

Referenced by DRAWSEGMENT::SetAngle().

◆ NormalizeAngle360Min()

template<class T >
T NormalizeAngle360Min ( Angle)
inline

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

Definition at line 231 of file trigo.h.

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

Referenced by PCB_IO::format(), D_PAD::Rotate(), TEXTE_PCB::SetTextAngle(), TEXTE_MODULE::SetTextAngle(), and WS_DRAW_ITEM_TEXT::SetTextAngle().

◆ NormalizeAngle90()

template<class T >
T NormalizeAngle90 ( Angle)
inline

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

Definition at line 321 of file trigo.h.

322 {
323  while( Angle < -900 )
324  Angle += 1800;
325  while( Angle > 900 )
326  Angle -= 1800;
327  return Angle;
328 }

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 285 of file trigo.h.

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

◆ NormalizeAngleDegreesPos()

double NormalizeAngleDegreesPos ( double  Angle)
inline

Normalize angle to be in the 0.0 .

. 360.0 range: angle is in degrees

Definition at line 258 of file trigo.h.

259 {
260  while( Angle < 0 )
261  Angle += 360.0;
262  while( Angle >= 360.0 )
263  Angle -= 360.0;
264  return Angle;
265 }

Referenced by NORMALIZE_ANGLE_DEGREES_POS().

◆ NormalizeAnglePos()

template<class T >
T NormalizeAnglePos ( Angle)
inline

Normalize angle to be in the 0.0 .

. 360.0 range: angle is in 1/10 degees

Definition at line 242 of file trigo.h.

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

Referenced by PCB_IO::format(), TEXT_MOD_GRID_TABLE::GetValue(), and NORMALIZE_ANGLE_POS().

◆ NormalizeAngleRadiansPos()

double NormalizeAngleRadiansPos ( double  Angle)
inline

Definition at line 274 of file trigo.h.

275 {
276  while( Angle < 0 )
277  Angle += (2 * M_PI );
278  while( Angle >= ( 2 * M_PI ) )
279  Angle -= ( 2 * M_PI );
280  return Angle;
281 }

Referenced by GERBER_DRAW_ITEM::HitTest().

◆ RAD2DECIDEG()

◆ RAD2DEG()

◆ RotatePoint() [1/8]

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

Definition at line 216 of file trigo.cpp.

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

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

Referenced by PCB_BASE_FRAME::AddPad(), PCAD2KICAD::PCB_MODULE::AddToBoard(), PCAD2KICAD::PCB_PAD::AddToModule(), SVG_PLOTTER::Arc(), FOOTPRINT_EDIT_FRAME::Begin_Edge_Module(), BuildConvexHull(), BuildCornersList_S_Shape(), D_PAD::BuildPadPolygon(), D_PAD::BuildSegmentFromOvalShape(), ZONE_FILLER::buildUnconnectedThermalStubsPolygonList(), LIB_ARC::CalcEdit(), DRC::checkClearancePadToPad(), DRC::checkClearanceSegmToPad(), DRAWSEGMENT::computeArcBBox(), PSLIKE_PLOTTER::computeTextParameters(), convertOblong2Segment(), ConvertOutlineToPolygon(), AM_PRIMITIVE::ConvertShapeToPolygon(), D_CODE::ConvertShapeToPolygon(), PCAD2KICAD::CorrectTextPosition(), PCB_EDIT_FRAME::Create_MuWaveComponent(), EXCELLON_WRITER::createDrillFile(), SCH_GLOBALLABEL::CreateGraphicShape(), CINFO3D_VISU::createNewPadWithClearance(), CreateThermalReliefPadPolygon(), D_PAD::CustomShapeAsPolygonToBoardPosition(), DRC::doTrackDrc(), EDGE_MODULE::Draw(), VIA::Draw(), AM_PRIMITIVE::DrawBasicShape(), DrawSegmentQcq(), AR_MATRIX::drawSegmentQcq(), D_PAD::DrawShape(), PCB_EDIT_FRAME::Edit_Gap(), GBR_TO_PCB_EXPORTER::export_segarc_copper_item(), export_vrml_module(), export_vrml_padshape(), fillArcPOLY(), HPGL_PLOTTER::FlashPadOval(), PSLIKE_PLOTTER::FlashPadOval(), GERBER_PLOTTER::FlashPadOval(), HPGL_PLOTTER::FlashPadRect(), PSLIKE_PLOTTER::FlashPadRect(), DXF_PLOTTER::FlashPadRect(), HPGL_PLOTTER::FlashPadTrapez(), PSLIKE_PLOTTER::FlashPadTrapez(), GERBER_PLOTTER::FlashPadTrapez(), DXF_PLOTTER::FlashPadTrapez(), FootprintWriteShape(), geom_transf(), GERBER_DRAW_ITEM::GetABPosition(), DRAWSEGMENT::GetArcEnd(), DRAWSEGMENT::GetArcMid(), LIB_TEXT::GetBoundingBox(), SCH_FIELD::GetBoundingBox(), LIB_PIN::GetBoundingBox(), SCH_TEXT::GetBoundingBox(), LIB_FIELD::GetBoundingBox(), DRAWSEGMENT::GetBoundingBox(), SCH_LABEL::GetBoundingBox(), D_PAD::GetBoundingBox(), EDA_RECT::GetBoundingBoxRotated(), WORKSHEET_DATAITEM_POLYPOLYGON::GetCornerPosition(), D_PAD::GetOblongDrillGeometry(), EDA_TEXT::GetPositionsOfLinesOfMultilineText(), GetRoundRectCornerCenters(), ARRAY_CIRCULAR_OPTIONS::GetTransform(), GERBER_DRAW_ITEM::GetXYPosition(), GRArc(), GRCSegm(), GRFilledArc(), D_PAD::HitTest(), idf_export_module(), InitialiseDragParameters(), C3D_RENDER_RAYTRACING::insert3DPadHole(), EDA_RECT::Intersects(), SCH_LEGACY_PLUGIN_CACHE::loadArc(), LEGACY_PLUGIN::loadPAD(), MODULE::MoveAnchorPosition(), MoveMarkedItemsExactly(), OGL_draw_half_open_cylinder(), GPCB_FPL_CACHE::parseMODULE(), PCB_PARSER::parseMODULE_unchecked(), PCB_BASE_FRAME::PlacePad(), PCB_BASE_FRAME::PlaceTexteModule(), BRDITEMS_PLOTTER::Plot_1_EdgeModule(), SCH_MARKER::Rotate(), SCH_JUNCTION::Rotate(), TEXTE_PCB::Rotate(), SCH_NO_CONNECT::Rotate(), PCB_TARGET::Rotate(), LIB_CIRCLE::Rotate(), MARKER_PCB::Rotate(), LIB_RECTANGLE::Rotate(), LIB_BEZIER::Rotate(), SCH_BUS_ENTRY_BASE::Rotate(), LIB_POLYLINE::Rotate(), LIB_ARC::Rotate(), TEXTE_MODULE::Rotate(), LIB_TEXT::Rotate(), TRACK::Rotate(), SCH_LINE::Rotate(), SCH_BITMAP::Rotate(), SCH_FIELD::Rotate(), SCH_TEXT::Rotate(), SCH_SHEET_PIN::Rotate(), DIMENSION::Rotate(), LIB_FIELD::Rotate(), MODULE::Rotate(), DRAWSEGMENT::Rotate(), ZONE_CONTAINER::Rotate(), LIB_PIN::Rotate(), SCH_SHEET::Rotate(), SCH_COMPONENT::Rotate(), D_PAD::Rotate(), SCH_LINE::RotateEnd(), RotatePoint(), SCH_LINE::RotateStart(), WORKSHEET_DATAITEM_POLYPOLYGON::SetBoundingBox(), EDGE_MODULE::SetDrawCoord(), TEXTE_MODULE::SetDrawCoord(), D_PAD::SetDrawCoord(), EDGE_MODULE::SetLocalCoord(), TEXTE_MODULE::SetLocalCoord(), D_PAD::SetLocalCoord(), DRAG_SEGM_PICKER::SetTrackEndsCoordinates(), D_PAD::ShapePos(), ShowBoundingBoxMicroWaveInductor(), ShowNewEdgeModule(), PLOTTER::sketchOval(), PNS_KICAD_IFACE::syncPad(), TEXTE_MODULE::TextHitTest(), EDA_TEXT::TextHitTest(), AR_MATRIX::TraceFilledRectangle(), TraceFilledRectangle(), DIALOG_PAD_PROPERTIES::TransferDataFromWindow(), EAGLE_PLUGIN::transferPad(), TransformArcToPolygon(), CINFO3D_VISU::TransformArcToSegments(), EDA_TEXT::TransformBoundingBoxWithClearanceToPolygon(), TransformCircleToPolygon(), TransformOvalClearanceToPolygon(), TransformRoundedEndsSegmentToPolygon(), DRAWSEGMENT::TransformShapeWithClearanceToPolygon(), and D_PAD::TransformShapeWithClearanceToPolygon().

◆ RotatePoint() [2/8]

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

Definition at line 256 of file trigo.cpp.

257 {
258  int ox, oy;
259 
260  ox = *pX - cx;
261  oy = *pY - cy;
262 
263  RotatePoint( &ox, &oy, angle );
264 
265  *pX = ox + cx;
266  *pY = oy + cy;
267 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
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 77 of file trigo.h.

78 {
79  RotatePoint( &point->x, &point->y, angle );
80 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
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 82 of file trigo.h.

83 {
84  RotatePoint( &point.x, &point.y, angle );
85 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
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 282 of file trigo.cpp.

283 {
284  wxPoint c( centre.x, centre.y );
285  wxPoint p( point.x, point.y );
286 
287  RotatePoint(&p, c, angle);
288 
289  point.x = p.x;
290  point.y = p.y;
291 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
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 270 of file trigo.cpp.

271 {
272  int ox, oy;
273 
274  ox = point->x - centre.x;
275  oy = point->y - centre.y;
276 
277  RotatePoint( &ox, &oy, angle );
278  point->x = ox + centre.x;
279  point->y = oy + centre.y;
280 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
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 308 of file trigo.cpp.

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

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

◆ RotatePoint() [8/8]

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

Definition at line 294 of file trigo.cpp.

295 {
296  double ox, oy;
297 
298  ox = *pX - cx;
299  oy = *pY - cy;
300 
301  RotatePoint( &ox, &oy, angle );
302 
303  *pX = ox + cx;
304  *pY = oy + cy;
305 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
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 
)

Function SegmentIntersectsSegment.

Parameters
a_p1_l1The first point of the first line.
a_p2_l1The second point of the first line.
a_p1_l2The first point of the second line.
a_p2_l2The second point of the second line.
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 58 of file trigo.cpp.

60 {
61 
62  //We are forced to use 64bit ints because the internal units can oveflow 32bit ints when
63  // multiplied with each other, the alternative would be to scale the units down (i.e. divide
64  // by a fixed number).
65  long long dX_a, dY_a, dX_b, dY_b, dX_ab, dY_ab;
66  long long num_a, num_b, den;
67 
68  //Test for intersection within the bounds of both line segments using line equations of the
69  // form:
70  // x_k(u_k) = u_k * dX_k + x_k(0)
71  // y_k(u_k) = u_k * dY_k + y_k(0)
72  // with 0 <= u_k <= 1 and k = [ a, b ]
73 
74  dX_a = a_p2_l1.x - a_p1_l1.x;
75  dY_a = a_p2_l1.y - a_p1_l1.y;
76  dX_b = a_p2_l2.x - a_p1_l2.x;
77  dY_b = a_p2_l2.y - a_p1_l2.y;
78  dX_ab = a_p1_l2.x - a_p1_l1.x;
79  dY_ab = a_p1_l2.y - a_p1_l1.y;
80 
81  den = dY_a * dX_b - dY_b * dX_a ;
82 
83  //Check if lines are parallel
84  if( den == 0 )
85  return false;
86 
87  num_a = dY_ab * dX_b - dY_b * dX_ab;
88  num_b = dY_ab * dX_a - dY_a * dX_ab;
89 
90  //We wont calculate directly the u_k of the intersection point to avoid floating point
91  // division but they could be calculated with:
92  // u_a = (float) num_a / (float) den;
93  // u_b = (float) num_b / (float) den;
94 
95  if( den < 0 )
96  {
97  den = -den;
98  num_a = -num_a;
99  num_b = -num_b;
100  }
101 
102  //Test sign( u_a ) and return false if negative
103  if( num_a < 0 )
104  return false;
105 
106  //Test sign( u_b ) and return false if negative
107  if( num_b < 0 )
108  return false;
109 
110  //Test to ensure (u_a <= 1)
111  if( num_a > den )
112  return false;
113 
114  //Test to ensure (u_b <= 1)
115  if( num_b > den )
116  return false;
117 
118  return true;
119 }

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 354 of file trigo.h.

355 {
356  return r * sin( DECIDEG2RAD( a ) );
357 }
double DECIDEG2RAD(double deg)
Definition: trigo.h:212

References DECIDEG2RAD().

Referenced by PLOTTER::Arc(), HPGL_PLOTTER::Arc(), PDF_PLOTTER::Arc(), GERBER_PLOTTER::Arc(), PCAD2KICAD::PCB_ARC::Parse(), TraceCircle(), and AR_MATRIX::traceCircle().

◆ TestSegmentHit()

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

Function TestSegmentHit test for hit on line segment i.e.

a reference point is within a given distance from segment

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

Definition at line 122 of file trigo.cpp.

123 {
124  int xmin = aStart.x;
125  int xmax = aEnd.x;
126  int ymin = aStart.y;
127  int ymax = aEnd.y;
128  wxPoint delta = aStart - aRefPoint;
129 
130  if( xmax < xmin )
131  std::swap( xmax, xmin );
132 
133  if( ymax < ymin )
134  std::swap( ymax, ymin );
135 
136  // First, check if we are outside of the bounding box
137  if( ( ymin - aRefPoint.y > aDist ) || ( aRefPoint.y - ymax > aDist ) )
138  return false;
139 
140  if( ( xmin - aRefPoint.x > aDist ) || ( aRefPoint.x - xmax > aDist ) )
141  return false;
142 
143  // Next, eliminate easy cases
144  if( aStart.x == aEnd.x && aRefPoint.y > ymin && aRefPoint.y < ymax )
145  return std::abs( delta.x ) <= aDist;
146 
147  if( aStart.y == aEnd.y && aRefPoint.x > xmin && aRefPoint.x < xmax )
148  return std::abs( delta.y ) <= aDist;
149 
150  wxPoint len = aEnd - aStart;
151  // Precision note here:
152  // These are 32-bit integers, so squaring requires 64 bits to represent
153  // exactly. 64-bit Doubles have only 52 bits in the mantissa, so we start to lose
154  // precision at 2^53, which corresponds to ~ ±1nm @ 9.5cm, 2nm at 90cm, etc...
155  // Long doubles avoid this ambiguity as well as the more expensive denormal double calc
156  // Long doubles usually (sometimes more if SIMD) have at least 64 bits in the mantissa
157  long double length_square = (long double) len.x * len.x + (long double) len.y * len.y;
158  long double cross = std::abs( (long double) len.x * delta.y - (long double) len.y * delta.x );
159  long double dist_square = (long double) aDist * aDist;
160 
161  // The perpendicular distance to a line is the vector magnitude of the line from
162  // a test point to the test line. That is the 2d determinant. Because we handled
163  // the zero length case above, so we are guaranteed a unique solution.
164 
165  return ( ( length_square >= cross && dist_square >= cross ) ||
166  ( length_square * dist_square >= cross * cross ) );
167 }
#define abs(a)
Definition: auxiliary.h:84
static const int delta[8][2]
Definition: solve.cpp:112

References abs, and delta.

Referenced by SCH_EAGLE_PLUGIN::addBusEntries(), convex2pointDRC(), DRAWING_TOOL::DrawVia(), LIB_RECTANGLE::HitTest(), LIB_BEZIER::HitTest(), LIB_POLYLINE::HitTest(), SCH_BUS_ENTRY_BASE::HitTest(), WS_DRAW_ITEM_LINE::HitTest(), SCH_LINE::HitTest(), DRAWSEGMENT::HitTest(), DIMENSION::HitTest(), TRACK::HitTest(), WS_DRAW_ITEM_RECT::HitTest(), GERBER_DRAW_ITEM::HitTest(), D_PAD::HitTest(), LocateIntrusion(), and SCH_EAGLE_PLUGIN::moveLabels().