-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
207 lines (190 loc) · 5.7 KB
/
index.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
export default p5 => {
const frameRate = 10;
let grid;
let colors;
let populationSlider, multSlider, groupSlider, preferenceSlider;
let populationSpan, multSpan, groupSpan, preferenceSpan;
const populationSliderOptions = {
min: 0.15,
max: 0.95,
default: 0.8,
step: 0.01
};
const multSliderOptions = {
min: 3,
max: 10,
default: 5,
step: 1
};
const groupSliderOptions = {
min: 2,
max: 10,
default: 2,
step: 1
};
const preferenceSliderOptions = {
min: 0,
max: 1,
default: 0.5,
step: 0.01
};
const initColors = numGroups => {
const hue = Math.random() * 360;
const sat = 50;
const bright = 75;
const offset = 360 / numGroups;
p5.colorMode(p5.HSB);
let colors = [];
for (let i = 0; i < numGroups; i++) {
colors.push(p5.color((hue + offset * i) % 360, sat, bright));
}
p5.colorMode(p5.RGB);
return colors;
};
let mult_, groups_, pop_, pref_, changed_;
let resized;
p5.setup = function setup() {
let canvas = p5.createCanvas();
canvas.parent("canvas");
canvas.addClass("w-100 h-100");
this.windowResized();
populationSlider = p5.createSlider(
populationSliderOptions.min,
populationSliderOptions.max,
populationSliderOptions.default,
populationSliderOptions.step
);
populationSlider.parent("pop-slider");
populationSlider.class("form-control-range");
multSlider = p5.createSlider(
multSliderOptions.min,
multSliderOptions.max,
multSliderOptions.default,
multSliderOptions.step
);
multSlider.parent("mult-slider");
multSlider.class("form-control-range");
groupSlider = p5.createSlider(
groupSliderOptions.min,
groupSliderOptions.max,
groupSliderOptions.default,
groupSliderOptions.step
);
groupSlider.parent("group-slider");
groupSlider.class("form-control-range");
preferenceSlider = p5.createSlider(
preferenceSliderOptions.min,
preferenceSliderOptions.max,
preferenceSliderOptions.default,
preferenceSliderOptions.step
);
preferenceSlider.parent("pref-slider");
preferenceSlider.class("form-control-range");
[
populationSlider,
multSlider,
groupSlider,
preferenceSlider
].forEach(slider => slider.addClass("custom-range"));
populationSpan = p5.createSpan();
populationSpan.parent("pop-number");
multSpan = p5.createSpan();
multSpan.parent("mult-number");
groupSpan = p5.createSpan();
groupSpan.parent("group-number");
preferenceSpan = p5.createSpan();
preferenceSpan.parent("pref-number");
p5.noStroke();
p5.frameRate(frameRate);
};
p5.draw = function draw() {
let groups = groupSlider.value();
let newGroups = groups_ != (groups_ = groups_ = groups);
if (newGroups) {
colors = initColors(groups_, p5);
}
let mult = multSlider.value();
let newMult = mult_ != (mult_ = mult);
let pop = populationSlider.value();
let newPop = pop_ != (pop_ = pop);
let newInit = newGroups || newMult || newPop || resized === true;
if (newInit) {
resized = false;
initGrid(mult, p5);
}
let pref = preferenceSlider.value();
let newPref = pref_ != (pref_ = pref);
changed_ |= newInit || newPref;
if (changed_) {
p5.clear();
changed_ = step(p5);
updateDisplayValue(populationSpan, populationSlider);
updateDisplayValue(multSpan, multSlider);
updateDisplayValue(groupSpan, groupSlider);
updateDisplayValue(preferenceSpan, preferenceSlider);
}
};
function updateDisplayValue(span, slider) {
span.html(`(${slider.value()})`);
}
function step() {
let modifiedGrid = grid.map(x => x.slice(0));
let changed = false;
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
let groupIndex = grid[i][j];
if (groupIndex != null) {
p5.fill(colors[groupIndex]);
p5.rect(i * mult_, j * mult_, mult_, mult_);
let similarNeighbors = 0;
let neighbors = 8;
for (let row = -1; row <= 1; row++) {
for (let col = -1; col <= 1; col++) {
if (!(row == 0 && col == 0)) {
let neighborGroupIndex =
grid[(i + row + grid.length) % grid.length][
(j + col + grid[i].length) % grid[i].length
];
if (neighborGroupIndex == null) {
neighbors--;
} else if (groupIndex == neighborGroupIndex) {
similarNeighbors++;
}
}
}
}
let neighborProportion =
neighbors != 0 ? similarNeighbors / neighbors : 1;
if (neighborProportion < pref_) {
changed = true;
while (true) {
let newRow = Math.floor(Math.random() * grid.length);
let newCol = Math.floor(Math.random() * grid[i].length);
if (modifiedGrid[newRow][newCol] == null) {
modifiedGrid[newRow][newCol] = groupIndex;
modifiedGrid[i][j] = null;
break;
}
}
}
}
}
}
grid = modifiedGrid;
return changed;
}
function initGrid(mult) {
// debugger;
grid = Array.from(Array(Math.floor(p5.width / mult)), () => {
return Array.from(Array(Math.floor(p5.height / mult)), () => {
if (Math.random() < pop_) return Math.floor(Math.random() * groups_);
else return null;
});
});
}
p5.windowResized = function windowResized() {
resized = true;
let canvas = p5.select("#canvas");
p5.resizeCanvas(canvas.width, canvas.height);
};
};