回答編集履歴
4
A B D E F C G H I J
test
CHANGED
@@ -170,3 +170,81 @@
|
|
170
170
|
}
|
171
171
|
```
|
172
172
|
![アプリ動画](https://ddjkaamml8q8x.cloudfront.net/questions/2024-01-26/391ab493-e6d9-467f-936b-dc587cddb625.gif)
|
173
|
+
|
174
|
+
---
|
175
|
+
|
176
|
+
さらに追記
|
177
|
+
```Processing
|
178
|
+
int member2 = 5;
|
179
|
+
Staff[] staff = {
|
180
|
+
new Staff(0, "A"),
|
181
|
+
new Staff(50, "B"),
|
182
|
+
new Staff(100, "C"),
|
183
|
+
new Staff(150, "D"),
|
184
|
+
new Staff(200, "E"),
|
185
|
+
new Staff(250, "F"),
|
186
|
+
new Staff(300, "G"),
|
187
|
+
new Staff(350, "H"),
|
188
|
+
new Staff(400, "I"),
|
189
|
+
new Staff(450, "J")
|
190
|
+
};
|
191
|
+
|
192
|
+
|
193
|
+
void setup() {
|
194
|
+
size(500, 200);
|
195
|
+
textAlign(CENTER, CENTER);
|
196
|
+
textSize(24);
|
197
|
+
}
|
198
|
+
|
199
|
+
void draw() {
|
200
|
+
background(255);
|
201
|
+
|
202
|
+
for (int i = 0; i < staff.length; i++) {
|
203
|
+
fill(0);
|
204
|
+
text(staff[i].c, staff[i].x, 75, 50, 50);
|
205
|
+
noFill();
|
206
|
+
rect(staff[i].x, 75, 50, 50);
|
207
|
+
}
|
208
|
+
}
|
209
|
+
|
210
|
+
void mouseClicked() {
|
211
|
+
int clickedIndex = int(map(mouseX, 0, width, 0, 10));
|
212
|
+
println("clickedIndex:" + clickedIndex);
|
213
|
+
|
214
|
+
if (clickedIndex <= member2 && clickedIndex >= 0) {
|
215
|
+
Staff tmp = staff[clickedIndex]; // clickedIndex番目をとっておく
|
216
|
+
for (int i = clickedIndex + 1; i <= member2; i++) { // clickedIndexの次からmember2番目も含め回す
|
217
|
+
staff[i - 1] = staff[i]; // 1個前へ
|
218
|
+
}
|
219
|
+
staff[member2] = tmp; // 抜けたmember2番目にはめる
|
220
|
+
|
221
|
+
int[] xx = new int[member2 + 1]; // member2がインデックス(5)なら要素数は+1です(6)
|
222
|
+
//int[] xx = new int[staff.length]; // ややこしくなるぐらいなら全件見てもいいのではないか?
|
223
|
+
for (int i = 0; i < xx.length; i++) {
|
224
|
+
xx[i] = staff[i].x;
|
225
|
+
}
|
226
|
+
|
227
|
+
xx = sort(xx);
|
228
|
+
|
229
|
+
for (int i = 0; i < xx.length; i++) {
|
230
|
+
staff[i].x = xx[i];
|
231
|
+
}
|
232
|
+
|
233
|
+
printArray(staff); // 配列表示
|
234
|
+
}
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
class Staff {
|
239
|
+
int x;
|
240
|
+
String c;
|
241
|
+
Staff(int x, String c) {
|
242
|
+
this.x = x;
|
243
|
+
this.c = c;
|
244
|
+
}
|
245
|
+
@Override String toString() { // toStringとprintArrayでデバッグしやすくなる
|
246
|
+
return c + " x:" + x;
|
247
|
+
}
|
248
|
+
}
|
249
|
+
```
|
250
|
+
![デバッグ動画](https://ddjkaamml8q8x.cloudfront.net/questions/2024-02-03/f1706325-09df-4104-bc2c-99cd4a1c2581.gif)
|
3
コメ消しちゃった
test
CHANGED
@@ -139,6 +139,7 @@
|
|
139
139
|
}
|
140
140
|
|
141
141
|
void mouseClicked() {
|
142
|
+
// mouseXを雑にcardsインデックス(0~4)に変換
|
142
143
|
int index = int(map(mouseX, 0, width, 0, 5));
|
143
144
|
println(index);
|
144
145
|
|
2
ソートし入れなおすようなアプローチ
test
CHANGED
@@ -118,11 +118,11 @@
|
|
118
118
|
追記 配列がそのまま並び順になるように変更できないでしょうか?(例えばこんな)
|
119
119
|
```Processing
|
120
120
|
Card[] cards = {
|
121
|
-
new Card(#ff0000),
|
121
|
+
new Card(5, #ff0000),
|
122
|
-
new Card(#00ff00),
|
122
|
+
new Card(65, #00ff00),
|
123
|
-
new Card(#0000ff),
|
123
|
+
new Card(125, #0000ff),
|
124
|
-
new Card(#ffff00),
|
124
|
+
new Card(185, #ffff00),
|
125
|
-
new Card(#00ffff)
|
125
|
+
new Card(245, #00ffff)
|
126
126
|
};
|
127
127
|
|
128
128
|
void setup() {
|
@@ -134,13 +134,11 @@
|
|
134
134
|
|
135
135
|
for (int i = 0; i < cards.length; i++) {
|
136
136
|
fill(cards[i].c);
|
137
|
-
// 配列がそのまま並び順
|
138
|
-
rect(
|
137
|
+
rect(cards[i].x, 75, 50, 50);
|
139
138
|
}
|
140
139
|
}
|
141
140
|
|
142
141
|
void mouseClicked() {
|
143
|
-
// mouseXを雑にcardsインデックス(0~4)に変換
|
144
142
|
int index = int(map(mouseX, 0, width, 0, 5));
|
145
143
|
println(index);
|
146
144
|
|
@@ -150,11 +148,22 @@
|
|
150
148
|
cards[i - 1] = cards[i];
|
151
149
|
}
|
152
150
|
cards[cards.length - 1] = tmp;
|
151
|
+
|
152
|
+
int[] xx = new int[cards.length]; // x用の配列を用意
|
153
|
+
for (int i = 0; i < cards.length; i++) {
|
154
|
+
xx[i] = cards[i].x; // 詰める
|
155
|
+
}
|
156
|
+
xx = sort(xx); // ソート
|
157
|
+
for (int i = 0; i < cards.length; i++) {
|
158
|
+
cards[i].x = xx[i]; // 詰め直し
|
159
|
+
}
|
153
160
|
}
|
154
161
|
|
155
162
|
class Card {
|
163
|
+
int x;
|
156
164
|
color c;
|
157
|
-
Card(color c) {
|
165
|
+
Card(int x, color c) {
|
166
|
+
this.x = x;
|
158
167
|
this.c = c;
|
159
168
|
}
|
160
169
|
}
|
1
追記 配列がそのまま並び順
test
CHANGED
@@ -112,3 +112,51 @@
|
|
112
112
|
|position||||||
|
113
113
|
|
114
114
|
あるいは今問題になっている部分を極々単純化した、実行できるプログラムを提示いただけると話が通じやすいです。
|
115
|
+
|
116
|
+
---
|
117
|
+
|
118
|
+
追記 配列がそのまま並び順になるように変更できないでしょうか?(例えばこんな)
|
119
|
+
```Processing
|
120
|
+
Card[] cards = {
|
121
|
+
new Card(#ff0000),
|
122
|
+
new Card(#00ff00),
|
123
|
+
new Card(#0000ff),
|
124
|
+
new Card(#ffff00),
|
125
|
+
new Card(#00ffff)
|
126
|
+
};
|
127
|
+
|
128
|
+
void setup() {
|
129
|
+
size(300, 200);
|
130
|
+
}
|
131
|
+
|
132
|
+
void draw() {
|
133
|
+
background(255);
|
134
|
+
|
135
|
+
for (int i = 0; i < cards.length; i++) {
|
136
|
+
fill(cards[i].c);
|
137
|
+
// 配列がそのまま並び順
|
138
|
+
rect(i * width / cards.length + 5, 75, 50, 50);
|
139
|
+
}
|
140
|
+
}
|
141
|
+
|
142
|
+
void mouseClicked() {
|
143
|
+
// mouseXを雑にcardsインデックス(0~4)に変換
|
144
|
+
int index = int(map(mouseX, 0, width, 0, 5));
|
145
|
+
println(index);
|
146
|
+
|
147
|
+
// 選択カードを配列の末尾に移動
|
148
|
+
Card tmp = cards[index];
|
149
|
+
for (int i = index + 1; i < cards.length; i++) {
|
150
|
+
cards[i - 1] = cards[i];
|
151
|
+
}
|
152
|
+
cards[cards.length - 1] = tmp;
|
153
|
+
}
|
154
|
+
|
155
|
+
class Card {
|
156
|
+
color c;
|
157
|
+
Card(color c) {
|
158
|
+
this.c = c;
|
159
|
+
}
|
160
|
+
}
|
161
|
+
```
|
162
|
+
![アプリ動画](https://ddjkaamml8q8x.cloudfront.net/questions/2024-01-26/391ab493-e6d9-467f-936b-dc587cddb625.gif)
|