forked from sugarlabs/musicblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathutils.js
329 lines (301 loc) · 9.15 KB
/
mathutils.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
* @file This contains the encapsulation related to math operations.
* @author Walter Bender
*
* @copyright 2014-2020 Walter Bender
* @copyright 2020 Anindya Kundu
*
* @license
* This program is free software; you can redistribute it and/or modify it under the terms of
* the The GNU Affero General Public License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* You should have received a copy of the GNU Affero General Public License along with this
* library; if not, write to the Free Software Foundation, 51 Franklin Street, Suite 500 Boston,
* MA 02110-1335 USA.
*/
/*
global SOLFEGENAMES
*/
/*
Global location
js/utils/musicutils.js
SOLFEGENAMES
*/
/* exported MathUtility */
/**
* Utility class for managing math operations.
*
* @class
* @classdesc This is the prototype that encapsulates the ten math operations: random, one-of,
* modulus, square root, add, subtract, multiply, divide, eucleidean distance, and exponent.
* The methods verify the input/s and return the operations' results or throws an exception if
* the arguments are invalid, which is then handled by their callers, i.e. `arg` methods in
* NumberBlocks.js
*/
class MathUtility {
/**
* Returns a random integer in a range.
*
* @static
* @param {number|string} a - Preferably the minimum. If a string, it should be a valid solfege name.
* @param {number|string} b - Preferably the maximum. If a string, it should be a valid solfege name.
* @param {number} [c] - Octave (for cases when a and b are solfeges).
* @returns {number|array} - A random number between a and b (both inclusive) or a random solfege array.
* @throws {string} NAN error if the arguments are not valid.
*/
static doRandom(a, b, c) {
/**
* Returns a random number between n1 and n2 (both inclusive).
*
* @param {number} n1
* @param {number} n2
* @returns {number}
*/
const GetRandom = (n1, n2) => {
// n1 should be <= n2
[n1, n2] = n1 > n2 ? [n2, n1] : [n1, n2];
return Math.floor(Math.random() * (Number(n2) - Number(n1) + 1) + Number(n1));
};
/**
* Returns a random solfege array between a1 and a2 with the given octave.
*
* @param {string} a1
* @param {string} a2
* @param {number} [octave=4]
* @returns {array} - A random solfege array.
*/
const GetRandomSolfege = (a1, a2, octave) => {
octave = octave === undefined ? 4 : octave;
const broadScale = [];
for (const i of[octave, octave + 1]) {
for (let j = 0; j < SOLFEGENAMES.length; j++) {
broadScale.push(SOLFEGENAMES[j] + " " + i);
}
}
const n1 = SOLFEGENAMES.indexOf(a1);
const n2 = SOLFEGENAMES.indexOf(a2);
const o1 = octave;
const o2 = n1 > n2 ? octave + 1 : octave;
let n11, n22;
n11 = broadScale.indexOf(a1 + " " + o1);
n22 = broadScale.indexOf(a2 + " " + o2);
return broadScale[GetRandom(n11, n22)].split(" ");
};
if (typeof a === "number" && typeof b === "number") {
return GetRandom(a, b);
} else if (
typeof a === "string" &&
typeof b === "string" &&
SOLFEGENAMES.includes(a) &&
SOLFEGENAMES.includes(b)
) {
return GetRandomSolfege(a, b, c);
} else {
throw "NanError";
}
}
/**
* Randomly returns either a or b.
*
* @static
* @param {*} a
* @param {*} b
* @returns {*} - Either a or b.
*/
static pickRandom(a, b) {
return Math.random() < 0.5 ? a : b;
}
/**
* Returns a modulo b.
*
* @static
* @param {number} a
* @param {number} b
* @returns {number} - Result of a modulo b.
* @throws {string} NAN error if the arguments are not valid.
*/
static doMod(a, b) {
if (typeof a === "number" && typeof b === "number") {
return Number(a) % Number(b);
} else {
throw "NanError";
}
}
/**
* Square-roots a number.
*
* @static
* @param {number} a
* @returns {number} - Square root of a.
* @throws {string} No square root error, NAN error if the arguments are not valid.
*/
static doSqrt(a) {
if (typeof a === "number") {
if (a < 0) {
throw "NoSqrtError";
}
return Math.sqrt(Number(a));
} else {
throw "NanError";
}
}
/**
* Adds a and b.
*
* @static
* @param {*} a
* @param {*} b
* @returns {number|string} - Sum of a and b. If either a or b is a string, it concatenates them.
*/
static doPlus(a, b) {
if (typeof a === "string" || typeof b === "string") {
const aString = typeof a === "string" ? a : a.toString();
const bString = typeof b === "string" ? b : b.toString();
return aString + bString;
} else {
return Number(a) + Number(b);
}
}
/**
* Subtracts b from a.
*
* @static
* @param {number} a
* @param {number} b
* @returns {number} - Result of a minus b.
* @throws {string} NAN error if the arguments are not valid.
*/
static doMinus(a, b) {
if (typeof a === "string" || typeof b === "string") {
throw "NanError";
}
return Number(a) - Number(b);
}
/**
* Multiplies a by b.
*
* @static
* @param {*} a
* @param {*} b
* @returns {number} - Result of a multiplied by b.
* @throws {string} NAN error if the arguments are not valid.
*/
static doMultiply(a, b) {
if (typeof a === "string" || typeof b === "string") {
throw "NanError";
}
return Number(a) * Number(b);
}
/**
* Divides a by b.
*
* @static
* @param {number} a
* @param {number} b
* @returns {number} - Result of a divided by b.
* @throws {string} Divide by Zero error, NAN error if the arguments are not valid.
*/
static doDivide(a, b) {
if (typeof a === "number" && typeof b === "number") {
if (Number(b) === 0) {
throw "DivByZeroError";
}
return Number(a) / Number(b);
} else {
throw "NanError";
}
}
/**
* Calculates Euclidean distance between (cursor x, cursor y) and (mouse 'x' and mouse 'y').
*
* @static
* @param {number} x1
* @param {number} y1
* @param {number} x2
* @param {number} y2
* @returns {number} - Euclidean distance between two points.
* @throws {string} NAN error if the arguments are not valid.
*/
static doCalculateDistance(x1, y1, x2, y2) {
if (
typeof x1 === "number" &&
typeof y1 === "number" &&
typeof x2 === "number" &&
typeof y2 === "number"
) {
if (x1 === x2 && y1 === y2) {
return 0;
}
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
} else {
throw "NanError";
}
}
/**
* Returns a to the power of b.
*
* @static
* @param {number} a
* @param {number} b
* @returns {number} - Result of a raised to the power of b.
* @throws {string} NAN error if the arguments are not valid.
*/
static doPower(a, b) {
if (typeof a === "number" && typeof b === "number") {
return Math.pow(a, b);
} else {
throw "NanError";
}
}
/**
* Returns absolute value.
*
* @static
* @param {number} a
* @returns {number} - Absolute value of a.
* @throws {string} NAN error if the argument is not valid.
*/
static doAbs(a) {
if (typeof a === "number") {
return Math.abs(a);
} else {
throw "NanError";
}
}
/**
* Returns negative value (if number) or string in reverse (if string).
*
* @static
* @param {*} a
* @returns {number|string} - Negative value of a (if number) or string in reverse (if string).
* @throws {string} No Negation error if the argument is not valid.
*/
static doNegate(a) {
if (typeof a === "number") {
return MathUtility.doMinus(0, a);
} else if (typeof a === "string") {
const obj = a.split("");
return obj.reverse().join("");
} else {
throw "NoNegError";
}
}
/**
* Returns integer value.
*
* @static
* @param {*} a
* @returns {number} - Integer value of a.
* @throws {string} NAN error if the argument is not valid.
*/
static doInt(a) {
try {
return Math.floor(Number(a) + 0.5);
} catch (e) {
throw "NanError";
}
}
}
// Ensure mathutils.js exports the MathUtility class
module.exports = MathUtility;