KiCad PCB EDA Suite
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)
 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, wxPoint *aIntersectionPoint=nullptr)
 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 299 of file trigo.h.

300 {
301  a1 += a2;
302  NORMALIZE_ANGLE_POS( a1 );
303  return a1;
304 }
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:252

References NORMALIZE_ANGLE_POS().

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

◆ ArcTangente()

double ArcTangente ( int  dy,
int  dx 
)

Definition at line 160 of file trigo.cpp.

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

References RAD2DECIDEG().

Referenced by BuildCornersList_S_Shape(), LIB_ARC::CalcEdit(), LIB_ARC::CalcRadiusAngles(), DRC::checkClearancePadToPad(), AM_PRIMITIVE::ConvertShapeToPolygon(), DRC::doTrackDrc(), AR_MATRIX::drawSegmentQcq(), fillArcPOLY(), DRAWSEGMENT::GetArcAngleStart(), GRCSegm(), DRAWSEGMENT::HitTest(), PCAD2KICAD::PCB_ARC::Parse(), BRDITEMS_PLOTTER::Plot_1_EdgeModule(), BRDITEMS_PLOTTER::PlotDrawSegment(), EDGE_MODULE::Print(), DRAWSEGMENT::Print(), PLOTTER::segmentAsOval(), AR_MATRIX::traceArc(), and TransformSegmentToPolygon().

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

366 {
367  return r * cos( DECIDEG2RAD( a ) );
368 }
double DECIDEG2RAD(double deg)
Definition: trigo.h:214

References DECIDEG2RAD().

Referenced by PLOTTER::Arc(), HPGL_PLOTTER::Arc(), PDF_PLOTTER::Arc(), GERBER_PLOTTER::Arc(), PCAD2KICAD::PCB_ARC::Parse(), AR_MATRIX::traceArc(), 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 177 of file trigo.h.

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

Referenced by 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 140 of file trigo.h.

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

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

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

Referenced by GRID_HELPER::AlignToSegment(), 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(), 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(), DRAWING_TOOL::DrawVia(), PAD_TOOL::EnumeratePads(), PNS::findCoupledVertices(), PNS::DIFF_PAIR_PLACER::findDpPrimitivePair(), gen_arc(), 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(), PNS::LINE::snapDraggedCorner(), PNS::DRAGGER::startDragSegment(), TransformArcToPolygon(), TransformOvalToPolygon(), and TransformSegmentToPolygon().

◆ EuclideanNorm() [2/2]

double EuclideanNorm ( const wxSize &  vector)
inline

Definition at line 129 of file trigo.h.

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

◆ 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 339 of file trigo.cpp.

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

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

Referenced by LIB_ARC::CalcEdit(), KIGFX::GERBVIEW_PAINTER::draw(), DRAWING_TOOL::DrawVia(), 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(), DSN::SPECCTRA_DB::makeIMAGE(), DRC_MARKER_FACTORY::NewMarker(), BRDITEMS_PLOTTER::Plot_1_EdgeModule(), BRDITEMS_PLOTTER::PlotDrawSegment(), GERBER_DRAW_ITEM::Print(), 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 161 of file trigo.h.

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

Referenced by LIB_ARC::HitTest(), GERBER_DRAW_ITEM::HitTest(), and EE_SELECTION_TOOL::SelectPoint().

◆ 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 40 of file trigo.cpp.

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

Referenced by SCH_LINE_WIRE_BUS_TOOL::AddJunctionsIfNeeded(), SCH_EDIT_FRAME::BreakSegment(), SCH_LINE_WIRE_BUS_TOOL::finishSegments(), 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 316 of file trigo.h.

317 {
318  Angle = NegateAndNormalizeAnglePos( Angle );
319 }
T NegateAndNormalizeAnglePos(T Angle)
Definition: trigo.h:307

References NegateAndNormalizeAnglePos().

Referenced by CreateComponentsSection().

◆ NegateAndNormalizeAnglePos()

