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)
 
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

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

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

Definition at line 288 of file trigo.h.

References NORMALIZE_ANGLE_POS().

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

289 {
290  a1 += a2;
291  NORMALIZE_ANGLE_POS( a1 );
292  return a1;
293 }
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:241
double ArcTangente ( int  dy,
int  dx 
)

Definition at line 170 of file trigo.cpp.

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(), RotatePoint(), PLOTTER::segmentAsOval(), ShowBoundingBoxMicroWaveInductor(), TraceArc(), AR_MATRIX::traceArc(), TransformRoundedEndsSegmentToPolygon(), and PCB_BASE_FRAME::UpdateStatusBar().

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

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().

355 {
356  return r * cos( DECIDEG2RAD( a ) );
357 }
double DECIDEG2RAD(double deg)
Definition: trigo.h:203
double CrossProduct ( const wxPoint &  vectorA,
const wxPoint &  vectorB 
)
inline

Determine the cross product.

Parameters
vectorATwo-dimensional vector
vectorBTwo-dimensional vector

Definition at line 166 of file trigo.h.

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

167 {
168  // As before the cast is to avoid int overflow
169  return (double)vectorA.x * vectorB.y - (double)vectorA.y * vectorB.x;
170 }
double DECIDEG2RAD ( double  deg)
inline
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 129 of file trigo.h.

References EuclideanNorm().

Referenced by LIB_ARC::CalcEdit().

132 {
133  // Some of the multiple double casts are redundant. However in the previous
134  // definition the cast was (implicitly) done too late, just before
135  // the division (EuclideanNorm gives a double so from int it would
136  // be promoted); that means that the whole expression were
137  // vulnerable to overflow during int multiplications
138  return fabs( ( double(linePointB.x - linePointA.x) *
139  double(linePointA.y - referencePoint.y) -
140  double(linePointA.x - referencePoint.x ) *
141  double(linePointB.y - linePointA.y) )
142  / EuclideanNorm( linePointB - linePointA ) );
143 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:112
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 112 of file trigo.h.

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::CalcEdit(), 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(), 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(), VECTOR2< unsigned int >::operator wxPoint(), 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().

113 {
114  // this is working with doubles
115  return hypot( vector.x, vector.y );
116 }
double EuclideanNorm ( const wxSize &  vector)
inline

Definition at line 118 of file trigo.h.

119 {
120  // this is working with doubles, too
121  return hypot( vector.x, vector.y );
122 }
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 191 of file trigo.h.

Referenced by LIB_CIRCLE::CalcEdit(), 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(), DRAWSEGMENT::GetRadius(), LIB_CIRCLE::HitTest(), LIB_ARC::HitTest(), GERBER_DRAW_ITEM::HitTest(), TRACK::IsPointOnEnds(), Magnetize(), DSN::SPECCTRA_DB::makeIMAGE(), DRC::newMarker(), BRDITEMS_PLOTTER::Plot_1_EdgeModule(), BRDITEMS_PLOTTER::PlotDrawSegment(), DRC::testDrilledHoles(), and GERBER_DRAW_ITEM::ViewGetLOD().

192 {
193  // Implicitly casted to double
194  return hypot( aPointA.x - aPointB.x,
195  aPointA.y - aPointB.y );
196 }
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 150 of file trigo.h.

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

152 {
153  wxPoint vectorAB = pointB - pointA;
154 
155  // Compare the distances squared. The double is needed to avoid
156  // overflow during int multiplication
157  double sqdistance = (double)vectorAB.x * vectorAB.x +
158  (double)vectorAB.y * vectorAB.y;
159 
160  return sqdistance < threshold * threshold;
161 }
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.

Referenced by SCH_EDIT_FRAME::BreakSegment(), SCH_EDIT_FRAME::CheckListConnections(), SCH_EDIT_FRAME::EndSegment(), RemoveBacktracks(), NETLIST_OBJECT_LIST::segmentToPointConnect(), SCH_EDIT_FRAME::TrimWire(), SCH_BUS_WIRE_ENTRY::UpdateDanglingState(), SCH_TEXT::UpdateDanglingState(), and SCH_BUS_BUS_ENTRY::UpdateDanglingState().

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 }
template<class T >
void NEGATE_AND_NORMALIZE_ANGLE_POS ( T &  Angle)
inline

Definition at line 305 of file trigo.h.

References NegateAndNormalizeAnglePos().

Referenced by CreateComponentsSection().

306 {
307  Angle = NegateAndNormalizeAnglePos( Angle );
308 }
T NegateAndNormalizeAnglePos(T Angle)
Definition: trigo.h:296
template<class T >
T NegateAndNormalizeAnglePos ( Angle)
inline

Definition at line 296 of file trigo.h.

Referenced by NEGATE_AND_NORMALIZE_ANGLE_POS().

297 {
298  Angle = -Angle;
299  while( Angle < 0 )
300  Angle += 3600;
301  while( Angle >= 3600 )
302  Angle -= 3600;
303  return Angle;
304 }
template<class T >
void NORMALIZE_ANGLE_180 ( T &  Angle)
inline

Definition at line 335 of file trigo.h.

References NormalizeAngle180().

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

336 {
337  Angle = NormalizeAngle180( Angle );
338 }
T NormalizeAngle180(T Angle)
Normalize angle to be in the -180.0 .. 180.0 range.
Definition: trigo.h:327
template<class T >
void NORMALIZE_ANGLE_90 ( T &  Angle)
inline

Definition at line 320 of file trigo.h.

References NormalizeAngle90().

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

