-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_25.js
189 lines (148 loc) · 4.16 KB
/
day_25.js
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
// (c) 2021 Joseph HENRY
// This code is licensed under MIT license (see LICENSE for details)
function easeInOutCubic(x) {
return x < 0.5 ? 4 * x * x * x : 1 - pow(-2 * x + 2, 3) / 2;
}
class Permutation {
constructor(i, j, startTime, duration) {
this.indices = [i, j];
this.startTime = startTime;
this.duration = duration;
}
/**
* Return the current duration of the permutation since the starting time
*/
getCurrentDuration() {
return window.performance.now() - this.startTime;
}
/**
* Return true if the permutation is over
*/
isOver() {
return this.getCurrentDuration() >= this.duration;
}
/**
* Return a normalized duration value between [0, 1]
*/
getNormalizedTime() {
return this.getCurrentDuration() / this.duration;
}
/**
* Return true if i is the first index of the permutation
*/
isFirst(i) {
return i == this.indices[0];
}
/**
* Return the distance between the two indices
* Can be negative
*/
getDistance() {
return this.indices[1] - this.indices[0];
}
}
class Set {
constructor(size) {
this.elements = new Array(size);
for (let i = 0; i < size; i++) {
this.elements
}
// Store a list of permutations
this.permutations = [];
}
/**
* Return the permutation of the corresponding element at index i
* Return null if not found
*/
getPermutation(i) {
for (let j = this.permutations.length - 1; j >= 0; j--) {
const perm = this.permutations[j];
if (perm.indices.includes(i)) {
return perm;
}
}
return null;
}
/**
* Permute two elements at location i and j
* Return true if succeded
*/
permute(i, j, duration) {
// Check for collision
if (this.getPermutation(i) || this.getPermutation(j)) {
return false;
}
// Otherwise add a new permutation
this.permutations.push(new Permutation(i, j, window.performance.now(), duration));
return true;
}
randomPermute(duration) {
const i = int(random(this.elements.length));
const j = int(random(this.elements.length));
this.permute(i, j, duration);
}
update() {
for (let i = this.permutations.length - 1; i >= 0; i--) {
const perm = this.permutations[i];
// If the permutation is over, delete it
if (perm.isOver()) {
this.permutations.pop();
}
}
}
display(centerX, centerY, length) {
const squareSize = length / this.elements.length;
push();
translate(centerX - length / 2 + squareSize / 2, centerY);
fill("#ff9900");
stroke("#003344");
strokeWeight(10);
rectMode(CENTER);
for (let i = 0; i < this.elements.length; i++) {
const x = i * squareSize;
const perm = this.getPermutation(i);
if (perm) {
const n = easeInOutCubic(perm.getNormalizedTime());
const distance = perm.getDistance();
const factor = perm.isFirst(i) ? 1 : -1;
const radius = (distance / 2) * squareSize;
const circleCenterX = x + radius * factor;
const angle = n * PI + (perm.isFirst(i) ? PI : 0);
const circleX = Math.cos(angle) * Math.abs(radius);
const circleY = Math.sin(angle) * Math.abs(radius);
push();
translate(circleCenterX + circleX, circleY);
rotate(n * TWO_PI * factor);
square(0, 0, squareSize);
pop();
} else {
square(x, 0, squareSize);
}
}
pop();
}
}
const sets = [];
const nSets = 10;
const size = 300;
let time = 0;
function setup() {
createCanvas(500, 500);
for (let i = 0; i < nSets; i++) {
sets.push(new Set(nSets));
}
}
function draw() {
background("#003344");
const rowHeight = size / nSets;
const startY = height / 2 - size / 2 + rowHeight / 2;
for (let i = 0; i < nSets; i++) {
sets[i].display(width / 2, startY + i * rowHeight, 300);
sets[i].update();
}
// Every 2s, add some random permutations
if (millis() - time > 2000) {
for (let i = 0; i < 10; i++) sets[int(random(sets.length))].randomPermute(random(1000, 2000));
time = millis();
}
}