/
helper.js
84 lines (70 loc) · 1.8 KB
/
helper.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
// DOM
function getElement(id) {
return document.getElementById(id);
}
// Math
function getDistance(x1, y1, x2, y2) {
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}
function getMagn(x, y) {
return Math.sqrt(x * x + y * y);
}
function toRadian(degrees) {
return degrees * Math.PI / 180;
}
function toDegree(radian) {
return radian * 180 / Math.PI;
}
// Random
function randInt(lower, upper) {
return Math.floor(lower + Math.random() * (upper - lower));
}
function randomElement(array) {
return array[Math.floor(Math.random() * array.length)]
}
function randomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
// Clean array generation
function new1dArray(length) {
let array = [];
for(let i = 0; i < length; i++) {
array.push(0);
}
return array;
}
function new2dArray(num_rows, num_cols) {
let array_2d= [];
for(let i = 0; i < num_rows; i++) {
let array_1d = [];
for(let j = 0; j < num_cols; j++) {
array_1d.push(0);
}
array_2d.push(array_1d);
}
return array_2d;
}
// Array manipulation
function removeElement(array, element) {
return array.filter(function (dummy) {
return dummy != element;
});
}
function shuffleKnuth(array) {
let currentIndex = array.length, randomIndex;
while (currentIndex != 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];
}
return array;
}
// rgb to hex
function rgbToHex(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}