321 {
322  Angle = NormalizeAngle90( Angle );
323 }
T NormalizeAngle90(T Angle)
Normalize angle to be in the -90.0 .. 90.0 range.
Definition: trigo.h:312
void NORMALIZE_ANGLE_DEGREES_POS ( double &  Angle)
inline

Definition at line 259 of file trigo.h.

References NormalizeAngleDegreesPos().

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

260 {
261  Angle = NormalizeAngleDegreesPos( Angle );
262 }
double NormalizeAngleDegreesPos(double Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:249
template<class T >
T NormalizeAngle180 ( Angle)
inline

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

Definition at line 327 of file trigo.h.

Referenced by NORMALIZE_ANGLE_180().

328 {
329  while( Angle <= -1800 )
330  Angle += 3600;
331  while( Angle > 1800 )
332  Angle -= 3600;
333  return Angle;
334 }
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 211 of file trigo.h.

Referenced by DRAWSEGMENT::SetAngle().

212 {
213  while( Angle < -3600 )
214  Angle += 3600;
215  while( Angle > 3600 )
216  Angle -= 3600;
217  return Angle;
218 }
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 222 of file trigo.h.

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

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

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

Definition at line 312 of file trigo.h.

Referenced by NORMALIZE_ANGLE_90().

313 {
314  while( Angle < -900 )
315  Angle += 1800;
316  while( Angle > 900 )
317  Angle -= 1800;
318  return Angle;
319 }
double NormalizeAngleDegrees ( double  Angle,
double  aMin,
double  aMax 
)
inline

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

Definition at line 276 of file trigo.h.

277 {
278  while( Angle < aMin )
279  Angle += 360.0;
280  while( Angle >= aMax )
281  Angle -= 360.0;
282  return Angle;
283 }
double NormalizeAngleDegreesPos ( double  Angle)
inline

Normalize angle to be in the 0.0 .

. 360.0 range: angle is in degrees

Definition at line 249 of file trigo.h.

Referenced by NORMALIZE_ANGLE_DEGREES_POS().

250 {
251  while( Angle < 0 )
252  Angle += 360.0;
253  while( Angle >= 360.0 )
254  Angle -= 360.0;
255  return Angle;
256 }
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 233 of file trigo.h.

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

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

Definition at line 265 of file trigo.h.

Referenced by GERBER_DRAW_ITEM::HitTest().

266 {
267  while( Angle < 0 )
268  Angle += (2 * M_PI );
269  while( Angle >= ( 2 * M_PI ) )
270  Angle -= ( 2 * M_PI );
271  return Angle;
272 }
void RotatePoint ( int *  pX,
int *  pY,
double  angle 
)

Definition at line 216 of file trigo.cpp.

References DECIDEG2RAD(), KiROUND(), and NORMALIZE_ANGLE_POS().

Referenced by DXF_IMPORT_PLUGIN::addArc(), 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(), LIB_TEXT::GetBoundingBox(), SCH_FIELD::GetBoundingBox(), SCH_TEXT::GetBoundingBox(), LIB_PIN::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(), 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(), TEXTE_PCB::Rotate(), SCH_MARKER::Rotate(), SCH_JUNCTION::Rotate(), SCH_NO_CONNECT::Rotate(), PCB_TARGET::Rotate(), MARKER_PCB::Rotate(), LIB_CIRCLE::Rotate(), LIB_BEZIER::Rotate(), LIB_RECTANGLE::Rotate(), TEXTE_MODULE::Rotate(), SCH_BUS_ENTRY_BASE::Rotate(), LIB_POLYLINE::Rotate(), TRACK::Rotate(), LIB_TEXT::Rotate(), LIB_ARC::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(), RotatePoint(), 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(), TransformRingToPolygon(), TransformRoundedEndsSegmentToPolygon(), DRAWSEGMENT::TransformShapeWithClearanceToPolygon(), and D_PAD::TransformShapeWithClearanceToPolygon().

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:121
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:241
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
double DECIDEG2RAD(double deg)
Definition: trigo.h:203
void RotatePoint ( int *  pX,
int *  pY,
int  cx,
int  cy,
double  angle 
)

Definition at line 256 of file trigo.cpp.

References RotatePoint().

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)
void RotatePoint ( wxPoint *  point,
double  angle 
)
inline

Definition at line 77 of file trigo.h.

References RotatePoint().

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)
void RotatePoint ( VECTOR2I point,
double  angle 
)
inline

Definition at line 82 of file trigo.h.

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

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)
void RotatePoint ( VECTOR2I point,
const VECTOR2I centre,
double  angle 
)

Definition at line 282 of file trigo.cpp.

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

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)
void RotatePoint ( wxPoint *  point,
const wxPoint &  centre,
double  angle 
)

Definition at line 270 of file trigo.cpp.

References RotatePoint().

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)
void RotatePoint ( double *  pX,
double *  pY,
double  angle 
)

Definition at line 308 of file trigo.cpp.

References DECIDEG2RAD(), and NORMALIZE_ANGLE_POS().

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:241
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
double DECIDEG2RAD(double deg)
Definition: trigo.h:203
void RotatePoint ( double *  pX,
double *  pY,
double  cx,
double  cy,
double  angle 
)

Definition at line 294 of file trigo.cpp.

References RotatePoint().

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)
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.

Referenced by EDA_RECT::Intersects().

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

References DECIDEG2RAD().

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

346 {
347  return r * sin( DECIDEG2RAD( a ) );
348 }
double DECIDEG2RAD(double deg)
Definition: trigo.h:203
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.

References abs, and delta.

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

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