-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.js
34 lines (34 loc) · 1.06 KB
/
functions.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
function randomNumber(a, b, index = false) {
//random number from a to b
if (index) {
return Math.floor(Math.random() * (b - a) + a);
} else {
return (Math.random() * (b - a) + a) - (0.5 * (b - a));
}
}
function getDistance(dot1, dot2) {
let dx = Math.abs(dot1.x - dot2.x);
let dy = Math.abs(dot1.y - dot2.y);
if (settings.wrap) {
if (dx > (canvas.width * 0.5)) {
dx = canvas.width - dx;
}
if (dy > (canvas.height * 0.5)) {
dy = canvas.height - dy;
}
}
return Math.sqrt(dx * dx + dy * dy);
}
function accelerator(factor, distance) {
let force;
if (distance < settings.minDistance) {
force = (settings.repel / settings.minDistance) * distance - settings.repel;
} else if (distance > settings.maxDistance) {
force = 0;
} else {
let mid = (settings.minDistance + settings.maxDistance) / 2;
let slope = factor / (mid - settings.minDistance);
force = -(slope * Math.abs(distance - mid)) + factor;
}
return force;
}