回答編集履歴

1

サンプルコード追加

2020/10/13 10:59

投稿

fana
fana

スコア11658

test CHANGED
@@ -39,3 +39,223 @@
39
39
 
40
40
 
41
41
  (どっちが「右回転」でどっちが「左回転」かは座標軸の取り方によります.ご自身の世界の上で検算してみれば明確になるでしょう)
42
+
43
+
44
+
45
+ ---
46
+
47
+
48
+
49
+ 簡単なサンプルコード.
50
+
51
+ 4x4配列の中身を90度回転する.
52
+
53
+
54
+
55
+ ```C++
56
+
57
+ namespace
58
+
59
+ {//座標変換
60
+
61
+
62
+
63
+ //便宜上,
64
+
65
+ //4x4配列のindexの世界と,回転を考える世界の座標との対応を以下とした
66
+
67
+ //
68
+
69
+ // index ←→ 回転用
70
+
71
+ // 0 ←→ -2
72
+
73
+ // 1 ←→ -1
74
+
75
+ // 2 ←→ 1
76
+
77
+ // 3 ←→ 2
78
+
79
+
80
+
81
+ inline int Cvt2ForRotCoord( int ArrayIndexCoord )
82
+
83
+ {
84
+
85
+ return ( ArrayIndexCoord<=1 ? ArrayIndexCoord-2 : ArrayIndexCoord-1 );
86
+
87
+ }
88
+
89
+
90
+
91
+ inline int Cvt2IndexCoord( int ForRotCoord )
92
+
93
+ {
94
+
95
+ return ( ForRotCoord<0 ? ForRotCoord+2 : ForRotCoord+1 );
96
+
97
+ }
98
+
99
+
100
+
101
+ std::pair<int,int> Cvt2ForRotCoord( std::pair<int,int> ArrayIndexCoord )
102
+
103
+ {
104
+
105
+ return std::pair<int,int>(
106
+
107
+ Cvt2ForRotCoord( ArrayIndexCoord.first ),
108
+
109
+ Cvt2ForRotCoord( ArrayIndexCoord.second )
110
+
111
+ );
112
+
113
+ }
114
+
115
+
116
+
117
+ std::pair<int,int> Cvt2IndexCoord( std::pair<int,int> ForRotCoord )
118
+
119
+ {
120
+
121
+ return std::pair<int,int>(
122
+
123
+ Cvt2IndexCoord( ForRotCoord.first ),
124
+
125
+ Cvt2IndexCoord( ForRotCoord.second )
126
+
127
+ );
128
+
129
+ }
130
+
131
+ }
132
+
133
+
134
+
135
+ //座標値の90度回転
136
+
137
+ std::pair<int,int> Rotate90Deg( std::pair<int,int> P )
138
+
139
+ { //※どちらを用いるかで回転の向きが変わるよ
140
+
141
+ #if 0
142
+
143
+ return std::pair<int,int>( P.second, -P.first );
144
+
145
+ #else
146
+
147
+ return std::pair<int,int>( -P.second, P.first );
148
+
149
+ #endif
150
+
151
+ }
152
+
153
+
154
+
155
+ //配列内容の表示
156
+
157
+ void ShowArray( const char A[4][4] )
158
+
159
+ {
160
+
161
+ for( int y=0; y<4; ++y )
162
+
163
+ {
164
+
165
+ for( int x=0; x<4; ++x )
166
+
167
+ {
168
+
169
+ std::cout << '[' << A[y][x] << ']';
170
+
171
+ }
172
+
173
+ std::cout << std::endl;
174
+
175
+ }
176
+
177
+ }
178
+
179
+
180
+
181
+ int main()
182
+
183
+ {
184
+
185
+ char SrcArray[4][4] =
186
+
187
+ {
188
+
189
+ { '0','1','2','3' },
190
+
191
+ { '4','5','6','7' },
192
+
193
+ { '8','9','A','B' },
194
+
195
+ { 'C','D','E','F' },
196
+
197
+ };
198
+
199
+ std::cout << "Src:" << std::endl;
200
+
201
+ ShowArray( SrcArray );
202
+
203
+
204
+
205
+ //{90度,180度,270度,360度}まわしてみるテスト
206
+
207
+ for( int ith=1; ith<=4; ++ith )
208
+
209
+ {
210
+
211
+ char ResultArray[4][4] = {0}; //SrcArrayの内容を回転した結果をここに格納する
212
+
213
+
214
+
215
+ for( int y=0; y<4; ++y )
216
+
217
+ {
218
+
219
+ for( int x=0; x<4; ++x )
220
+
221
+ {
222
+
223
+ std::pair<int,int> ArrayIndexCoord( x,y );
224
+
225
+ auto ForRotCoord = Cvt2ForRotCoord( ArrayIndexCoord );
226
+
227
+
228
+
229
+ for( int i=0; i<ith; ++i )
230
+
231
+ { ForRotCoord = Rotate90Deg( ForRotCoord ); }
232
+
233
+
234
+
235
+ auto ResultIndexCoord = Cvt2IndexCoord( ForRotCoord );
236
+
237
+
238
+
239
+ ResultArray[ ResultIndexCoord.second ][ ResultIndexCoord.first ] = SrcArray[y][x];
240
+
241
+ }
242
+
243
+ }
244
+
245
+
246
+
247
+ std::cout << "Result:(" << ith*90 << "度回転)" << std::endl;
248
+
249
+ ShowArray( ResultArray );
250
+
251
+ }
252
+
253
+
254
+
255
+ std::cin.ignore();
256
+
257
+ return 0;
258
+
259
+ }
260
+
261
+ ```