-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
100 lines (87 loc) · 2.05 KB
/
sketch.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
const capturer = new CCapture({
framerate: 60,
format: 'webm',
name: 'flocking',
quality: 100,
verbose: true,
});
const flock = [];
let enableQtree, QtreeShow, preceptShow, boundaryType;
let isLoop, isRecording, save;
let p5Canvas;
function setup() {
p5Canvas = createCanvas(800, 800);
p5Canvas.parent('sketch-holder');
setupUI();
for (let i = 0; i < 500; i++) {
flock.push(new Boid());
}
optimize = true;
showQtree = false;
preceptShow = false;
isLoop = true;
isRecording = false;
save = false;
freshFrame = true;
boundaryType = 'Unbound';
}
function draw() {
if (isRecording && freshFrame) {
freshFrame = false;
capturer.start();
}
background(51, 50);
numOfBoids();
// Make quadtree
let boundary = new Rectangle(width / 2, height / 2, width, height);
for (i = 0; i < qtreeCapacity.length; i++) {
if (qtreeCapacity[i].checked) {
var capacity = qtreeCapacity[i].value;
}
}
let qtree = new QuadTree(boundary, capacity);
for (let boid of flock) {
let point = new Point(boid.position.x, boid.position.y, boid);
qtree.insert(point);
}
if (showQtree) {
qtree.show();
}
for (i = 0; i < bounding.length; i++) {
if (bounding[i].checked) {
var bounding_value = bounding[i].value;
}
}
// flock
for (let boid of flock) {
if (enableQtree.checked()) {
let range = new Circle(boid.position.x, boid.position.y, 50, 50);
let points = qtree.query(range);
let newFlock = [];
for (let point of points) {
newFlock.push(point.userData);
}
boid.edges(bounding_value);
boid.flock(newFlock);
boid.update();
boid.show(preceptShow);
} else {
boid.edges(bounding_value);
boid.flock(flock);
boid.update();
boid.show(preceptShow);
}
}
qtree.clear();
if (isRecording) {
record.html('Stop Recording')
capturer.capture(p5Canvas.canvas);
}
if (save && !isRecording) {
save = false;
freshFrame = true;
record.html('Record')
capturer.stop();
capturer.save();
}
}