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, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <sstream>
23
27
28
30 const EDA_ANGLE& aRotation )
31{
32 VECTOR2I segVec{ 0, 0 };
33 int width;
34
35 // Find the major axis, without endcaps
36 if( aOverallSize.x > aOverallSize.y )
37 {
38 width = aOverallSize.y;
39 segVec.x = aOverallSize.x - width;
40 }
41 else
42 {
43 width = aOverallSize.x;
44 segVec.y = aOverallSize.y - width;
45 }
46
47 RotatePoint( segVec, aRotation );
48
49 return SHAPE_SEGMENT( aCenter - segVec / 2, aCenter + segVec / 2, width );
50}
51
52
53const std::string SHAPE_SEGMENT::Format( bool aCplusPlus ) const
54{
55 std::stringstream ss;
56
57 if( aCplusPlus )
58 {
59 ss << "SHAPE_SEGMENT( VECTOR2I( ";
60 ss << m_seg.A.x;
61 ss << ", ";
62 ss << m_seg.A.y;
63 ss << "), VECTOR2I( ";
64 ss << m_seg.B.x;
65 ss << ", ";
66 ss << m_seg.B.y;
67 ss << "), ";
68 ss << m_width;
69 ss << "); ";
70 }
71 else
72 {
73 ss << SHAPE::Format( aCplusPlus ) << " ";
74 ss << m_seg.A.x;
75 ss << " ";
76 ss << m_seg.A.y;
77 ss << " ";
78 ss << m_seg.B.x;
79 ss << " ";
80 ss << m_seg.B.y;
81 ss << " ";
82 ss << m_width;
83 }
84
85 return ss.str();
86}
87
88const std::string SHAPE_CIRCLE::Format( bool aCplusPlus ) const
89{
90 std::stringstream ss;
91
92 if( aCplusPlus )
93 {
94 ss << "SHAPE_CIRCLE( VECTOR2I( ";
95 ss << m_circle.Center.x;
96 ss << ", ";
97 ss << m_circle.Center.y;
98 ss << "), ";
99 ss << m_circle.Radius;
100 ss << "); ";
101 } else
102 {
103 ss << SHAPE::Format( aCplusPlus ) << " ";
104 ss << m_circle.Center.x;
105 ss << " ";
106 ss << m_circle.Center.y;
107 ss << " ";
108 ss << m_circle.Radius;
109 }
110 return ss.str();
111}
112
113
115 ERROR_LOC aErrorLoc ) const
116{
117 TransformCircleToPolygon( aBuffer, m_circle.Center, m_circle.Radius, aError, aErrorLoc );
118}
119
120
121bool SHAPE_SEGMENT::Is45Degree( EDA_ANGLE aTollerance ) const
122{
123 EDA_ANGLE mag = EDA_ANGLE( m_seg.A - m_seg.B ).Normalize180();
124
125 double f = fmod( mag.AsDegrees(), 45.0 );
126 double d = aTollerance.AsDegrees();
127
128 if( f >= 45.0 - d || f <= d )
129 {
130 return true;
131 }
132
133 return false;
134}
135
136
138 ERROR_LOC aErrorLoc ) const
139{
140 TransformOvalToPolygon( aBuffer, m_seg.A, m_seg.B, m_width, aError, aErrorLoc );
141}
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:43
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:225
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683