-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
301 lines (258 loc) · 6.97 KB
/
sketch.js
File metadata and controls
301 lines (258 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
let maxN = 100; // -n ~ n revolutions per period
let step = 0;
// status: 1 - input, 2 - process, 3- solve, 4 - show result, -1 - error
let cwx = [];
let cwy = [];
let ccwx = [];
let ccwy = [];
let dotx = [];
let doty = [];
let ix = [0];
let iy = [0];
let targetN = -1;
let procN = 0;
let t = 0;
let n = 30;
let unit = 0.05;
let showOriginal = false;
let focusMode = true;
let scaleFactor = 7;
function setup() {
let canvas = createCanvas(800, 600); // create a 800 * 600 pixel canvas
canvas.parent('canvas-holder'); // move to the right div
reset();
}
function reset() {
resetMatrix();
background(0);
step = 1;
targetN = 0;
procN = 0;
t = 0;
n = 30; // 30 pairs(60 in total) vectors will be rotated
scaleFactor = 7; // scale factor for focus mode
focusMode = true;
showOriginal = false;
ix = [];
iy = [];
dotx = [];
doty = [];
cwx = new Array(maxN + 1).fill(0);
cwy = new Array(maxN + 1).fill(0);
ccwx = new Array(maxN + 1).fill(0);
ccwy = new Array(maxN + 1).fill(0);
stroke(0);
fill(0);
ellipseMode(RADIUS);
}
function draw() {
if (step === 1) {
if (mouseIsPressed)
input();
} else if (step === 2) {
process(procN);
procN++;
} else if (step === 3) {
textSize(32);
fill(255);
if (targetN <= maxN) {
background(0);
text("Loading : " + targetN + " / " + maxN, width / 2 - 100, height - 70);
solve(targetN);
targetN++;
} else {
step = 4;
}
} else if (step === 4) { // show result
background(0);
if (focusMode)
focus_result(n, t);
else
result(n, t);
t += 0.001;
fill('#FFFF00');
noStroke();
} else { // unexpected step
background(0);
fill(255);
textSize(32);
text("Error! Please press 'R' to reset.", width / 2 - 100, height / 2);
}
}
function input() {
if (ix.length === 0) {
ix.push(mouseX);
iy.push(mouseY);
return;
}
let prevx = ix[ix.length - 1];
let prevy = iy[iy.length - 1];
if (dist(mouseX, mouseY, prevx, prevy) > 0) {
ix.push(mouseX);
iy.push(mouseY);
stroke('#FF00FF');
line(prevx, prevy, ix[ix.length - 1], iy[iy.length - 1]);
}
}
function end_input() {
if(ix.length === 0) {
step = -1;
} else {
ix.push(ix[0]);
iy.push(iy[0]);
procN = 0;
step = 2;
}
}
function process(i) {
if (i >= ix.length - 1) {
targetN = 0;
step = 3;
return;
}
// split every line into equal small pieces
let l = dist(ix[i], iy[i], ix[i + 1], iy[i + 1]);
for (let j = 0; j < l; j += unit) {
let tempX = map(j, 0, l, ix[i], ix[i + 1]);
let tempY = map(j, 0, l, iy[i], iy[i + 1]);
dotx.push(tempX);
doty.push(tempY);
}
fill('#0000FF');
noStroke();
circle(ix[i + 1], iy[i + 1], 5);
}
// solve the initial position of the vector that rotates currTargetN times per period
function solve(currTargetN) {
let transx = new Array(dotx.length).fill(0);
let transy = new Array(doty.length).fill(0);
let sumx = 0;
let sumy = 0;
if (currTargetN === 0) {
for (let i = 0; i < dotx.length; i++) {
sumx += dotx[i];
sumy += doty[i];
}
cwx[0] = sumx / dotx.length;
cwy[0] = sumy / dotx.length;
return;
}
// solve cw
for (let i = 0; i < dotx.length; i++) {
let time = map(i, 0, dotx.length, 0, 2 * PI);
let alpha = currTargetN * time;
transx[i] = dotx[i] * cos(alpha);
transy[i] = dotx[i] * sin(alpha);
sumx += transx[i];
sumy += transy[i];
}
cwx[currTargetN] += sumx / dotx.length;
cwy[currTargetN] += sumy / dotx.length;
ccwx[currTargetN] += sumx / dotx.length;
ccwy[currTargetN] += -sumy / dotx.length;
sumx = 0; sumy = 0;
// solve ccw
for (let i = 0; i < doty.length; i++) {
let time = map(i, 0, doty.length, 0, 2 * PI);
let alpha = currTargetN * time;
transy[i] = doty[i] * cos(alpha);
transx[i] = -doty[i] * sin(alpha);
sumx += transx[i];
sumy += transy[i];
}
cwx[currTargetN] += sumx / dotx.length;
cwy[currTargetN] += sumy / dotx.length;
ccwx[currTargetN] += -sumx / dotx.length;
ccwy[currTargetN] += sumy / dotx.length;
}
function result(m, currentTime) {
translate(cwx[0], cwy[0]);
// show trajectory
for (let tem = 0; tem < currentTime; tem += 0.001) {
push();
for (let i = 1; i <= m; i++) {
rotate(2 * PI * i * tem);
translate(cwx[i], cwy[i]);
rotate(-2 * PI * i * tem);
rotate(-2 * PI * i * tem);
translate(ccwx[i], ccwy[i]);
rotate(2 * PI * i * tem);
}
fill('#2299FF');
noStroke();
if (focusMode) circle(0, 0, 2 / scaleFactor);
else circle(0, 0, 1.5);
pop();
}
translate(-cwx[0], -cwy[0]);
if (showOriginal) {
for (let i = 0; i < dotx.length; i += 10 / unit) {
noStroke();
fill('#00FF00');
circle(dotx[idx], doty[idx], 3 / scaleFactor);
}
}
translate(cwx[0], cwy[0]);
noFill();
if (focusMode) strokeWeight(3 / scaleFactor);
else strokeWeight(2);
for (let i = 1; i <= m; i++) {
stroke(constrain(25 * i, 0, 255), constrain(255 - 20 * i, 0, 255), constrain(200 - 2 * i, 100, 255));
rotate(2 * PI * i * currentTime);
line(0, 0, cwx[i], cwy[i]);
circle(0, 0, dist(0, 0, cwx[i], cwy[i]));
translate(cwx[i], cwy[i]);
rotate(-2 * PI * i * currentTime);
rotate(-2 * PI * i * currentTime);
line(0, 0, ccwx[i], ccwy[i]);
circle(0, 0, dist(0, 0, ccwx[i], ccwy[i]));
translate(ccwx[i], ccwy[i]);
rotate(2 * PI * i * currentTime);
}
}
function focus_result(m, currentTime) {
scale(scaleFactor);
let end_x = cwx[0];
let end_y = cwy[0];
// calculate the end point of the vector
for (let i = 1; i <= m; i++) {
end_x += cwx[i] * cos(-2 * PI * i * currentTime);
end_y += cwx[i] * -sin(-2 * PI * i * currentTime);
end_x += cwy[i] * sin(-2 * PI * i * currentTime);
end_y += cwy[i] * cos(-2 * PI * i * currentTime);
end_x += ccwx[i] * cos(2 * PI * i * currentTime);
end_y += ccwx[i] * -sin(2 * PI * i * currentTime);
end_x += ccwy[i] * sin(2 * PI * i * currentTime);
end_y += ccwy[i] * cos(2 * PI * i * currentTime);
}
// translate the end point to the center of the canvas
translate(-end_x + width / 2 / scaleFactor, -end_y + height / 2 / scaleFactor);
result(m, currentTime);
}
function keyPressed() {
console.log("Key:", key, "KeyCode:", keyCode);
if (key === 'r' || key === 'R') {
reset();
}
if (step === 1 && keyCode === 13) {
end_input();
return;
}
if (step !== 4) return;
if (keyCode === 38 ) {
if (n < maxN) n++;
console.log("Current rotating vectors: " + 2*n);
} else if (keyCode === 40) {
if (n > 1) n--;
console.log("Current rotating vectors: " + 2*n);
} else if (key === 'f' || key === 'F') {
focusMode = !focusMode;
return false;
} else if (keyCode === 39) {
if (scaleFactor <= 15) scaleFactor += 0.02;
console.log("Current scaleFactor: " + scaleFactor);
} else if (keyCode === 37) {
if (scaleFactor >= 0.7) scaleFactor -= 0.02;
console.log("Current scaleFactor: " + scaleFactor);
}
}