KiCad PCB EDA Suite
Loading...
Searching...
No Matches
array_options.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) 2019 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <array_options.h>
25
26#include <trigo.h>
27
28
30{
31 return m_nx * m_ny;
32}
33
34
36{
37 const int axisSize = m_horizontalThenVertical ? m_nx : m_ny;
38
39 int x = n % axisSize;
40 int y = n / axisSize;
41
42 // reverse on this row/col?
43 if( m_reverseNumberingAlternate && ( y % 2 ) )
44 x = axisSize - x - 1;
45
46 VECTOR2I coords( x, y );
47
48 return coords;
49}
50
51
53{
54 VECTOR2I point;
55
56 VECTOR2I coords = getGridCoords( n );
57
58 // swap axes if needed
60 std::swap( coords.x, coords.y );
61
62 point.x = coords.x * m_delta.x + coords.y * m_offset.x;
63 point.y = coords.y * m_delta.y + coords.x * m_offset.y;
64
65 if( std::abs( m_stagger ) > 1 )
66 {
67 const int stagger = std::abs( m_stagger );
68 const bool sr = m_stagger_rows;
69 const int stagger_idx = ( ( sr ? coords.y : coords.x ) % stagger );
70
71 VECTOR2I stagger_delta( ( sr ? m_delta.x : m_offset.x ), ( sr ? m_offset.y : m_delta.y ) );
72
73 // Stagger to the left/up if the sign of the stagger is negative
74 point += stagger_delta * copysign( stagger_idx, m_stagger ) / stagger;
75 }
76
77 return point;
78}
79
80
82{
84
85 // Bump the item by half the array size
86 if( m_centred )
87 {
88 // Get the array extents
89 const int arrayExtentX = ( m_nx - 1 ) * m_delta.x + ( m_ny - 1 ) * m_offset.x;
90 const int arrayExtentY = ( m_ny - 1 ) * m_delta.y + ( m_nx - 1 ) * m_offset.y;
91
92 std::cout << "Subtracting " << VECTOR2I( arrayExtentX, arrayExtentY ) / 2 << std::endl;
93 point -= VECTOR2I( arrayExtentX, arrayExtentY ) / 2;
94 }
95
96 return { point, ANGLE_0 };
97}
98
99
101{
102 wxString itemNum;
103
105 {
106 VECTOR2I coords = getGridCoords( n );
107
108 itemNum << m_pri_axis.GetItemNumber( coords.x );
109 itemNum << m_sec_axis.GetItemNumber( coords.y );
110 }
111 else
112 {
113 itemNum << m_pri_axis.GetItemNumber( n );
114 }
115
116 return itemNum;
117}
118
119
121{
122 return m_nPts;
123}
124
125
127{
128 EDA_ANGLE angle;
129
130 if( m_angle.IsZero() )
131 // angle is zero, divide evenly into m_nPts
132 angle = EDA_ANGLE( 360.0 * n / double( m_nPts ), DEGREES_T );
133 else
134 // n'th step
135 angle = EDA_ANGLE( m_angle.AsDegrees() * n, DEGREES_T );
136
137 VECTOR2I new_pos = aPos;
138 RotatePoint( new_pos, m_centre, angle );
139
140 // take off the rotation (but not the translation) if needed
141 if( !m_rotateItems )
142 angle = ANGLE_0;
143
144 return { new_pos - aPos, angle };
145}
146
147
149{
150 return m_axis.GetItemNumber( aN );
151}
wxString GetItemNumber(int n) const
Get the position number (name) for the n'th axis point.
Definition: array_axis.cpp:135
double AsDegrees() const
Definition: eda_angle.h:113
bool IsZero() const
Definition: eda_angle.h:133
static constexpr EDA_ANGLE ANGLE_0
Definition: eda_angle.h:401
@ DEGREES_T
Definition: eda_angle.h:31
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:390
long m_nPts
number of point in the array
EDA_ANGLE m_angle
angle between points, or 0 for each point separated by this value (decideg)
wxString GetItemNumber(int n) const override
Get the position number (name) for the n'th array point.
int GetArraySize() const override
The number of points in this array.
TRANSFORM GetTransform(int aN, const VECTOR2I &aPos) const override
Get the transform of the n-th point in the array.
VECTOR2I getGridCoords(int n) const
bool m_reverseNumberingAlternate
wxString GetItemNumber(int n) const override
Get the position number (name) for the n'th array point.
ARRAY_AXIS m_sec_axis
VECTOR2I gtItemPosRelativeToItem0(int n) const
ARRAY_AXIS m_pri_axis
int GetArraySize() const override
The number of points in this array.
TRANSFORM GetTransform(int aN, const VECTOR2I &aPos) const override
Get the transform of the n-th point in the array.
Transform applied to an object by this array.
Definition: array_options.h:60
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:691