KiCad PCB EDA Suite
Loading...
Searching...
No Matches
shape_segment.cpp
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) 2017 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Tomasz Wlostowski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <sstream>
27
31
32
34 const EDA_ANGLE& aRotation )
35{
36 VECTOR2I segVec{ 0, 0 };
37 int width;
38
39 // Find the major axis, without endcaps
40 if( aOverallSize.x > aOverallSize.y )
41 {
42 width = aOverallSize.y;
43 segVec.x = aOverallSize.x - width;
44 }
45 else
46 {
47 width = aOverallSize.x;
48 segVec.y = aOverallSize.y - width;
49 }
50
51 RotatePoint( segVec, aRotation );
52
53 return SHAPE_SEGMENT( aCenter - segVec / 2, aCenter + segVec / 2, width );
54}
55
56
57const std::string SHAPE_SEGMENT::Format( bool aCplusPlus ) const
58{
59 std::stringstream ss;
60
61 if( aCplusPlus )
62 {
63 ss << "SHAPE_SEGMENT( VECTOR2I( ";
64 ss << m_seg.A.x;
65 ss << ", ";
66 ss << m_seg.A.y;
67 ss << "), VECTOR2I( ";
68 ss << m_seg.B.x;
69 ss << ", ";
70 ss << m_seg.B.y;
71 ss << "), ";
72 ss << m_width;
73 ss << "); ";
74 }
75 else
76 {
77 ss << SHAPE::Format( aCplusPlus ) << " ";
78 ss << m_seg.A.x;
79 ss << " ";
80 ss << m_seg.A.y;
81 ss << " ";
82 ss << m_seg.B.x;
83 ss << " ";
84 ss << m_seg.B.y;
85 ss << " ";
86 ss << m_width;
87 }
88
89 return ss.str();
90}
91
92const std::string SHAPE_CIRCLE::Format( bool aCplusPlus ) const
93{
94 std::stringstream ss;
95
96 if( aCplusPlus )
97 {
98 ss << "SHAPE_CIRCLE( VECTOR2I( ";
99 ss << m_circle.Center.x;
100 ss << ", ";
101 ss << m_circle.Center.y;
102 ss << "), ";
103 ss << m_circle.Radius;
104 ss << "); ";
105 } else
106 {
107 ss << SHAPE::Format( aCplusPlus ) << " ";
108 ss << m_circle.Center.x;
109 ss << " ";
110 ss << m_circle.Center.y;
111 ss << " ";
112 ss << m_circle.Radius;
113 }
114 return ss.str();
115}
116
117
119 ERROR_LOC aErrorLoc ) const
120{
121 TransformCircleToPolygon( aBuffer, m_circle.Center, m_circle.Radius, aError, aErrorLoc );
122}
123
124
125bool SHAPE_SEGMENT::Is45Degree( EDA_ANGLE aTollerance ) const
126{
127 EDA_ANGLE mag = EDA_ANGLE( m_seg.A - m_seg.B ).Normalize180();
128
129 double f = fmod( mag.AsDegrees(), 45.0 );
130 double d = aTollerance.AsDegrees();
131
132 if( f >= 45.0 - d || f <= d )
133 {
134 return true;
135 }
136
137 return false;
138}
139
140
142 ERROR_LOC aErrorLoc ) const
143{
144 TransformOvalToPolygon( aBuffer, m_seg.A, m_seg.B, m_width, aError, aErrorLoc );
145}
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
double AsDegrees() const
Definition eda_angle.h:116
EDA_ANGLE Normalize180()
Definition eda_angle.h:268
void TransformToPolygon(SHAPE_POLY_SET &aBuffer, int aError, ERROR_LOC aErrorLoc) const override
Fills a SHAPE_POLY_SET with a polygon representation of this shape.
virtual const std::string Format(bool aCplusPlus=true) const override
Represent a set of closed polygons.
bool Is45Degree(EDA_ANGLE aTollerance=EDA_ANGLE(1.0, DEGREES_T)) const
virtual const std::string Format(bool aCplusPlus=true) const override
void TransformToPolygon(SHAPE_POLY_SET &aBuffer, int aError, ERROR_LOC aErrorLoc) const override
Fills a SHAPE_POLY_SET with a polygon representation of this shape.
static SHAPE_SEGMENT BySizeAndCenter(const VECTOR2I &aSize, const VECTOR2I &aCenter, const EDA_ANGLE &aRotation)
virtual const std::string Format(bool aCplusPlus=true) const
Definition shape.cpp:47
void TransformCircleToPolygon(SHAPE_LINE_CHAIN &aBuffer, const VECTOR2I &aCenter, int aRadius, int aError, ERROR_LOC aErrorLoc, int aMinSegCount=0)
Convert a circle to a polygon, using multiple straight lines.
void TransformOvalToPolygon(SHAPE_POLY_SET &aBuffer, const VECTOR2I &aStart, const VECTOR2I &aEnd, int aWidth, int aError, ERROR_LOC aErrorLoc, int aMinSegCount=0)
Convert a oblong shape to a polygon, using multiple segments.
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:229
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695