-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandompoints.js
44 lines (40 loc) · 1.1 KB
/
randompoints.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
var squaredDistanceMin = 0.02;
function HSVtoRGB(h, s, v) {
var r, g, b, i, f, p, q, t;
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return [r, g, b];
}
var points = [];
var tries = 0;
while (tries < 100) {
var x = Math.random();
var y = Math.random();
var z = Math.random();
if (!points.some(function(point) {
var dx = (x - point.center[0] + 1.5) % 1 - 0.5;
var dy = (y - point.center[1] + 1.5) % 1 - 0.5;
var dz = (y - point.center[2] + 1.5) % 1 - 0.5;
return (dx * dx + dy * dy + dz * dz < squaredDistanceMin);
})) {
points.push({
center: [x, y, z],
color: HSVtoRGB(Math.random(), 0.5, 1)
});
tries = 0;
} else
++tries;
}
console.log("var trailPoints =", points, ";");
console.error("%d points", points.length);