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 The 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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <array_options.h>
21
22#include <trigo.h>
23
24
26{
27 return m_nx * m_ny;
28}
29
30
32{
33 const int axisSize = m_horizontalThenVertical ? m_nx : m_ny;
34
35 int x = n % axisSize;
36 int y = n / axisSize;
37
38 // reverse on this row/col?
39 if( m_reverseNumberingAlternate && ( y % 2 ) )
40 x = axisSize - x - 1;
41
42 VECTOR2I coords( x, y );
43
44 return coords;
45}
46
47
49{
50 VECTOR2I point;
51
52 VECTOR2I coords = getGridCoords( n );
53
54 // swap axes if needed
56 std::swap( coords.x, coords.y );
57
58 point.x = coords.x * m_delta.x + coords.y * m_offset.x;
59 point.y = coords.y * m_delta.y + coords.x * m_offset.y;
60
61 if( std::abs( m_stagger ) > 1 )
62 {
63 const int stagger = std::abs( m_stagger );
64 const bool sr = m_stagger_rows;
65 const int stagger_idx = ( ( sr ? coords.y : coords.x ) % stagger );
66
67 VECTOR2I stagger_delta( ( sr ? m_delta.x : m_offset.x ), ( sr ? m_offset.y : m_delta.y ) );
68
69 // Stagger to the left/up if the sign of the stagger is negative
70 point += stagger_delta * copysign( stagger_idx, m_stagger ) / stagger;
71 }
72
73 return point;
74}
75
76
78{
80
81 // Bump the item by half the array size
82 if( m_centred )
83 {
84 // Get the array extents
85 const int arrayExtentX = ( m_nx - 1 ) * m_delta.x + ( m_ny - 1 ) * m_offset.x;
86 const int arrayExtentY = ( m_ny - 1 ) * m_delta.y + ( m_nx - 1 ) * m_offset.y;
87
88 point -= VECTOR2I( arrayExtentX, arrayExtentY ) / 2;
89 }
90
91 return { point, ANGLE_0 };
92}
93
94
95wxString ARRAY_GRID_OPTIONS::GetItemNumber( int n ) const
96{
97 wxString itemNum;
98
100 {
101 VECTOR2I coords = getGridCoords( n );
102
103 itemNum << m_pri_axis.GetItemNumber( coords.x );
104 itemNum << m_sec_axis.GetItemNumber( coords.y );
105 }
106 else
107 {
108 itemNum << m_pri_axis.GetItemNumber( n );
109 }
110
111 return itemNum;
112}
113
114
116{
117 return m_nPts;
118}
119
120
122{
123 EDA_ANGLE angle;
124
125 if( m_angle.IsZero() )
126 // angle is zero, divide evenly into m_nPts
127 angle = EDA_ANGLE( 360.0 * n / double( m_nPts ), DEGREES_T );
128 else
129 // n'th step
130 angle = EDA_ANGLE( m_angle.AsDegrees() * n, DEGREES_T );
131
132 angle += m_angleOffset;
133
134 if( m_clockwise )
135 angle = -angle;
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}
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:411
@ DEGREES_T
Definition eda_angle.h:31
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:400
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
wxString GetItemNumber(int n) const override
Get the position number (name) for the n'th array point.
VECTOR2I gtItemPosRelativeToItem0(int n) const
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.
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