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

References NORMALIZE_ANGLE_POS().

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

268 {
269  a1 += a2;
270  NORMALIZE_ANGLE_POS( a1 );
271  return a1;
272 }
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:241
double ArcTangente ( int  dy,
int  dx 
)

Definition at line 271 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(), fillArcPOLY(), DRAWSEGMENT::GetArcAngleStart(), GRCSegm(), DRAWSEGMENT::HitTest(), PCAD2KICAD::PCB_ARC::Parse(), BRDITEMS_PLOTTER::Plot_1_EdgeModule(), BRDITEMS_PLOTTER::PlotDrawSegment(), PLOTTER::segmentAsOval(), ShowBoundingBoxMicroWaveInductor(), DRC::testTexts(), TraceArc(), TransformRoundedEndsSegmentToPolygon(), and PCB_BASE_FRAME::UpdateStatusBar().

272 {
273 
274  /* gcc is surprisingly smart in optimizing these conditions in
275  a tree! */
276 
277  if( dx == 0 && dy == 0 )
278  return 0;
279 
280  if( dy == 0 )
281  {
282  if( dx >= 0 )
283  return 0;
284  else
285  return -1800;
286  }
287 
288  if( dx == 0 )
289  {
290  if( dy >= 0 )
291  return 900;
292  else
293  return -900;
294  }
295 
296  if( dx == dy )
297  {
298  if( dx >= 0 )
299  return 450;
300  else
301  return -1800 + 450;
302  }
303 
304  if( dx == -dy )
305  {
306  if( dx >= 0 )
307  return -450;
308  else
309  return 1800 - 450;
310  }
311 
312  // Of course dy and dx are treated as double
313  return RAD2DECIDEG( atan2( (double) dy, (double) dx ) );
314 }
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 333 of file trigo.h.

References DECIDEG2RAD().

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

334 {
335  return r * cos( DECIDEG2RAD( a ) );
336 }
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.

