ts/util/MathUtil.ts - StructureJS

StructureJS

0.15.2

A class based utility library for building modular and scalable web platform applications. Features opt-in classes and utilities which provide a solid foundation and toolset to build your next project.

File: ts/util/MathUtil.ts

  1. /**
  2. * A helper class to do calculations and conversions.
  3. *
  4. * @class MathUtil
  5. * @module StructureJS
  6. * @submodule util
  7. * @author Robert S. (www.codeBelt.com)
  8. * @static
  9. */
  10. class MathUtil
  11. {
  12. constructor()
  13. {
  14. throw new Error('[MathUtil] Do not instantiate the MathUtil class because it is a static class.');
  15. }
  16.  
  17. /**
  18. * Returns a number constrained between min and max.
  19. *
  20. * @method constrain
  21. * @param num {number}
  22. * @param min {number}
  23. * @param max {number}
  24. * @return {number}
  25. * @example
  26. * MathUtil.constrain(12, 3, 20);
  27. * // 12
  28. *
  29. * MathUtil.constrain(22, 3, 20);
  30. * // 20
  31. *
  32. * MathUtil.constrain(0, 3, 20);
  33. * // 3
  34. */
  35. public static constrain(num:number, min:number = 0, max:number = 1):number
  36. {
  37. if (num < min)
  38. {
  39. return min;
  40. }
  41. if (num > max)
  42. {
  43. return max;
  44. }
  45. return num;
  46. }
  47.  
  48.  
  49. /**
  50. * Returns a random number between min and max.
  51. *
  52. * @method randomRange
  53. * @param min {number}
  54. * @param max {number}
  55. * @param [wholeNumber=true] {number}
  56. * @return {number}
  57. * @example
  58. *
  59. */
  60. public static randomRange(min:number, max:number, wholeNumber:boolean = true):number
  61. {
  62. const num:number = (min + Math.random() * (max - min));
  63.  
  64. if (wholeNumber)
  65. {
  66. return Math.round(num);
  67. }
  68. return num;
  69. }
  70.  
  71.  
  72. /**
  73. * Returns the percentage of a number in a given range.
  74. * Example: num = 15 range 10 to 20 // outputs 0.5
  75. *
  76. * @method rangeToPercent
  77. * @param num {number}
  78. * @param min {number}
  79. * @param max {number}
  80. * @param constrainMin {boolean} Returns 0 if num < min.
  81. * @param constrainMax {boolean} Returns 1 if num > max.
  82. * @return {number}
  83. * @example
  84. * MathUtil.rangeToPercent(15, 10, 20);
  85. * // 0.5
  86. */
  87. public static rangeToPercent(num:number, min:number, max:number, constrainMin:boolean = false, constrainMax:boolean = false):number
  88. {
  89. if (constrainMin && num < min)
  90. {
  91. return 0;
  92. }
  93. if (constrainMax && num > max)
  94. {
  95. return 1;
  96. }
  97. return (num - min) / (max - min);
  98. }
  99.  
  100.  
  101. /**
  102. * Returns the number that corresponds to the percentage in a given range.
  103. * Example: percent = 0.5 range 10 to 20 // outputs 15
  104. *
  105. * @method percentToRange
  106. * @param percent {number}
  107. * @param min {number}
  108. * @param max {number}
  109. * @return {number}
  110. * @example
  111. * MathUtil.percentToRange(0.5, 10, 20);
  112. * // 15
  113. */
  114. public static percentToRange(percent:number, min:number, max:number):number
  115. {
  116. return (percent * (max - min)) + min;
  117. }
  118.  
  119.  
  120. /**
  121. * Re-maps a number from one range to another. The output is the same as inputing the result of rangeToPercent() numbero percentToRange().
  122. * Example: num = 10, min1 = 0, max1 = 100, min2 = 0, max2 = 50 // outputs 5
  123. *
  124. * @method map
  125. * @param num {number}
  126. * @param min1 {number}
  127. * @param max1 {number}
  128. * @param min2 {number}
  129. * @param max2 {number}
  130. * @return {number}
  131. * @example
  132. * MathUtil.map(10, 0, 100, 0, 50);
  133. * // 5
  134. */
  135. public static map(num:number, min1:number, max1:number, min2:number, max2:number, round:boolean = true, constrainMin:boolean = true, constrainMax:boolean = true):number
  136. {
  137. if (constrainMin && num < min1)
  138. {
  139. return min2;
  140. }
  141. if (constrainMax && num > max1)
  142. {
  143. return max2;
  144. }
  145.  
  146. const num1:number = (num - min1) / (max1 - min1);
  147. const num2:number = (num1 * (max2 - min2)) + min2;
  148. if (round)
  149. {
  150. return Math.round(num2);
  151. }
  152. return num2;
  153. }
  154.  
  155.  
  156. /**
  157. * Converts radians to degrees.
  158. *
  159. * @method radiansToDegrees
  160. * @param radians {number}
  161. * @return {number}
  162. * @example
  163. * MathUtil.radiansToDegrees(1.5707963267948966);
  164. * // 90
  165. *
  166. * MathUtil.radiansToDegrees(3.141592653589793);
  167. * // 180
  168. */
  169. public static radiansToDegrees(radians:number):number
  170. {
  171. return radians * (180 / Math.PI);
  172. }
  173.  
  174.  
  175. /**
  176. * Converts degrees to radians.
  177. *
  178. * @method degreesToRadians
  179. * @param degrees {number}
  180. * @return {number}
  181. * @example
  182. * MathUtil.degreesToRadians(90);
  183. * // 1.5707963267948966
  184. *
  185. * MathUtil.degreesToRadians(180);
  186. * // 3.141592653589793
  187. */
  188. public static degreesToRadians(degrees:number):number
  189. {
  190. return (degrees * Math.PI / 180);
  191. }
  192.  
  193.  
  194. /**
  195. * Returns 1 if the value is >= 0. Returns -1 if the value is < 0.
  196. *
  197. * @method sign
  198. * @param num {number}
  199. * @return {number}
  200. * @example
  201. * MathUtil.sign(23);
  202. * // 1
  203. *
  204. * MathUtil.sign(-23);
  205. * // -1
  206. */
  207. public static sign(num:number):number
  208. {
  209. if (num < 0)
  210. {
  211. return -1
  212. }
  213. return 1;
  214. }
  215.  
  216. /**
  217. * Check if number is positive (zero is positive).
  218. *
  219. * @method isPositive
  220. * @param num {number} The number.
  221. * @return {boolean}
  222. * @example
  223. * MathUtil.isPositive(23);
  224. * // true
  225. *
  226. * MathUtil.isPositive(-23);
  227. * // false
  228. */
  229. public static isPositive(num:number):boolean
  230. {
  231. return (num >= 0);
  232. }
  233.  
  234. /**
  235. * Check if number is negative.
  236. *
  237. * @method isNegative
  238. * @param num {number} The
  239. * @return {boolean}
  240. * @example
  241. * MathUtil.isNegative(23);
  242. * // false
  243. *
  244. * MathUtil.isNegative(-23);
  245. * // true
  246. */
  247. public static isNegative(num:number):boolean
  248. {
  249. return (num < 0);
  250. }
  251.  
  252. /**
  253. * Check if number is odd (convert to Integer if necessary).
  254. *
  255. * @method isOdd
  256. * @param num {number} The number.
  257. * @return {boolean}
  258. * @example
  259. * MathUtil.isOdd(2);
  260. * // false
  261. *
  262. * MathUtil.isOdd(3);
  263. * // true
  264. */
  265. public static isOdd(num:number):boolean
  266. {
  267. const i:number = num;
  268. const e:number = 2;
  269. return Boolean(i % e);
  270. }
  271.  
  272. /**
  273. * Check if number is even (convert to Integer if necessary).
  274. *
  275. * @method isEven
  276. * @param num {number} The number.
  277. * @return {boolean}
  278. * @example
  279. * MathUtil.isEven(2);
  280. * // true
  281. *
  282. * MathUtil.isEven(3);
  283. * // false
  284. */
  285. public static isEven(num:number):boolean
  286. {
  287. const int:number = num;
  288. const e:number = 2;
  289. return (int % e == 0);
  290. }
  291.  
  292. /**
  293. * Check if number is Prime (divisible only by itself and one).
  294. *
  295. * @method isPrime
  296. * @param num {number} The number.
  297. * @return {boolean}
  298. * @example
  299. * MathUtil.isPrime(4);
  300. * // false
  301. *
  302. * MathUtil.isPrime(5);
  303. * // true
  304. */
  305. public static isPrime(num:number):boolean
  306. {
  307. if (num > 2 && num % 2 == 0)
  308. {
  309. return false;
  310. }
  311. const l:number = Math.sqrt(num);
  312. let i:number = 3;
  313. for (i; i <= l; i += 2)
  314. {
  315. if (num % i == 0)
  316. {
  317. return false;
  318. }
  319. }
  320. return true;
  321. }
  322.  
  323. /**
  324. * Calculate the factorial of the integer.
  325. *
  326. * @method factorial
  327. * @param num {number} The number.
  328. * @return {number}
  329. * @example
  330. * MathUtil.factorial(5);
  331. * // 120
  332. *
  333. * MathUtil.factorial(9);
  334. * // 362880
  335. */
  336. public static factorial(num:number):number
  337. {
  338. if (num == 0)
  339. {
  340. return 1;
  341. }
  342. let d:number = <number>num.valueOf();
  343. let i:number = d - 1;
  344. while (i)
  345. {
  346. d = d * i;
  347. i--;
  348. }
  349. return d;
  350. }
  351.  
  352. /**
  353. * Return an array of divisors of the integer.
  354. *
  355. * @method getDivisors
  356. * @param num {number} The number.
  357. * @return {Array.<number>}
  358. * @example
  359. *
  360. */
  361. public static getDivisors(num:number):Array<number>
  362. {
  363. const r:Array<number> = [];
  364.  
  365. for (let i:number = 1, e:number = num / 2; i <= e; i++)
  366. {
  367. if (num % i == 0)
  368. {
  369. r.push(i);
  370. }
  371. }
  372.  
  373. if (num != 0)
  374. {
  375. r.push(<number>num.valueOf());
  376. }
  377.  
  378. return r;
  379. }
  380.  
  381. }
  382.  
  383. export default MathUtil;