KiCad PCB EDA Suite
geometry_utils.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #ifndef GEOMETRY_UTILS_H
31 #define GEOMETRY_UTILS_H
32 
33 #include <math/vector2d.h>
34 
35 class EDA_RECT;
36 
37 
46 int GetArcToSegmentCount( int aRadius, int aErrorMax, double aArcAngleDegree );
47 
59 double GetCircletoPolyCorrectionFactor( int aSegCountforCircle );
60 
72 template<typename T>
74 {
75  auto newVec = aVec;
76  const VECTOR2<T> absVec { std::abs( aVec.x ), std::abs( aVec.y ) };
77 
78  if ( absVec.x > absVec.y * 2 )
79  {
80  // snap along x-axis
81  newVec.y = 0;
82  }
83  else if ( absVec.y > absVec.x * 2 )
84  {
85  // snap onto y-axis
86  newVec.x = 0;
87  }
88  else if ( absVec.x > absVec.y )
89  {
90  // snap away from x-axis towards 45
91  newVec.y = std::copysign( aVec.x, aVec.y );
92  } else
93  {
94  // snap away from y-axis towards 45
95  newVec.x = std::copysign( aVec.y, aVec.x );
96  }
97 
98  return newVec;
99 }
100 
101 
115 bool ClipLine( const EDA_RECT *aClipBox, int &x1, int &y1, int &x2, int &y2 );
116 
117 
125 #define DOT_WIDTH_MILS 0.0254
126 
127 #define DOT_MARK_LEN( aLineWidth ) \
128  ( std::max( 1.0, IU_PER_MILS * DOT_WIDTH_MILS - aLineWidth ) )
129 
130 #define DASH_GAP_LEN( aLineWidth ) \
131  ( 3.0 * DOT_MARK_LEN( aLineWidth ) + ( 2.0 * aLineWidth ) )
132 
133 #define DASH_MARK_LEN( aLineWidth ) \
134  ( std::max( DASH_GAP_LEN( aLineWidth ), 5.0 * DOT_MARK_LEN( aLineWidth ) ) )
135 
136 
137 
138 #endif // #ifndef GEOMETRY_UTILS_H
139 
Class VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
#define abs(a)
Definition: auxiliary.h:84
bool ClipLine(const EDA_RECT *aClipBox, int &x1, int &y1, int &x2, int &y2)
Test if any part of a line falls within the bounds of a rectangle.
double GetCircletoPolyCorrectionFactor(int aSegCountforCircle)
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
VECTOR2< T > GetVectorSnapped45(const VECTOR2< T > &aVec)
Snap a vector onto the nearest 0, 45 or 90 degree line.
int GetArcToSegmentCount(int aRadius, int aErrorMax, double aArcAngleDegree)