References wxPoint::x, and wxPoint::y.

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 DEG2RAD ( 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(), wxPoint::x, and wxPoint::y.

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.

References wxPoint::x, and wxPoint::y.

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(), 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(), compareOriginDistance::operator()(), SHAPE_LINE_CHAIN::compareOriginDistance::operator()(), GPCB_FPL_CACHE::parseMODULE(), SHAPE_LINE_CHAIN::PathLength(), pushoutForce(), PNS::LINE_PLACER::rhMarkObstacles(), PNS::DIFF_PAIR_PLACER::routeHead(), PLOTTER::segmentAsOval(), ShowBoundingBoxMicroWaveInductor(), PNS::LINE::snapDraggedCorner(), PNS::DRAGGER::startDragSegment(), 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.

References wxPoint::x, and wxPoint::y.

Referenced by LIB_CIRCLE::calcEdit(), KIGFX::GERBVIEW_PAINTER::draw(), GERBER_DRAW_ITEM::Draw(), TRACK::DrawShortNetname(), DRC::fillMarker(), FootprintWriteShape(), GERBER_DRAW_ITEM::GetBoundingBox(), SCH_LINE::GetLength(), TRACK::GetLength(), DRAWSEGMENT::GetLength(), DRAWSEGMENT::GetMsgPanelInfo(), DRAWSEGMENT::GetRadius(), LIB_CIRCLE::HitTest(), LIB_ARC::HitTest(), TRACK::IsPointOnEnds(), Magnetize(), DSN::SPECCTRA_DB::makeIMAGE(), BRDITEMS_PLOTTER::Plot_1_EdgeModule(), BRDITEMS_PLOTTER::PlotDrawSegment(), 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.

References wxPoint::x, and wxPoint::y.

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.

References wxPoint::x, and wxPoint::y.

Referenced by SCH_BUS_ENTRY_BASE::IsDanglingStateChanged(), SCH_TEXT::IsDanglingStateChanged(), RemoveBacktracks(), and NETLIST_OBJECT_LIST::segmentToPointConnect().

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

References NegateAndNormalizeAnglePos().

Referenced by CreateComponentsSection().

285 {
286  Angle = NegateAndNormalizeAnglePos( Angle );
287 }
T NegateAndNormalizeAnglePos(T Angle)
Definition: trigo.h:275
template<class T >
T NegateAndNormalizeAnglePos ( Angle)
inline

Definition at line 275 of file trigo.h.

Referenced by NEGATE_AND_NORMALIZE_ANGLE_POS().

276 {
277  Angle = -Angle;
278  while( Angle < 0 )
279  Angle += 3600;
280  while( Angle >= 3600 )
281  Angle -= 3600;
282  return Angle;
283 }
template<class T >
void NORMALIZE_ANGLE_180 ( T &  Angle)
inline

Definition at line 314 of file trigo.h.

References NormalizeAngle180().

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

315 {
316  Angle = NormalizeAngle180( Angle );
317 }
T NormalizeAngle180(T Angle)
Normalize angle to be in the -180.0 .. 180.0 range.
Definition: trigo.h:306
template<class T >
void NORMALIZE_ANGLE_90 ( T &  Angle)
inline

Definition at line 299 of file trigo.h.

References NormalizeAngle90().

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

300 {
301  Angle = NormalizeAngle90( Angle );
302 }
T NormalizeAngle90(T Angle)
Normalize angle to be in the -90.0 .. 90.0 range.
Definition: trigo.h:291
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 306 of file trigo.h.

Referenced by NORMALIZE_ANGLE_180(), and DialogEditModuleText::TransferDataToWindow().

307 {
308  while( Angle <= -1800 )
309  Angle += 3600;
310  while( Angle > 1800 )
311  Angle -= 3600;
312  return Angle;
313 }
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(), and DIALOG_MODEDIT_FP_BODY_ITEM_PROPERTIES::Validate().

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

Referenced by NORMALIZE_ANGLE_90().

292 {
293  while( Angle < -900 )
294  Angle += 1800;
295  while( Angle > 900 )
296  Angle -= 1800;
297  return Angle;
298 }
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(), and NORMALIZE_ANGLE_POS().

234 {
235  while( Angle < 0 )
236  Angle += 3600;
237  while( Angle >= 3600 )
238  Angle -= 3600;
239  return Angle;
240 }
double RAD2DEG ( double  rad)
inline
void RotatePoint ( int *  pX,
int *  pY,
double  angle 
)

Definition at line 317 of file trigo.cpp.

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

Referenced by DXF2BRD_CONVERTER::addArc(), PCB_BASE_FRAME::AddPad(), CINFO3D_VISU::AddShapeWithClearanceToContainer(), 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(), BuildUnconnectedThermalStubsPolygonList(), LIB_ARC::calcEdit(), DRC::checkClearancePadToPad(), DRC::checkClearanceSegmToPad(), DRAWSEGMENT::computeArcBBox(), PSLIKE_PLOTTER::computeTextParameters(), convertOblong2Segment(), ConvertOutlineToPolygon(), AM_PRIMITIVE::ConvertShapeToPolygon(), D_CODE::ConvertShapeToPolygon(), 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(), D_PAD::DrawShape(), PCB_EDIT_FRAME::Edit_Gap(), GBR_TO_PCB_EXPORTER::export_segarc_copper_item(), export_vrml_edge_module(), 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(), 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(), GERBER_DRAW_ITEM::GetXYPosition(), GRArc(), GRCSegm(), GRFilledArc(), D_PAD::HitTest(), idf_export_module(), InitialiseDragParameters(), C3D_RENDER_RAYTRACING::insert3DPadHole(), EDA_RECT::Intersects(), LIB_ARC::Load(), SCH_LEGACY_PLUGIN_CACHE::loadArc(), LEGACY_PLUGIN::loadPAD(), MODULE::MoveAnchorPosition(), MoveMarkedItemsExactly(), OGL_draw_half_open_cylinder(), EAGLE_PLUGIN::packagePad(), EAGLE_PLUGIN::packageSMD(), GPCB_FPL_CACHE::parseMODULE(), PCB_PARSER::parseMODULE_unchecked(), PCB_BASE_FRAME::PlacePad(), PCB_BASE_FRAME::PlaceTexteModule(), BRDITEMS_PLOTTER::Plot_1_EdgeModule(), SCH_JUNCTION::Rotate(), TEXTE_PCB::Rotate(), SCH_NO_CONNECT::Rotate(), MARKER_PCB::Rotate(), PCB_TARGET::Rotate(), SCH_MARKER::Rotate(), LIB_BEZIER::Rotate(), LIB_CIRCLE::Rotate(), TEXTE_MODULE::Rotate(), LIB_RECTANGLE::Rotate(), SCH_BUS_ENTRY_BASE::Rotate(), LIB_POLYLINE::Rotate(), TRACK::Rotate(), LIB_TEXT::Rotate(), SCH_BITMAP::Rotate(), LIB_ARC::Rotate(), SCH_LINE::Rotate(), SCH_FIELD::Rotate(), SCH_TEXT::Rotate(), SCH_SHEET_PIN::Rotate(), DIMENSION::Rotate(), DRAWSEGMENT::Rotate(), MODULE::Rotate(), LIB_FIELD::Rotate(), SCH_LABEL::Rotate(), SCH_GLOBALLABEL::Rotate(), SCH_HIERLABEL::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(), DRC::testTexts(), TEXTE_MODULE::TextHitTest(), EDA_TEXT::TextHitTest(), TraceFilledRectangle(), DIALOG_PAD_PROPERTIES::TransferDataFromWindow(), TransformArcToPolygon(), CINFO3D_VISU::TransformArcToSegments(), TEXTE_PCB::TransformBoundingBoxWithClearanceToPolygon(), TransformCircleToPolygon(), TransformRingToPolygon(), TransformRoundedEndsSegmentToPolygon(), DRAWSEGMENT::TransformShapeWithClearanceToPolygon(), and D_PAD::TransformShapeWithClearanceToPolygon().

318 {
319  int tmp;
320 
322 
323  // Cheap and dirty optimizations for 0, 90, 180, and 270 degrees.
324  if( angle == 0 )
325  return;
326 
327  if( angle == 900 ) /* sin = 1, cos = 0 */
328  {
329  tmp = *pX;
330  *pX = *pY;
331  *pY = -tmp;
332  }
333  else if( angle == 1800 ) /* sin = 0, cos = -1 */
334  {
335  *pX = -*pX;
336  *pY = -*pY;
337  }
338  else if( angle == 2700 ) /* sin = -1, cos = 0 */
339  {
340  tmp = *pX;
341  *pX = -*pY;
342  *pY = tmp;
343  }
344  else
345  {
346  double fangle = DECIDEG2RAD( angle );
347  double sinus = sin( fangle );
348  double cosinus = cos( fangle );
349  double fpx = (*pY * sinus ) + (*pX * cosinus );
350  double fpy = (*pY * cosinus ) - (*pX * sinus );
351  *pX = KiROUND( fpx );
352  *pY = KiROUND( fpy );
353  }
354 }
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:106
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 357 of file trigo.cpp.

References RotatePoint().

358 {
359  int ox, oy;
360 
361  ox = *pX - cx;
362  oy = *pY - cy;
363 
364  RotatePoint( &ox, &oy, angle );
365 
366  *pX = ox + cx;
367  *pY = oy + cy;
368 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
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(), wxPoint::x, and wxPoint::y.

78 {
79  RotatePoint( &point->x, &point->y, angle );
80 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
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 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:317
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
void RotatePoint ( VECTOR2I point,
const VECTOR2I centre,
double  angle 
)

Definition at line 383 of file trigo.cpp.

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

384 {
385  wxPoint c( centre.x, centre.y );
386  wxPoint p( point.x, point.y );
387 
388  RotatePoint(&p, c, angle);
389 
390  point.x = p.x;
391  point.y = p.y;
392 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
void RotatePoint ( wxPoint point,
const wxPoint centre,
double  angle 
)

Definition at line 371 of file trigo.cpp.

References RotatePoint(), wxPoint::x, and wxPoint::y.

372 {
373  int ox, oy;
374 
375  ox = point->x - centre.x;
376  oy = point->y - centre.y;
377 
378  RotatePoint( &ox, &oy, angle );
379  point->x = ox + centre.x;
380  point->y = oy + centre.y;
381 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
void RotatePoint ( double *  pX,
double *  pY,
double  angle 
)

Definition at line 409 of file trigo.cpp.

References DECIDEG2RAD(), and NORMALIZE_ANGLE_POS().

410 {
411  double tmp;
412 
414 
415  // Cheap and dirty optimizations for 0, 90, 180, and 270 degrees.
416  if( angle == 0 )
417  return;
418 
419  if( angle == 900 ) /* sin = 1, cos = 0 */
420  {
421  tmp = *pX;
422  *pX = *pY;
423  *pY = -tmp;
424  }
425  else if( angle == 1800 ) /* sin = 0, cos = -1 */
426  {
427  *pX = -*pX;
428  *pY = -*pY;
429  }
430  else if( angle == 2700 ) /* sin = -1, cos = 0 */
431  {
432  tmp = *pX;
433  *pX = -*pY;
434  *pY = tmp;
435  }
436  else
437  {
438  double fangle = DECIDEG2RAD( angle );
439  double sinus = sin( fangle );
440  double cosinus = cos( fangle );
441 
442  double fpx = (*pY * sinus ) + (*pX * cosinus );
443  double fpy = (*pY * cosinus ) - (*pX * sinus );
444  *pX = fpx;
445  *pY = fpy;
446  }
447 }
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 395 of file trigo.cpp.

References RotatePoint().

396 {
397  double ox, oy;
398 
399  ox = *pX - cx;
400  oy = *pY - cy;
401 
402  RotatePoint( &ox, &oy, angle );
403 
404  *pX = ox + cx;
405  *pY = oy + cy;
406 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
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.

References wxPoint::x, and wxPoint::y.

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

References DECIDEG2RAD().

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

325 {
326  return r * sin( DECIDEG2RAD( a ) );
327 }
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 142 of file trigo.cpp.

References abs, dist, square(), wxPoint::x, and wxPoint::y.

Referenced by SCH_EAGLE_PLUGIN::addBusEntries(), convex2pointDRC(), 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().

144 {
145  // test for vertical or horizontal segment
146  if( aEnd.x == aStart.x )
147  {
148  // vertical segment
149  int ll = abs( aRefPoint.x - aStart.x );
150 
151  if( ll > aDist )
152  return false;
153 
154  // To have only one case to examine, ensure aEnd.y > aStart.y
155  if( aEnd.y < aStart.y )
156  std::swap( aStart.y, aEnd.y );
157 
158  if( aRefPoint.y <= aEnd.y && aRefPoint.y >= aStart.y )
159  return true;
160 
161  // there is a special case: x,y near an end point (distance < dist )
162  // the distance should be carefully calculated
163  if( (aStart.y - aRefPoint.y) < aDist )
164  {
165  double dd = square( aRefPoint.x - aStart.x) +
166  square( aRefPoint.y - aStart.y );
167  if( dd <= square( aDist ) )
168  return true;
169  }
170 
171  if( (aRefPoint.y - aEnd.y) < aDist )
172  {
173  double dd = square( aRefPoint.x - aEnd.x ) +
174  square( aRefPoint.y - aEnd.y );
175  if( dd <= square( aDist ) )
176  return true;
177  }
178  }
179  else if( aEnd.y == aStart.y )
180  {
181  // horizontal segment
182  int ll = abs( aRefPoint.y - aStart.y );
183 
184  if( ll > aDist )
185  return false;
186 
187  // To have only one case to examine, ensure xf > xi
188  if( aEnd.x < aStart.x )
189  std::swap( aStart.x, aEnd.x );
190 
191  if( aRefPoint.x <= aEnd.x && aRefPoint.x >= aStart.x )
192  return true;
193 
194  // there is a special case: x,y near an end point (distance < dist )
195  // the distance should be carefully calculated
196  if( (aStart.x - aRefPoint.x) <= aDist )
197  {
198  double dd = square( aRefPoint.x - aStart.x ) +
199  square( aRefPoint.y - aStart.y );
200  if( dd <= square( aDist ) )
201  return true;
202  }
203 
204  if( (aRefPoint.x - aEnd.x) <= aDist )
205  {
206  double dd = square( aRefPoint.x - aEnd.x ) +
207  square( aRefPoint.y - aEnd.y );
208  if( dd <= square( aDist ) )
209  return true;
210  }
211  }
212  else
213  {
214  // oblique segment:
215  // First, we need to calculate the distance between the point
216  // and the line defined by aStart and aEnd
217  // this dist should be < dist
218  //
219  // find a,slope such that aStart and aEnd lie on y = a + slope*x
220  double slope = (double) (aEnd.y - aStart.y) / (aEnd.x - aStart.x);
221  double a = (double) aStart.y - slope * aStart.x;
222  // find c,orthoslope such that (x,y) lies on y = c + orthoslope*x,
223  // where orthoslope=(-1/slope)
224  // to calculate xp, yp = near point from aRefPoint
225  // which is on the line defined by aStart, aEnd
226  double orthoslope = -1.0 / slope;
227  double c = (double) aRefPoint.y - orthoslope * aRefPoint.x;
228  // find nearest point to (x,y) on line defined by aStart, aEnd
229  double xp = (a - c) / (orthoslope - slope);
230  double yp = a + slope * xp;
231  // find distance to line, in fact the square of dist,
232  // because we just know if it is > or < aDist
233  double dd = square( aRefPoint.x - xp ) + square( aRefPoint.y - yp );
234  double dist = square( aDist );
235 
236  if( dd > dist ) // this reference point is not a good candiadte.
237  return false;
238 
239  // dd is < dist, therefore we should make a fine test
240  if( fabs( slope ) > 0.7 )
241  {
242  // line segment more vertical than horizontal
243  if( (aEnd.y > aStart.y && yp <= aEnd.y && yp >= aStart.y) ||
244  (aEnd.y < aStart.y && yp >= aEnd.y && yp <= aStart.y) )
245  return true;
246  }
247  else
248  {
249  // line segment more horizontal than vertical
250  if( (aEnd.x > aStart.x && xp <= aEnd.x && xp >= aStart.x) ||
251  (aEnd.x < aStart.x && xp >= aEnd.x && xp <= aStart.x) )
252  return true;
253  }
254 
255  // Here, the test point is still a good candidate,
256  // however it is not "between" the end points of the segment.
257  // It is "outside" the segment, but it could be near a segment end point
258  // Therefore, we test the dist from the test point to each segment end point
259  dd = square( aRefPoint.x - aEnd.x ) + square( aRefPoint.y - aEnd.y );
260  if( dd <= dist )
261  return true;
262  dd = square( aRefPoint.x - aStart.x ) + square( aRefPoint.y - aStart.y );
263  if( dd <= dist )
264  return true;
265  }
266 
267  return false; // no hit
268 }
static const int dist[10][10]
Definition: dist.cpp:57
#define abs(a)
Definition: auxiliary.h:84
static double square(int x)
Definition: trigo.cpp:138