template<class T >
T NegateAndNormalizeAnglePos ( Angle)
inline

Definition at line 307 of file trigo.h.

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

Referenced by NEGATE_AND_NORMALIZE_ANGLE_POS().

◆ NORMALIZE_ANGLE_180()

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

Definition at line 346 of file trigo.h.

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

References NormalizeAngle180().

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

◆ NORMALIZE_ANGLE_90()

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

Definition at line 331 of file trigo.h.

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

References NormalizeAngle90().

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

◆ NORMALIZE_ANGLE_DEGREES_POS()

void NORMALIZE_ANGLE_DEGREES_POS ( double &  Angle)
inline

Definition at line 270 of file trigo.h.

271 {
272  Angle = NormalizeAngleDegreesPos( Angle );
273 }
double NormalizeAngleDegreesPos(double Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:260

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

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

Referenced by DIALOG_COPPER_ZONE::AcceptOptions(), and 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 222 of file trigo.h.

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

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

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

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

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

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

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

◆ NormalizeAngleDegreesPos()

double NormalizeAngleDegreesPos ( double  Angle)
inline

Normalize angle to be in the 0.0 .

. 360.0 range: angle is in degrees

Definition at line 260 of file trigo.h.

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

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

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

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

◆ NormalizeAngleRadiansPos()

double NormalizeAngleRadiansPos ( double  Angle)
inline

Definition at line 276 of file trigo.h.

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

Referenced by GERBER_DRAW_ITEM::HitTest().

◆ RAD2DECIDEG()

◆ RAD2DEG()

◆ RotatePoint() [1/8]

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

Definition at line 206 of file trigo.cpp.

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

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

Referenced by PCAD2KICAD::PCB_MODULE::AddToBoard(), PCAD2KICAD::PCB_PAD::AddToModule(), SVG_PLOTTER::Arc(), BuildConvexHull(), BuildCornersList_S_Shape(), D_PAD::BuildPadPolygon(), D_PAD::BuildSegmentFromOvalShape(), 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(), DRC::doTrackDrc(), AM_PRIMITIVE::DrawBasicShape(), AR_MATRIX::drawSegmentQcq(), 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(), GERBER_PLOTTER::FlashRegularPolygon(), FootprintWriteShape(), geom_transf(), GERBER_DRAW_ITEM::GetABPosition(), CN_ITEM::GetAnchor(), DRAWSEGMENT::GetArcEnd(), DRAWSEGMENT::GetArcMid(), LIB_TEXT::GetBoundingBox(), SCH_FIELD::GetBoundingBox(), LIB_PIN::GetBoundingBox(), LIB_FIELD::GetBoundingBox(), SCH_TEXT::GetBoundingBox(), DRAWSEGMENT::GetBoundingBox(), SCH_LABEL::GetBoundingBox(), D_PAD::GetBoundingBox(), EDA_RECT::GetBoundingBoxRotated(), WS_DATA_ITEM_POLYGONS::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(), C3D_RENDER_RAYTRACING::insert3DPadHole(), EDA_RECT::Intersects(), SCH_LEGACY_PLUGIN_CACHE::loadArc(), LEGACY_PLUGIN::loadPAD(), MODULE::MoveAnchorPosition(), OGL_draw_half_open_cylinder(), GPCB_FPL_CACHE::parseMODULE(), PCB_PARSER::parseMODULE_unchecked(), BRDITEMS_PLOTTER::Plot_1_EdgeModule(), EDGE_MODULE::Print(), VIA::Print(), D_PAD::PrintShape(), SCH_MARKER::Rotate(), SCH_JUNCTION::Rotate(), TEXTE_PCB::Rotate(), LIB_RECTANGLE::Rotate(), LIB_CIRCLE::Rotate(), PCB_TARGET::Rotate(), SCH_NO_CONNECT::Rotate(), LIB_BEZIER::Rotate(), MARKER_PCB::Rotate(), LIB_TEXT::Rotate(), TRACK::Rotate(), LIB_ARC::Rotate(), LIB_POLYLINE::Rotate(), SCH_BUS_ENTRY_BASE::Rotate(), TEXTE_MODULE::Rotate(), PAD_CS_PRIMITIVE::Rotate(), SCH_BITMAP::Rotate(), SCH_LINE::Rotate(), SCH_FIELD::Rotate(), SCH_TEXT::Rotate(), SCH_SHEET_PIN::Rotate(), LIB_FIELD::Rotate(), DIMENSION::Rotate(), DRAWSEGMENT::Rotate(), MODULE::Rotate(), ZONE_CONTAINER::Rotate(), LIB_PIN::Rotate(), SCH_SHEET::Rotate(), SCH_COMPONENT::Rotate(), D_PAD::Rotate(), SCH_LINE::RotateEnd(), RotatePoint(), SCH_LINE::RotateStart(), WS_DATA_ITEM_POLYGONS::SetBoundingBox(), EDGE_MODULE::SetDrawCoord(), TEXTE_MODULE::SetDrawCoord(), D_PAD::SetDrawCoord(), EDGE_MODULE::SetLocalCoord(), TEXTE_MODULE::SetLocalCoord(), D_PAD::SetLocalCoord(), D_PAD::ShapePos(), PLOTTER::sketchOval(), PNS_KICAD_IFACE::syncPad(), TEXTE_MODULE::TextHitTest(), EDA_TEXT::TextHitTest(), AR_MATRIX::TraceFilledRectangle(), DIALOG_PAD_PROPERTIES::TransferDataFromWindow(), EAGLE_PLUGIN::transferPad(), TransformArcToPolygon(), CINFO3D_VISU::TransformArcToSegments(), EDA_TEXT::TransformBoundingBoxWithClearanceToPolygon(), TransformCircleToPolygon(), TransformOvalToPolygon(), TransformSegmentToPolygon(), DRAWSEGMENT::TransformShapeWithClearanceToPolygon(), and D_PAD::TransformShapeWithClearanceToPolygon().

◆ RotatePoint() [2/8]

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

Definition at line 246 of file trigo.cpp.

247 {
248  int ox, oy;
249 
250  ox = *pX - cx;
251  oy = *pY - cy;
252 
253  RotatePoint( &ox, &oy, angle );
254 
255  *pX = ox + cx;
256  *pY = oy + cy;
257 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:206
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 79 of file trigo.h.

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

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

273 {
274  wxPoint c( centre.x, centre.y );
275  wxPoint p( point.x, point.y );
276 
277  RotatePoint(&p, c, angle);
278 
279  point.x = p.x;
280  point.y = p.y;
281 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:206
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 260 of file trigo.cpp.

261 {
262  int ox, oy;
263 
264  ox = point->x - centre.x;
265  oy = point->y - centre.y;
266 
267  RotatePoint( &ox, &oy, angle );
268  point->x = ox + centre.x;
269  point->y = oy + centre.y;
270 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:206
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 298 of file trigo.cpp.

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

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 284 of file trigo.cpp.

285 {
286  double ox, oy;
287 
288  ox = *pX - cx;
289  oy = *pY - cy;
290 
291  RotatePoint( &ox, &oy, angle );
292 
293  *pX = ox + cx;
294  *pY = oy + cy;
295 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:206
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 
)

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.
aIntersectionPointis 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 59 of file trigo.cpp.

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

References KiROUND().

Referenced by DRC::doTrackDrc(), and 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 356 of file trigo.h.

357 {
358  return r * sin( DECIDEG2RAD( a ) );
359 }
double DECIDEG2RAD(double deg)
Definition: trigo.h:214

References DECIDEG2RAD().

Referenced by PLOTTER::Arc(), HPGL_PLOTTER::Arc(), PDF_PLOTTER::Arc(), GERBER_PLOTTER::Arc(), PCAD2KICAD::PCB_ARC::Parse(), 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 127 of file trigo.cpp.

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

References abs, and SEG::PointCloserThan().

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