- 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
/**
* a collection of math utility functions
* @namespace Math
*/
/**
* constant to convert from degrees to radians
* @public
* @type {number}
* @name DEG_TO_RAD
* @memberof Math
*/
export const DEG_TO_RAD = Math.PI / 180.0;
/**
* constant to convert from radians to degrees
* @public
* @type {number}
* @name RAD_TO_DEG
* @memberof Math
*/
export const RAD_TO_DEG = 180.0 / Math.PI;
/**
* constant equals to 2 times pi
* @public
* @type {number}
* @name TAU
* @memberof Math
*/
export const TAU = Math.PI * 2;
/**
* constant equals to half pi
* @public
* @type {number}
* @name ETA
* @memberof Math
*/
export const ETA = Math.PI * 0.5;
/**
* the difference between 1 and the smallest floating point number greater than 1
* @public
* @type {number}
* @name EPSILON
* @memberof Math
*/
export const EPSILON = 0.000001;
/**
* returns true if the given value is a power of two
* @public
* @memberof Math
* @name isPowerOfTwo
* @param {number} val
* @returns {boolean}
*/
export function isPowerOfTwo(val) {
return (val & (val - 1)) === 0;
}
/**
* returns the next power of two for the given value
* @public
* @memberof Math
* @name nextPowerOfTwo
* @param {number} val
* @returns {boolean}
*/
export function nextPowerOfTwo(val) {
val --;
val |= val >> 1;
val |= val >> 2;
val |= val >> 4;
val |= val >> 8;
val |= val >> 16;
val ++;
return val;
}
/**
* Converts an angle in degrees to an angle in radians
* @public
* @memberof Math
* @name degToRad
* @param {number} angle - angle in degrees
* @returns {number} corresponding angle in radians
* @example
* // convert a specific angle
* me.Math.degToRad(60); // return 1.0471...
*/
export function degToRad(angle) {
return angle * DEG_TO_RAD;
}
/**
* Converts an angle in radians to an angle in degrees.
* @public
* @memberof Math
* @name radToDeg
* @param {number} radians - angle in radians
* @returns {number} corresponding angle in degrees
* @example
* // convert a specific angle
* me.Math.radToDeg(1.0471975511965976); // return 60
*/
export function radToDeg(radians) {
return radians * RAD_TO_DEG;
}
/**
* clamp the given value
* @public
* @memberof Math
* @name clamp
* @param {number} val - the value to clamp
* @param {number} low - lower limit
* @param {number} high - higher limit
* @returns {number} clamped value
*/
export function clamp(val, low, high) {
return val < low ? low : val > high ? high : +val;
}
/**
* return a random integer between min (included) and max (excluded)
* @public
* @memberof Math
* @name random
* @param {number} min - minimum value.
* @param {number} max - maximum value.
* @returns {number} random value
* @example
* // Print a random number; one of 5, 6, 7, 8, 9
* console.log(me.Math.random(5, 10) );
*/
export function random(min, max) {
return (~~(Math.random() * (max - min)) + min);
}
/**
* return a random float between min, max (exclusive)
* @public
* @memberof Math
* @name randomFloat
* @param {number} min - minimum value.
* @param {number} max - maximum value.
* @returns {number} random value
* @example
* // Print a random number; one of 5, 6, 7, 8, 9
* console.log(me.Math.randomFloat(5, 10) );
*/
export function randomFloat(min, max) {
return (Math.random() * (max - min)) + min;
}
/**
* return a weighted random between min, max (exclusive)
* @public
* @memberof Math
* @name weightedRandom
* @param {number} min - minimum value.
* @param {number} max - maximum value.
* @returns {number} random value
* @example
* // Print a random number; one of 5, 6, 7, 8, 9
* console.log(me.Math.weightedRandom(5, 10) );
*/
export function weightedRandom(min, max) {
return (~~(Math.pow(Math.random(), 2) * (max - min)) + min);
}
/**
* round a value to the specified number of digit
* @public
* @memberof Math
* @name round
* @param {number} num - value to be rounded.
* @param {number} [dec=0] - number of decimal digit to be rounded to.
* @returns {number} rounded value
* @example
* // round a specific value to 2 digits
* me.Math.round(10.33333, 2); // return 10.33
*/
export function round(num, dec = 0) {
// if only one argument use the object value
const powres = Math.pow(10, dec);
return (~~(0.5 + num * powres) / powres);
}
/**
* check if the given value is close to the expected one
* @public
* @memberof Math
* @name toBeCloseTo
* @param {number} expected - value to be compared with.
* @param {number} actual - actual value to compare
* @param {number} [precision=2] - float precision for the comparison
* @returns {boolean} if close to
* @example
* // test if the given value is close to 10
* if (me.Math.toBeCloseTo(10, value)) {
* // do something
* }
*/
export function toBeCloseTo(expected, actual, precision = 2) {
return Math.abs(expected - actual) < (Math.pow(10, -precision) / 2);
}