PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/docs/Math.js.html

https://gitlab.com/lobsterhands/phaser
HTML | 1926 lines | 1503 code | 420 blank | 3 comment | 0 complexity | 6d6e8fc26ee2d391a3f1c9966e2a0a77 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Phaser Source: math/Math.js</title>
  6. <!--[if lt IE 9]>
  7. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  8. <![endif]-->
  9. <link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
  10. <link type="text/css" rel="stylesheet" href="styles/site.cerulean.css">
  11. </head>
  12. <body>
  13. <div class="container-fluid">
  14. <div class="navbar navbar-fixed-top navbar-inverse">
  15. <div class="navbar-inner">
  16. <a class="brand" href="index.html">Phaser</a>
  17. <ul class="nav">
  18. <li class="dropdown">
  19. <a href="namespaces.list.html" class="dropdown-toggle" data-toggle="dropdown">Namespaces<b
  20. class="caret"></b></a>
  21. <ul class="dropdown-menu ">
  22. <li class="class-depth-0">
  23. <a href="Phaser.html">Phaser</a>
  24. </li>
  25. </ul>
  26. </li>
  27. <li class="dropdown">
  28. <a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b
  29. class="caret"></b></a>
  30. <ul class="dropdown-menu ">
  31. <li class="class-depth-1">
  32. <a href="Phaser.Animation.html">Animation</a>
  33. </li>
  34. <li class="class-depth-1">
  35. <a href="Phaser.AnimationManager.html">AnimationManager</a>
  36. </li>
  37. <li class="class-depth-1">
  38. <a href="Phaser.AnimationParser.html">AnimationParser</a>
  39. </li>
  40. <li class="class-depth-1">
  41. <a href="Phaser.BitmapData.html">BitmapData</a>
  42. </li>
  43. <li class="class-depth-1">
  44. <a href="Phaser.BitmapText.html">BitmapText</a>
  45. </li>
  46. <li class="class-depth-1">
  47. <a href="Phaser.Button.html">Button</a>
  48. </li>
  49. <li class="class-depth-1">
  50. <a href="Phaser.Cache.html">Cache</a>
  51. </li>
  52. <li class="class-depth-1">
  53. <a href="Phaser.Camera.html">Camera</a>
  54. </li>
  55. <li class="class-depth-1">
  56. <a href="Phaser.Canvas.html">Canvas</a>
  57. </li>
  58. <li class="class-depth-1">
  59. <a href="Phaser.Circle.html">Circle</a>
  60. </li>
  61. <li class="class-depth-1">
  62. <a href="Phaser.Color.html">Color</a>
  63. </li>
  64. <li class="class-depth-1">
  65. <a href="Phaser.Device.html">Device</a>
  66. </li>
  67. <li class="class-depth-1">
  68. <a href="Phaser.Easing.html">Easing</a>
  69. </li>
  70. <li class="class-depth-2">
  71. <a href="Phaser.Easing.Back.html">Back</a>
  72. </li>
  73. <li class="class-depth-2">
  74. <a href="Phaser.Easing.Bounce.html">Bounce</a>
  75. </li>
  76. <li class="class-depth-2">
  77. <a href="Phaser.Easing.Circular.html">Circular</a>
  78. </li>
  79. <li class="class-depth-2">
  80. <a href="Phaser.Easing.Cubic.html">Cubic</a>
  81. </li>
  82. <li class="class-depth-2">
  83. <a href="Phaser.Easing.Elastic.html">Elastic</a>
  84. </li>
  85. <li class="class-depth-2">
  86. <a href="Phaser.Easing.Exponential.html">Exponential</a>
  87. </li>
  88. <li class="class-depth-2">
  89. <a href="Phaser.Easing.Linear.html">Linear</a>
  90. </li>
  91. <li class="class-depth-2">
  92. <a href="Phaser.Easing.Quadratic.html">Quadratic</a>
  93. </li>
  94. <li class="class-depth-2">
  95. <a href="Phaser.Easing.Quartic.html">Quartic</a>
  96. </li>
  97. <li class="class-depth-2">
  98. <a href="Phaser.Easing.Quintic.html">Quintic</a>
  99. </li>
  100. <li class="class-depth-2">
  101. <a href="Phaser.Easing.Sinusoidal.html">Sinusoidal</a>
  102. </li>
  103. <li class="class-depth-1">
  104. <a href="Phaser.Ellipse.html">Ellipse</a>
  105. </li>
  106. <li class="class-depth-1">
  107. <a href="Phaser.Events.html">Events</a>
  108. </li>
  109. <li class="class-depth-1">
  110. <a href="Phaser.Filter.html">Filter</a>
  111. </li>
  112. <li class="class-depth-1">
  113. <a href="Phaser.Frame.html">Frame</a>
  114. </li>
  115. <li class="class-depth-1">
  116. <a href="Phaser.FrameData.html">FrameData</a>
  117. </li>
  118. <li class="class-depth-1">
  119. <a href="Phaser.Game.html">Game</a>
  120. </li>
  121. <li class="class-depth-1">
  122. <a href="Phaser.GameObjectCreator.html">GameObjectCreator</a>
  123. </li>
  124. <li class="class-depth-1">
  125. <a href="Phaser.GameObjectFactory.html">GameObjectFactory</a>
  126. </li>
  127. <li class="class-depth-1">
  128. <a href="Phaser.Gamepad.html">Gamepad</a>
  129. </li>
  130. <li class="class-depth-1">
  131. <a href="Phaser.GamepadButton.html">GamepadButton</a>
  132. </li>
  133. <li class="class-depth-1">
  134. <a href="Phaser.Graphics.html">Graphics</a>
  135. </li>
  136. <li class="class-depth-1">
  137. <a href="Phaser.Group.html">Group</a>
  138. </li>
  139. <li class="class-depth-1">
  140. <a href="Phaser.Image.html">Image</a>
  141. </li>
  142. <li class="class-depth-1">
  143. <a href="Phaser.Input.html">Input</a>
  144. </li>
  145. <li class="class-depth-1">
  146. <a href="Phaser.InputHandler.html">InputHandler</a>
  147. </li>
  148. <li class="class-depth-1">
  149. <a href="Phaser.Key.html">Key</a>
  150. </li>
  151. <li class="class-depth-1">
  152. <a href="Phaser.Keyboard.html">Keyboard</a>
  153. </li>
  154. <li class="class-depth-1">
  155. <a href="Phaser.Line.html">Line</a>
  156. </li>
  157. <li class="class-depth-1">
  158. <a href="Phaser.LinkedList.html">LinkedList</a>
  159. </li>
  160. <li class="class-depth-1">
  161. <a href="Phaser.Loader.html">Loader</a>
  162. </li>
  163. <li class="class-depth-1">
  164. <a href="Phaser.LoaderParser.html">LoaderParser</a>
  165. </li>
  166. <li class="class-depth-1">
  167. <a href="Phaser.Math.html">Math</a>
  168. </li>
  169. <li class="class-depth-1">
  170. <a href="Phaser.Mouse.html">Mouse</a>
  171. </li>
  172. <li class="class-depth-1">
  173. <a href="Phaser.MSPointer.html">MSPointer</a>
  174. </li>
  175. <li class="class-depth-1">
  176. <a href="Phaser.Net.html">Net</a>
  177. </li>
  178. <li class="class-depth-1">
  179. <a href="Phaser.Particle.html">Particle</a>
  180. </li>
  181. <li class="class-depth-1">
  182. <a href="Phaser.Particles.html">Particles</a>
  183. </li>
  184. <li class="class-depth-3">
  185. <a href="Phaser.Particles.Arcade.Emitter.html">Emitter</a>
  186. </li>
  187. <li class="class-depth-1">
  188. <a href="Phaser.Physics.html">Physics</a>
  189. </li>
  190. <li class="class-depth-2">
  191. <a href="Phaser.Physics.Arcade.html">Arcade</a>
  192. </li>
  193. <li class="class-depth-3">
  194. <a href="Phaser.Physics.Arcade.Body.html">Body</a>
  195. </li>
  196. <li class="class-depth-2">
  197. <a href="Phaser.Physics.Ninja.html">Ninja</a>
  198. </li>
  199. <li class="class-depth-3">
  200. <a href="Phaser.Physics.Ninja.AABB.html">AABB</a>
  201. </li>
  202. <li class="class-depth-3">
  203. <a href="Phaser.Physics.Ninja.Body.html">Body</a>
  204. </li>
  205. <li class="class-depth-3">
  206. <a href="Phaser.Physics.Ninja.Circle.html">Circle</a>
  207. </li>
  208. <li class="class-depth-3">
  209. <a href="Phaser.Physics.Ninja.Tile.html">Tile</a>
  210. </li>
  211. <li class="class-depth-2">
  212. <a href="Phaser.Physics.P2.html">P2</a>
  213. </li>
  214. <li class="class-depth-3">
  215. <a href="Phaser.Physics.P2.Body.html">Body</a>
  216. </li>
  217. <li class="class-depth-3">
  218. <a href="Phaser.Physics.P2.BodyDebug.html">BodyDebug</a>
  219. </li>
  220. <li class="class-depth-3">
  221. <a href="Phaser.Physics.P2.CollisionGroup.html">CollisionGroup</a>
  222. </li>
  223. <li class="class-depth-3">
  224. <a href="Phaser.Physics.P2.ContactMaterial.html">ContactMaterial</a>
  225. </li>
  226. <li class="class-depth-3">
  227. <a href="Phaser.Physics.P2.DistanceConstraint.html">DistanceConstraint</a>
  228. </li>
  229. <li class="class-depth-3">
  230. <a href="Phaser.Physics.P2.FixtureList.html">FixtureList</a>
  231. </li>
  232. <li class="class-depth-3">
  233. <a href="Phaser.Physics.P2.GearConstraint.html">GearConstraint</a>
  234. </li>
  235. <li class="class-depth-3">
  236. <a href="Phaser.Physics.P2.InversePointProxy.html">InversePointProxy</a>
  237. </li>
  238. <li class="class-depth-3">
  239. <a href="Phaser.Physics.P2.LockConstraint.html">LockConstraint</a>
  240. </li>
  241. <li class="class-depth-3">
  242. <a href="Phaser.Physics.P2.Material.html">Material</a>
  243. </li>
  244. <li class="class-depth-3">
  245. <a href="Phaser.Physics.P2.PointProxy.html">PointProxy</a>
  246. </li>
  247. <li class="class-depth-3">
  248. <a href="Phaser.Physics.P2.PrismaticConstraint.html">PrismaticConstraint</a>
  249. </li>
  250. <li class="class-depth-3">
  251. <a href="Phaser.Physics.P2.RevoluteConstraint.html">RevoluteConstraint</a>
  252. </li>
  253. <li class="class-depth-3">
  254. <a href="Phaser.Physics.P2.Spring.html">Spring</a>
  255. </li>
  256. <li class="class-depth-1">
  257. <a href="Phaser.Plugin.html">Plugin</a>
  258. </li>
  259. <li class="class-depth-1">
  260. <a href="Phaser.PluginManager.html">PluginManager</a>
  261. </li>
  262. <li class="class-depth-1">
  263. <a href="Phaser.Point.html">Point</a>
  264. </li>
  265. <li class="class-depth-1">
  266. <a href="Phaser.Pointer.html">Pointer</a>
  267. </li>
  268. <li class="class-depth-1">
  269. <a href="Phaser.Polygon.html">Polygon</a>
  270. </li>
  271. <li class="class-depth-1">
  272. <a href="Phaser.QuadTree.html">QuadTree</a>
  273. </li>
  274. <li class="class-depth-1">
  275. <a href="Phaser.RandomDataGenerator.html">RandomDataGenerator</a>
  276. </li>
  277. <li class="class-depth-1">
  278. <a href="Phaser.Rectangle.html">Rectangle</a>
  279. </li>
  280. <li class="class-depth-1">
  281. <a href="Phaser.RenderTexture.html">RenderTexture</a>
  282. </li>
  283. <li class="class-depth-1">
  284. <a href="Phaser.RequestAnimationFrame.html">RequestAnimationFrame</a>
  285. </li>
  286. <li class="class-depth-1">
  287. <a href="Phaser.RetroFont.html">RetroFont</a>
  288. </li>
  289. <li class="class-depth-1">
  290. <a href="Phaser.ScaleManager.html">ScaleManager</a>
  291. </li>
  292. <li class="class-depth-1">
  293. <a href="Phaser.Signal.html">Signal</a>
  294. </li>
  295. <li class="class-depth-1">
  296. <a href="Phaser.SignalBinding.html">SignalBinding</a>
  297. </li>
  298. <li class="class-depth-1">
  299. <a href="Phaser.SinglePad.html">SinglePad</a>
  300. </li>
  301. <li class="class-depth-1">
  302. <a href="Phaser.Sound.html">Sound</a>
  303. </li>
  304. <li class="class-depth-1">
  305. <a href="Phaser.SoundManager.html">SoundManager</a>
  306. </li>
  307. <li class="class-depth-1">
  308. <a href="Phaser.Sprite.html">Sprite</a>
  309. </li>
  310. <li class="class-depth-1">
  311. <a href="Phaser.SpriteBatch.html">SpriteBatch</a>
  312. </li>
  313. <li class="class-depth-1">
  314. <a href="Phaser.Stage.html">Stage</a>
  315. </li>
  316. <li class="class-depth-1">
  317. <a href="Phaser.State.html">State</a>
  318. </li>
  319. <li class="class-depth-1">
  320. <a href="Phaser.StateManager.html">StateManager</a>
  321. </li>
  322. <li class="class-depth-1">
  323. <a href="Phaser.Text.html">Text</a>
  324. </li>
  325. <li class="class-depth-1">
  326. <a href="Phaser.Tile.html">Tile</a>
  327. </li>
  328. <li class="class-depth-1">
  329. <a href="Phaser.Tilemap.html">Tilemap</a>
  330. </li>
  331. <li class="class-depth-1">
  332. <a href="Phaser.TilemapLayer.html">TilemapLayer</a>
  333. </li>
  334. <li class="class-depth-1">
  335. <a href="Phaser.TilemapParser.html">TilemapParser</a>
  336. </li>
  337. <li class="class-depth-1">
  338. <a href="Phaser.Tileset.html">Tileset</a>
  339. </li>
  340. <li class="class-depth-1">
  341. <a href="Phaser.TileSprite.html">TileSprite</a>
  342. </li>
  343. <li class="class-depth-1">
  344. <a href="Phaser.Time.html">Time</a>
  345. </li>
  346. <li class="class-depth-1">
  347. <a href="Phaser.Timer.html">Timer</a>
  348. </li>
  349. <li class="class-depth-1">
  350. <a href="Phaser.TimerEvent.html">TimerEvent</a>
  351. </li>
  352. <li class="class-depth-1">
  353. <a href="Phaser.Touch.html">Touch</a>
  354. </li>
  355. <li class="class-depth-1">
  356. <a href="Phaser.Tween.html">Tween</a>
  357. </li>
  358. <li class="class-depth-1">
  359. <a href="Phaser.TweenManager.html">TweenManager</a>
  360. </li>
  361. <li class="class-depth-1">
  362. <a href="Phaser.Utils.html">Utils</a>
  363. </li>
  364. <li class="class-depth-2">
  365. <a href="Phaser.Utils.Debug.html">Debug</a>
  366. </li>
  367. <li class="class-depth-1">
  368. <a href="Phaser.World.html">World</a>
  369. </li>
  370. </ul>
  371. </li>
  372. </ul>
  373. </div>
  374. </div>
  375. <div class="row-fluid">
  376. <div class="span12">
  377. <div id="main">
  378. <h1 class="page-title">Source: math/Math.js</h1>
  379. <section>
  380. <article>
  381. <pre class="sunlight-highlight-javascript linenums">/**
  382. * @author Richard Davey &lt;rich@photonstorm.com>
  383. * @copyright 2014 Photon Storm Ltd.
  384. * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
  385. */
  386. /**
  387. * A collection of mathematical methods.
  388. *
  389. * @class Phaser.Math
  390. */
  391. Phaser.Math = {
  392. /**
  393. * = 2 &pi;
  394. * @method Phaser.Math#PI2
  395. */
  396. PI2: Math.PI * 2,
  397. /**
  398. * Two number are fuzzyEqual if their difference is less than &epsilon;.
  399. * @method Phaser.Math#fuzzyEqual
  400. * @param {number} a
  401. * @param {number} b
  402. * @param {number} epsilon
  403. * @return {boolean} True if |a-b|&lt;&epsilon;
  404. */
  405. fuzzyEqual: function (a, b, epsilon) {
  406. if (typeof epsilon === "undefined") { epsilon = 0.0001; }
  407. return Math.abs(a - b) &lt; epsilon;
  408. },
  409. /**
  410. * a is fuzzyLessThan b if it is less than b + &epsilon;.
  411. * @method Phaser.Math#fuzzyLessThan
  412. * @param {number} a
  413. * @param {number} b
  414. * @param {number} epsilon
  415. * @return {boolean} True if a&lt;b+&epsilon;
  416. */
  417. fuzzyLessThan: function (a, b, epsilon) {
  418. if (typeof epsilon === "undefined") { epsilon = 0.0001; }
  419. return a &lt; b + epsilon;
  420. },
  421. /**
  422. * a is fuzzyGreaterThan b if it is more than b - &epsilon;.
  423. * @method Phaser.Math#fuzzyGreaterThan
  424. * @param {number} a
  425. * @param {number} b
  426. * @param {number} epsilon
  427. * @return {boolean} True if a>b+&epsilon;
  428. */
  429. fuzzyGreaterThan: function (a, b, epsilon) {
  430. if (typeof epsilon === "undefined") { epsilon = 0.0001; }
  431. return a > b - epsilon;
  432. },
  433. /**
  434. * @method Phaser.Math#fuzzyCeil
  435. * @param {number} val
  436. * @param {number} epsilon
  437. * @return {boolean} ceiling(val-&epsilon;)
  438. */
  439. fuzzyCeil: function (val, epsilon) {
  440. if (typeof epsilon === "undefined") { epsilon = 0.0001; }
  441. return Math.ceil(val - epsilon);
  442. },
  443. /**
  444. * @method Phaser.Math#fuzzyFloor
  445. * @param {number} val
  446. * @param {number} epsilon
  447. * @return {boolean} floor(val-&epsilon;)
  448. */
  449. fuzzyFloor: function (val, epsilon) {
  450. if (typeof epsilon === "undefined") { epsilon = 0.0001; }
  451. return Math.floor(val + epsilon);
  452. },
  453. /**
  454. * Averages all values passed to the function and returns the result. You can pass as many parameters as you like.
  455. * @method Phaser.Math#average
  456. * @return {number} The average of all given values.
  457. */
  458. average: function () {
  459. var args = [];
  460. for (var _i = 0; _i &lt; (arguments.length - 0); _i++) {
  461. args[_i] = arguments[_i + 0];
  462. }
  463. var avg = 0;
  464. for (var i = 0; i &lt; args.length; i++) {
  465. avg += args[i];
  466. }
  467. return avg / args.length;
  468. },
  469. /**
  470. * @method Phaser.Math#truncate
  471. * @param {number} n
  472. * @return {number}
  473. */
  474. truncate: function (n) {
  475. return (n > 0) ? Math.floor(n) : Math.ceil(n);
  476. },
  477. /**
  478. * @method Phaser.Math#shear
  479. * @param {number} n
  480. * @return {number} n mod 1
  481. */
  482. shear: function (n) {
  483. return n % 1;
  484. },
  485. /**
  486. * Snap a value to nearest grid slice, using rounding.
  487. *
  488. * Example: if you have an interval gap of 5 and a position of 12... you will snap to 10 whereas 14 will snap to 15.
  489. *
  490. * @method Phaser.Math#snapTo
  491. * @param {number} input - The value to snap.
  492. * @param {number} gap - The interval gap of the grid.
  493. * @param {number} [start] - Optional starting offset for gap.
  494. * @return {number}
  495. */
  496. snapTo: function (input, gap, start) {
  497. if (typeof start === "undefined") { start = 0; }
  498. if (gap === 0) {
  499. return input;
  500. }
  501. input -= start;
  502. input = gap * Math.round(input / gap);
  503. return start + input;
  504. },
  505. /**
  506. * Snap a value to nearest grid slice, using floor.
  507. *
  508. * Example: if you have an interval gap of 5 and a position of 12... you will snap to 10. As will 14 snap to 10... but 16 will snap to 15
  509. *
  510. * @method Phaser.Math#snapToFloor
  511. * @param {number} input - The value to snap.
  512. * @param {number} gap - The interval gap of the grid.
  513. * @param {number} [start] - Optional starting offset for gap.
  514. * @return {number}
  515. */
  516. snapToFloor: function (input, gap, start) {
  517. if (typeof start === "undefined") { start = 0; }
  518. if (gap === 0) {
  519. return input;
  520. }
  521. input -= start;
  522. input = gap * Math.floor(input / gap);
  523. return start + input;
  524. },
  525. /**
  526. * Snap a value to nearest grid slice, using ceil.
  527. *
  528. * Example: if you have an interval gap of 5 and a position of 12... you will snap to 15. As will 14 will snap to 15... but 16 will snap to 20.
  529. *
  530. * @method Phaser.Math#snapToCeil
  531. * @param {number} input - The value to snap.
  532. * @param {number} gap - The interval gap of the grid.
  533. * @param {number} [start] - Optional starting offset for gap.
  534. * @return {number}
  535. */
  536. snapToCeil: function (input, gap, start) {
  537. if (typeof start === "undefined") { start = 0; }
  538. if (gap === 0) {
  539. return input;
  540. }
  541. input -= start;
  542. input = gap * Math.ceil(input / gap);
  543. return start + input;
  544. },
  545. /**
  546. * Snaps a value to the nearest value in an array.
  547. * @method Phaser.Math#snapToInArray
  548. * @param {number} input
  549. * @param {array} arr
  550. * @param {boolean} sort - True if the array needs to be sorted.
  551. * @return {number}
  552. */
  553. snapToInArray: function (input, arr, sort) {
  554. if (typeof sort === "undefined") { sort = true; }
  555. if (sort) {
  556. arr.sort();
  557. }
  558. if (input &lt; arr[0]) {
  559. return arr[0];
  560. }
  561. var i = 1;
  562. while (arr[i] &lt; input) {
  563. i++;
  564. }
  565. var low = arr[i - 1];
  566. var high = (i &lt; arr.length) ? arr[i] : Number.POSITIVE_INFINITY;
  567. return ((high - input) &lt;= (input - low)) ? high : low;
  568. },
  569. /**
  570. * Round to some place comparative to a 'base', default is 10 for decimal place.
  571. *
  572. * 'place' is represented by the power applied to 'base' to get that place
  573. * e.g.
  574. * 2000/7 ~= 285.714285714285714285714 ~= (bin)100011101.1011011011011011
  575. *
  576. * roundTo(2000/7,3) === 0
  577. * roundTo(2000/7,2) == 300
  578. * roundTo(2000/7,1) == 290
  579. * roundTo(2000/7,0) == 286
  580. * roundTo(2000/7,-1) == 285.7
  581. * roundTo(2000/7,-2) == 285.71
  582. * roundTo(2000/7,-3) == 285.714
  583. * roundTo(2000/7,-4) == 285.7143
  584. * roundTo(2000/7,-5) == 285.71429
  585. *
  586. * roundTo(2000/7,3,2) == 288 -- 100100000
  587. * roundTo(2000/7,2,2) == 284 -- 100011100
  588. * roundTo(2000/7,1,2) == 286 -- 100011110
  589. * roundTo(2000/7,0,2) == 286 -- 100011110
  590. * roundTo(2000/7,-1,2) == 285.5 -- 100011101.1
  591. * roundTo(2000/7,-2,2) == 285.75 -- 100011101.11
  592. * roundTo(2000/7,-3,2) == 285.75 -- 100011101.11
  593. * roundTo(2000/7,-4,2) == 285.6875 -- 100011101.1011
  594. * roundTo(2000/7,-5,2) == 285.71875 -- 100011101.10111
  595. *
  596. * Note what occurs when we round to the 3rd space (8ths place), 100100000, this is to be assumed
  597. * because we are rounding 100011.1011011011011011 which rounds up.
  598. *
  599. * @method Phaser.Math#roundTo
  600. * @param {number} value - The value to round.
  601. * @param {number} place - The place to round to.
  602. * @param {number} base - The base to round in... default is 10 for decimal.
  603. * @return {number}
  604. */
  605. roundTo: function (value, place, base) {
  606. if (typeof place === "undefined") { place = 0; }
  607. if (typeof base === "undefined") { base = 10; }
  608. var p = Math.pow(base, -place);
  609. return Math.round(value * p) / p;
  610. },
  611. /**
  612. * @method Phaser.Math#floorTo
  613. * @param {number} value - The value to round.
  614. * @param {number} place - The place to round to.
  615. * @param {number} base - The base to round in... default is 10 for decimal.
  616. * @return {number}
  617. */
  618. floorTo: function (value, place, base) {
  619. if (typeof place === "undefined") { place = 0; }
  620. if (typeof base === "undefined") { base = 10; }
  621. var p = Math.pow(base, -place);
  622. return Math.floor(value * p) / p;
  623. },
  624. /**
  625. * @method Phaser.Math#ceilTo
  626. * @param {number} value - The value to round.
  627. * @param {number} place - The place to round to.
  628. * @param {number} base - The base to round in... default is 10 for decimal.
  629. * @return {number}
  630. */
  631. ceilTo: function (value, place, base) {
  632. if (typeof place === "undefined") { place = 0; }
  633. if (typeof base === "undefined") { base = 10; }
  634. var p = Math.pow(base, -place);
  635. return Math.ceil(value * p) / p;
  636. },
  637. /**
  638. * A one dimensional linear interpolation of a value.
  639. * @method Phaser.Math#interpolateFloat
  640. * @param {number} a
  641. * @param {number} b
  642. * @param {number} weight
  643. * @return {number}
  644. */
  645. interpolateFloat: function (a, b, weight) {
  646. return (b - a) * weight + a;
  647. },
  648. /**
  649. * Find the angle of a segment from (x1, y1) -> (x2, y2).
  650. * @method Phaser.Math#angleBetween
  651. * @param {number} x1
  652. * @param {number} y1
  653. * @param {number} x2
  654. * @param {number} y2
  655. * @return {number}
  656. */
  657. angleBetween: function (x1, y1, x2, y2) {
  658. return Math.atan2(x2 - x1, y2 - y1);
  659. },
  660. /**
  661. * Find the angle of a segment from (point1.x, point1.y) -> (point2.x, point2.y).
  662. * @method Phaser.Math#angleBetweenPoints
  663. * @param {Phaser.Point} point1
  664. * @param {Phaser.Point} point2
  665. * @return {number}
  666. */
  667. angleBetweenPoints: function (point1, point2) {
  668. return Math.atan2(point2.x - point1.x, point2.y - point1.y);
  669. },
  670. /**
  671. * Reverses an angle.
  672. * @method Phaser.Math#reverseAngle
  673. * @param {number} angleRad - The angle to reverse, in radians.
  674. * @return {number} Returns the reverse angle, in radians.
  675. */
  676. reverseAngle: function (angleRad) {
  677. return this.normalizeAngle(angleRad + Math.PI, true);
  678. },
  679. /**
  680. * Normalizes an angle to the [0,2pi) range.
  681. * @method Phaser.Math#normalizeAngle
  682. * @param {number} angleRad - The angle to normalize, in radians.
  683. * @return {number} Returns the angle, fit within the [0,2pi] range, in radians.
  684. */
  685. normalizeAngle: function (angleRad) {
  686. angleRad = angleRad % (2 * Math.PI);
  687. return angleRad >= 0 ? angleRad : angleRad + 2 * Math.PI;
  688. },
  689. /**
  690. * Normalizes a latitude to the [-90,90] range. Latitudes above 90 or below -90 are capped, not wrapped.
  691. * @method Phaser.Math#normalizeLatitude
  692. * @param {number} lat - The latitude to normalize, in degrees.
  693. * @return {number} Returns the latitude, fit within the [-90,90] range.
  694. */
  695. normalizeLatitude: function (lat) {
  696. return Math.max(-90, Math.min(90, lat));
  697. },
  698. /**
  699. * Normalizes a longitude to the [-180,180] range. Longitudes above 180 or below -180 are wrapped.
  700. * @method Phaser.Math#normalizeLongitude
  701. * @param {number} lng - The longitude to normalize, in degrees.
  702. * @return {number} Returns the longitude, fit within the [-180,180] range.
  703. */
  704. normalizeLongitude: function (lng) {
  705. if (lng % 360 == 180)
  706. {
  707. return 180;
  708. }
  709. lng = lng % 360;
  710. return lng &lt; -180 ? lng + 360 : lng > 180 ? lng - 360 : lng;
  711. },
  712. /**
  713. * Closest angle between two angles from a1 to a2 absolute value the return for exact angle
  714. * @method Phaser.Math#nearestAngleBetween
  715. * @param {number} a1
  716. * @param {number} a2
  717. * @param {boolean} radians - True if angle sizes are expressed in radians.
  718. * @return {number}
  719. */
  720. nearestAngleBetween: function (a1, a2, radians) {
  721. if (typeof radians === "undefined") { radians = true; }
  722. var rd = (radians) ? Math.PI : 180;
  723. a1 = this.normalizeAngle(a1, radians);
  724. a2 = this.normalizeAngle(a2, radians);
  725. if (a1 &lt; -rd / 2 && a2 > rd / 2)
  726. {
  727. a1 += rd * 2;
  728. }
  729. if (a2 &lt; -rd / 2 && a1 > rd / 2)
  730. {
  731. a2 += rd * 2;
  732. }
  733. return a2 - a1;
  734. },
  735. /**
  736. * Interpolate across the shortest arc between two angles.
  737. * @method Phaser.Math#interpolateAngles
  738. * @param {number} a1 - Description.
  739. * @param {number} a2 - Description.
  740. * @param {number} weight - Description.
  741. * @param {boolean} radians - True if angle sizes are expressed in radians.
  742. * @param {Description} ease - Description.
  743. * @return {number}
  744. */
  745. interpolateAngles: function (a1, a2, weight, radians, ease) {
  746. if (typeof radians === "undefined") { radians = true; }
  747. if (typeof ease === "undefined") { ease = null; }
  748. a1 = this.normalizeAngle(a1, radians);
  749. a2 = this.normalizeAngleToAnother(a2, a1, radians);
  750. return (typeof ease === 'function') ? ease(weight, a1, a2 - a1, 1) : this.interpolateFloat(a1, a2, weight);
  751. },
  752. /**
  753. * Generate a random bool result based on the chance value.
  754. * &lt;p>
  755. * Returns true or false based on the chance value (default 50%). For example if you wanted a player to have a 30% chance
  756. * of getting a bonus, call chanceRoll(30) - true means the chance passed, false means it failed.
  757. * &lt;/p>
  758. * @method Phaser.Math#chanceRoll
  759. * @param {number} chance - The chance of receiving the value. A number between 0 and 100 (effectively 0% to 100%).
  760. * @return {boolean} True if the roll passed, or false otherwise.
  761. */
  762. chanceRoll: function (chance) {
  763. if (typeof chance === "undefined") { chance = 50; }
  764. if (chance &lt;= 0)
  765. {
  766. return false;
  767. }
  768. else if (chance >= 100)
  769. {
  770. return true;
  771. }
  772. else
  773. {
  774. if (Math.random() * 100 >= chance)
  775. {
  776. return false;
  777. }
  778. else
  779. {
  780. return true;
  781. }
  782. }
  783. },
  784. /**
  785. * Returns an Array containing the numbers from min to max (inclusive).
  786. *
  787. * @method Phaser.Math#numberArray
  788. * @param {number} min - The minimum value the array starts with.
  789. * @param {number} max - The maximum value the array contains.
  790. * @return {array} The array of number values.
  791. */
  792. numberArray: function (min, max) {
  793. var result = [];
  794. for (var i = min; i &lt;= max; i++)
  795. {
  796. result.push(i);
  797. }
  798. return result;
  799. },
  800. /**
  801. * Adds the given amount to the value, but never lets the value go over the specified maximum.
  802. *
  803. * @method Phaser.Math#maxAdd
  804. * @param {number} value - The value to add the amount to.
  805. * @param {number} amount - The amount to add to the value.
  806. * @param {number} max- The maximum the value is allowed to be.
  807. * @return {number}
  808. */
  809. maxAdd: function (value, amount, max) {
  810. value += amount;
  811. if (value > max)
  812. {
  813. value = max;
  814. }
  815. return value;
  816. },
  817. /**
  818. * Subtracts the given amount from the value, but never lets the value go below the specified minimum.
  819. *
  820. * @method Phaser.Math#minSub
  821. * @param {number} value - The base value.
  822. * @param {number} amount - The amount to subtract from the base value.
  823. * @param {number} min - The minimum the value is allowed to be.
  824. * @return {number} The new value.
  825. */
  826. minSub: function (value, amount, min) {
  827. value -= amount;
  828. if (value &lt; min)
  829. {
  830. value = min;
  831. }
  832. return value;
  833. },
  834. /**
  835. * Ensures that the value always stays between min and max, by wrapping the value around.
  836. * max should be larger than min, or the function will return 0.
  837. *
  838. * @method Phaser.Math#wrap
  839. * @param {number} value - The value to wrap.
  840. * @param {number} min - The minimum the value is allowed to be.
  841. * @param {number} max - The maximum the value is allowed to be.
  842. * @return {number} The wrapped value.
  843. */
  844. wrap: function (value, min, max) {
  845. var range = max - min;
  846. if (range &lt;= 0)
  847. {
  848. return 0;
  849. }
  850. var result = (value - min) % range;
  851. if (result &lt; 0)
  852. {
  853. result += range;
  854. }
  855. return result + min;
  856. },
  857. /**
  858. * Adds value to amount and ensures that the result always stays between 0 and max, by wrapping the value around.
  859. * Values must be positive integers, and are passed through Math.abs.
  860. *
  861. * @method Phaser.Math#wrapValue
  862. * @param {number} value - The value to add the amount to.
  863. * @param {number} amount - The amount to add to the value.
  864. * @param {number} max - The maximum the value is allowed to be.
  865. * @return {number} The wrapped value.
  866. */
  867. wrapValue: function (value, amount, max) {
  868. var diff;
  869. value = Math.abs(value);
  870. amount = Math.abs(amount);
  871. max = Math.abs(max);
  872. diff = (value + amount) % max;
  873. return diff;
  874. },
  875. /**
  876. * Randomly returns either a 1 or -1.
  877. *
  878. * @method Phaser.Math#randomSign
  879. * @return {number} 1 or -1
  880. */
  881. randomSign: function () {
  882. return (Math.random() > 0.5) ? 1 : -1;
  883. },
  884. /**
  885. * Returns true if the number given is odd.
  886. *
  887. * @method Phaser.Math#isOdd
  888. * @param {number} n - The number to check.
  889. * @return {boolean} True if the given number is odd. False if the given number is even.
  890. */
  891. isOdd: function (n) {
  892. return (n & 1);
  893. },
  894. /**
  895. * Returns true if the number given is even.
  896. *
  897. * @method Phaser.Math#isEven
  898. * @param {number} n - The number to check.
  899. * @return {boolean} True if the given number is even. False if the given number is odd.
  900. */
  901. isEven: function (n) {
  902. if (n & 1)
  903. {
  904. return false;
  905. }
  906. else
  907. {
  908. return true;
  909. }
  910. },
  911. /**
  912. * Updated version of Math.min that can be passed either an array of numbers or the numbers as parameters.
  913. * See http://jsperf.com/math-s-min-max-vs-homemade/5
  914. *
  915. * @method Phaser.Math#min
  916. * @return {number} The lowest value from those given.
  917. */
  918. min: function () {
  919. if (arguments.length === 1 && typeof arguments[0] === 'object')
  920. {
  921. var data = arguments[0];
  922. }
  923. else
  924. {
  925. var data = arguments;
  926. }
  927. for (var i = 1, min = 0, len = data.length; i &lt; len; i++)
  928. {
  929. if (data[i] &lt; data[min])
  930. {
  931. min = i;
  932. }
  933. }
  934. return data[min];
  935. },
  936. /**
  937. * Updated version of Math.max that can be passed either an array of numbers or the numbers as parameters.
  938. *
  939. * @method Phaser.Math#max
  940. * @return {number} The largest value from those given.
  941. */
  942. max: function () {
  943. if (arguments.length === 1 && typeof arguments[0] === 'object')
  944. {
  945. var data = arguments[0];
  946. }
  947. else
  948. {
  949. var data = arguments;
  950. }
  951. for (var i = 1, max = 0, len = data.length; i &lt; len; i++)
  952. {
  953. if (data[i] > data[max])
  954. {
  955. max = i;
  956. }
  957. }
  958. return data[max];
  959. },
  960. /**
  961. * Updated version of Math.min that can be passed a property and either an array of objects or the objects as parameters.
  962. * It will find the lowest matching property value from the given objects.
  963. *
  964. * @method Phaser.Math#minProperty
  965. * @return {number} The lowest value from those given.
  966. */
  967. minProperty: function (property) {
  968. if (arguments.length === 2 && typeof arguments[1] === 'object')
  969. {
  970. var data = arguments[1];
  971. }
  972. else
  973. {
  974. var data = arguments.slice(1);
  975. }
  976. for (var i = 1, min = 0, len = data.length; i &lt; len; i++)
  977. {
  978. if (data[i][property] &lt; data[min][property])
  979. {
  980. min = i;
  981. }
  982. }
  983. return data[min][property];
  984. },
  985. /**
  986. * Updated version of Math.max that can be passed a property and either an array of objects or the objects as parameters.
  987. * It will find the largest matching property value from the given objects.
  988. *
  989. * @method Phaser.Math#maxProperty
  990. * @return {number} The largest value from those given.
  991. */
  992. maxProperty: function (property) {
  993. if (arguments.length === 2 && typeof arguments[1] === 'object')
  994. {
  995. var data = arguments[1];
  996. }
  997. else
  998. {
  999. var data = arguments.slice(1);
  1000. }
  1001. for (var i = 1, max = 0, len = data.length; i &lt; len; i++)
  1002. {
  1003. if (data[i][property] > data[max][property])
  1004. {
  1005. max = i;
  1006. }
  1007. }
  1008. return data[max][property];
  1009. },
  1010. /**
  1011. * Keeps an angle value between -180 and +180&lt;br>
  1012. * Should be called whenever the angle is updated on the Sprite to stop it from going insane.
  1013. *
  1014. * @method Phaser.Math#wrapAngle
  1015. * @param {number} angle - The angle value to check
  1016. * @param {boolean} radians - True if angle sizes are expressed in radians.
  1017. * @return {number} The new angle value, returns the same as the input angle if it was within bounds.
  1018. */
  1019. wrapAngle: function (angle, radians) {
  1020. var radianFactor = (radians) ? Math.PI / 180 : 1;
  1021. return this.wrap(angle, -180 * radianFactor, 180 * radianFactor);
  1022. },
  1023. /**
  1024. * Keeps an angle value between the given min and max values.
  1025. *
  1026. * @method Phaser.Math#angleLimit
  1027. * @param {number} angle - The angle value to check. Must be between -180 and +180.
  1028. * @param {number} min - The minimum angle that is allowed (must be -180 or greater).
  1029. * @param {number} max - The maximum angle that is allowed (must be 180 or less).
  1030. * @return {number} The new angle value, returns the same as the input angle if it was within bounds
  1031. */
  1032. angleLimit: function (angle, min, max) {
  1033. var result = angle;
  1034. if (angle > max)
  1035. {
  1036. result = max;
  1037. }
  1038. else if (angle &lt; min)
  1039. {
  1040. result = min;
  1041. }
  1042. return result;
  1043. },
  1044. /**
  1045. * A Linear Interpolation Method, mostly used by Phaser.Tween.
  1046. * @method Phaser.Math#linearInterpolation
  1047. * @param {number} v
  1048. * @param {number} k
  1049. * @return {number}
  1050. */
  1051. linearInterpolation: function (v, k) {
  1052. var m = v.length - 1;
  1053. var f = m * k;
  1054. var i = Math.floor(f);
  1055. if (k &lt; 0)
  1056. {
  1057. return this.linear(v[0], v[1], f);
  1058. }
  1059. if (k > 1)
  1060. {
  1061. return this.linear(v[m], v[m - 1], m - f);
  1062. }
  1063. return this.linear(v[i], v[i + 1 > m ? m : i + 1], f - i);
  1064. },
  1065. /**
  1066. * A Bezier Interpolation Method, mostly used by Phaser.Tween.
  1067. * @method Phaser.Math#bezierInterpolation
  1068. * @param {number} v
  1069. * @param {number} k
  1070. * @return {number}
  1071. */
  1072. bezierInterpolation: function (v, k) {
  1073. var b = 0;
  1074. var n = v.length - 1;
  1075. for (var i = 0; i &lt;= n; i++)
  1076. {
  1077. b += Math.pow(1 - k, n - i) * Math.pow(k, i) * v[i] * this.bernstein(n, i);
  1078. }
  1079. return b;
  1080. },
  1081. /**
  1082. * A Catmull Rom Interpolation Method, mostly used by Phaser.Tween.
  1083. * @method Phaser.Math#catmullRomInterpolation
  1084. * @param {number} v
  1085. * @param {number} k
  1086. * @return {number}
  1087. */
  1088. catmullRomInterpolation: function (v, k) {
  1089. var m = v.length - 1;
  1090. var f = m * k;
  1091. var i = Math.floor(f);
  1092. if (v[0] === v[m])
  1093. {
  1094. if (k &lt; 0)
  1095. {
  1096. i = Math.floor(f = m * (1 + k));
  1097. }
  1098. return this.catmullRom(v[(i - 1 + m) % m], v[i], v[(i + 1) % m], v[(i + 2) % m], f - i);
  1099. }
  1100. else
  1101. {
  1102. if (k &lt; 0)
  1103. {
  1104. return v[0] - (this.catmullRom(v[0], v[0], v[1], v[1], -f) - v[0]);
  1105. }
  1106. if (k > 1)
  1107. {
  1108. return v[m] - (this.catmullRom(v[m], v[m], v[m - 1], v[m - 1], f - m) - v[m]);
  1109. }
  1110. return this.catmullRom(v[i ? i - 1 : 0], v[i], v[m &lt; i + 1 ? m : i + 1], v[m &lt; i + 2 ? m : i + 2], f - i);
  1111. }
  1112. },
  1113. /**
  1114. * Description.
  1115. * @method Phaser.Math#Linear
  1116. * @param {number} p0
  1117. * @param {number} p1
  1118. * @param {number} t
  1119. * @return {number}
  1120. */
  1121. linear: function (p0, p1, t) {
  1122. return (p1 - p0) * t + p0;
  1123. },
  1124. /**
  1125. * @method Phaser.Math#bernstein
  1126. * @param {number} n
  1127. * @param {number} i
  1128. * @return {number}
  1129. */
  1130. bernstein: function (n, i) {
  1131. return this.factorial(n) / this.factorial(i) / this.factorial(n - i);
  1132. },
  1133. /**
  1134. * Description.
  1135. * @method Phaser.Math#catmullRom
  1136. * @param {number} p0
  1137. * @param {number} p1
  1138. * @param {number} p2
  1139. * @param {number} p3
  1140. * @param {number} t
  1141. * @return {number}
  1142. */
  1143. catmullRom: function (p0, p1, p2, p3, t) {
  1144. var v0 = (p2 - p0) * 0.5, v1 = (p3 - p1) * 0.5, t2 = t * t, t3 = t * t2;
  1145. return (2 * p1 - 2 * p2 + v0 + v1) * t3 + (-3 * p1 + 3 * p2 - 2 * v0 - v1) * t2 + v0 * t + p1;
  1146. },
  1147. /**
  1148. * @method Phaser.Math#difference
  1149. * @param {number} a
  1150. * @param {number} b
  1151. * @return {number}
  1152. */
  1153. difference: function (a, b) {
  1154. return Math.abs(a - b);
  1155. },
  1156. /**
  1157. * Fetch a random entry from the given array.
  1158. * Will return null if random selection is missing, or array has no entries.
  1159. *
  1160. * @method Phaser.Math#getRandom
  1161. * @param {array} objects - An array of objects.
  1162. * @param {number} startIndex - Optional offset off the front of the array. Default value is 0, or the beginning of the array.
  1163. * @param {number} length - Optional restriction on the number of values you want to randomly select from.
  1164. * @return {object} The random object that was selected.
  1165. */
  1166. getRandom: function (objects, startIndex, length) {
  1167. if (typeof startIndex === "undefined") { startIndex = 0; }
  1168. if (typeof length === "undefined") { length = 0; }
  1169. if (objects != null) {
  1170. var l = length;
  1171. if ((l === 0) || (l > objects.length - startIndex))
  1172. {
  1173. l = objects.length - startIndex;
  1174. }
  1175. if (l > 0)
  1176. {
  1177. return objects[startIndex + Math.floor(Math.random() * l)];
  1178. }
  1179. }
  1180. return null;
  1181. },
  1182. /**
  1183. * Removes a random object from the given array and returns it.
  1184. * Will return null if random selection is missing, or array has no entries.
  1185. *
  1186. * @method Phaser.Math#removeRandom
  1187. * @param {array} objects - An array of objects.
  1188. * @param {number} startIndex - Optional offset off the front of the array. Default value is 0, or the beginning of the array.
  1189. * @param {number} length - Optional restriction on the number of values you want to randomly select from.
  1190. * @return {object} The random object that was removed.
  1191. */
  1192. removeRandom: function (objects, startIndex, length) {
  1193. if (typeof startIndex === "undefined") { startIndex = 0; }
  1194. if (typeof length === "undefined") { length = 0; }
  1195. if (objects != null) {
  1196. var l = length;
  1197. if ((l === 0) || (l > objects.length - startIndex))
  1198. {
  1199. l = objects.length - startIndex;
  1200. }
  1201. if (l > 0)
  1202. {
  1203. var idx = startIndex + Math.floor(Math.random() * l);
  1204. var removed = objects.splice(idx, 1);
  1205. return removed[0];
  1206. }
  1207. }
  1208. return null;
  1209. },
  1210. /**
  1211. * Round down to the next whole number. E.g. floor(1.7) == 1, and floor(-2.7) == -2.
  1212. *
  1213. * @method Phaser.Math#floor
  1214. * @param {number} Value Any number.
  1215. * @return {number} The rounded value of that number.
  1216. */
  1217. floor: function (value) {
  1218. var n = value | 0;
  1219. return (value > 0) ? (n) : ((n != value) ? (n - 1) : (n));
  1220. },
  1221. /**
  1222. * Round up to the next whole number. E.g. ceil(1.3) == 2, and ceil(-2.3) == -3.
  1223. *
  1224. * @method Phaser.Math#ceil
  1225. * @param {number} value - Any number.
  1226. * @return {number} The rounded value of that number.
  1227. */
  1228. ceil: function (value) {
  1229. var n = value | 0;
  1230. return (value > 0) ? ((n != value) ? (n + 1) : (n)) : (n);
  1231. },
  1232. /**
  1233. * Generate a sine and cosine table simultaneously and extremely quickly. Based on research by Franky of scene.at
  1234. * &lt;p>
  1235. * The parameters allow you to specify the length, amplitude and frequency of the wave. Once you have called this function
  1236. * you should get the results via getSinTable() and getCosTable(). This generator is fast enough to be used in real-time.
  1237. * &lt;/p>
  1238. * @method Phaser.Math#sinCosGenerator
  1239. * @param {number} length - The length of the wave
  1240. * @param {number} sinAmplitude - The amplitude to apply to the sine table (default 1.0) if you need values between say -+ 125 then give 125 as the value
  1241. * @param {number} cosAmplitude - The amplitude to apply to the cosine table (default 1.0) if you need values between say -+ 125 then give 125 as the value
  1242. * @param {number} frequency - The frequency of the sine and cosine table data
  1243. * @return {Array} Returns the sine table
  1244. */
  1245. sinCosGenerator: function (length, sinAmplitude, cosAmplitude, frequency) {
  1246. if (typeof sinAmplitude === "undefined") { sinAmplitude = 1.0; }
  1247. if (typeof cosAmplitude === "undefined") { cosAmplitude = 1.0; }
  1248. if (typeof frequency === "undefined") { frequency = 1.0; }
  1249. var sin = sinAmplitude;
  1250. var cos = cosAmplitude;
  1251. var frq = frequency * Math.PI / length;
  1252. var cosTable = [];
  1253. var sinTable = [];
  1254. for (var c = 0; c &lt; length; c++) {
  1255. cos -= sin * frq;
  1256. sin += cos * frq;
  1257. cosTable[c] = cos;
  1258. sinTable[c] = sin;
  1259. }
  1260. return { sin: sinTable, cos: cosTable, length: length };
  1261. },
  1262. /**
  1263. * Removes the top element from the stack and re-inserts it onto the bottom, then returns it.
  1264. * The original stack is modified in the process. This effectively moves the position of the data from the start to the end of the table.
  1265. *
  1266. * @method Phaser.Math#shift
  1267. * @param {array} stack - The array to shift.
  1268. * @return {any} The shifted value.
  1269. */
  1270. shift: function (stack) {
  1271. var s = stack.shift();
  1272. stack.push(s);
  1273. return s;
  1274. },
  1275. /**
  1276. * Shuffles the data in the given array into a new order
  1277. * @method Phaser.Math#shuffleArray
  1278. * @param {array} array - The array to shuffle
  1279. * @return {array} The array
  1280. */
  1281. shuffleArray: function (array) {
  1282. for (var i = array.length - 1; i > 0; i--) {
  1283. var j = Math.floor(Math.random() * (i + 1));
  1284. var temp = array[i];
  1285. array[i] = array[j];
  1286. array[j] = temp;
  1287. }
  1288. return array;
  1289. },
  1290. /**
  1291. * Returns the distance between the two given set of coordinates.
  1292. *
  1293. * @method Phaser.Math#distance
  1294. * @param {number} x1
  1295. * @param {number} y1
  1296. * @param {number} x2
  1297. * @param {number} y2
  1298. * @return {number} The distance between the two sets of coordinates.
  1299. */
  1300. distance: function (x1, y1, x2, y2) {
  1301. var dx = x1 - x2;
  1302. var dy = y1 - y2;
  1303. return Math.sqrt(dx * dx + dy * dy);
  1304. },
  1305. /**
  1306. * Returns the distance between the two given set of coordinates at the power given.
  1307. *
  1308. * @method Phaser.Math#distancePow
  1309. * @param {number} x1
  1310. * @param {number} y1
  1311. * @param {number} x2
  1312. * @param {number} y2
  1313. * @param {number} [pow=2]
  1314. * @return {number} The distance between the two sets of coordinates.
  1315. */
  1316. distancePow: function (x1, y1, x2, y2, pow) {
  1317. if (typeof pow === 'undefined') { pow = 2; }
  1318. return Math.sqrt(Math.pow(x2 - x1, pow) + Math.pow(y2 - y1, pow));
  1319. },
  1320. /**
  1321. * Returns the rounded distance between the two given set of coordinates.
  1322. *
  1323. * @method Phaser.Math#distanceRounded
  1324. * @param {number} x1
  1325. * @param {number} y1
  1326. * @param {number} x2
  1327. * @param {number} y2
  1328. * @return {number} The distance between this Point object and the destination Point object.
  1329. */
  1330. distanceRounded: function (x1, y1, x2, y2) {
  1331. return Math.round(Phaser.Math.distance(x1, y1, x2, y2));
  1332. },
  1333. /**
  1334. * Force a value within the boundaries of two values.
  1335. * Clamp value to range &lt;a, b>
  1336. *
  1337. * @method Phaser.Math#clamp
  1338. * @param {number} x
  1339. * @param {number} a
  1340. * @param {number} b
  1341. * @return {number}
  1342. */
  1343. clamp: function ( x, a, b ) {
  1344. return ( x &lt; a ) ? a : ( ( x > b ) ? b : x );
  1345. },
  1346. /**
  1347. * Clamp value to range &lt;a, inf).
  1348. *
  1349. * @method Phaser.Math#clampBottom
  1350. * @param {number} x
  1351. * @param {number} a
  1352. * @return {number}
  1353. */
  1354. clampBottom: function ( x, a ) {
  1355. return x &lt; a ? a : x;
  1356. },
  1357. /**
  1358. * Checks if two values are within the given tolerance of each other.
  1359. *
  1360. * @method Phaser.Math#within
  1361. * @param {number} a - The first number to check
  1362. * @param {number} b - The second number to check
  1363. * @param {number} tolerance - The tolerance. Anything equal to or less than this is considered within the range.
  1364. * @return {boolean} True if a is &lt;= tolerance of b.
  1365. */
  1366. within: function ( a, b, tolerance ) {
  1367. return (Math.abs(a - b) &lt;= tolerance);
  1368. },
  1369. /**
  1370. * Linear mapping from range &lt;a1, a2> to range &lt;b1, b2>
  1371. *
  1372. * @method Phaser.Math#mapLinear
  1373. * @param {number} x the value to map
  1374. * @param {number} a1 first endpoint of the range &lt;a1, a2>
  1375. * @param {number} a2 final endpoint of the range &lt;a1, a2>
  1376. * @param {number} b1 first endpoint of the range &lt;b1, b2>
  1377. * @param {number} b2 final endpoint of the range &lt;b1, b2>
  1378. * @return {number}
  1379. */
  1380. mapLinear: function ( x, a1, a2, b1, b2 ) {
  1381. return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
  1382. },
  1383. /**
  1384. * Smoothstep function as detailed at http://en.wikipedia.org/wiki/Smoothstep
  1385. *
  1386. * @method Phaser.Math#smoothstep
  1387. * @param {number} x
  1388. * @param {number} min
  1389. * @param {number} max
  1390. * @return {number}
  1391. */
  1392. smoothstep: function ( x, min, max ) {
  1393. if (x &lt;= min)
  1394. {
  1395. return 0;
  1396. }
  1397. if (x >= max)
  1398. {
  1399. return 1;
  1400. }
  1401. x = (x - min) / (max - min);
  1402. return x * x * (3 - 2 * x);
  1403. },
  1404. /**
  1405. * Smootherstep function as detailed at http://en.wikipedia.org/wiki/Smoothstep
  1406. *
  1407. * @method Phaser.Math#smootherstep
  1408. * @param {number} x
  1409. * @param {number} min
  1410. * @param {number} max
  1411. * @return {number}
  1412. */
  1413. smootherstep: function ( x, min, max ) {
  1414. if (x &lt;= min)
  1415. {
  1416. return 0;
  1417. }
  1418. if (x >= max)
  1419. {
  1420. return 1;
  1421. }
  1422. x = (x - min) / (max - min);
  1423. return x * x * x * (x * (x * 6 - 15) + 10);
  1424. },
  1425. /**
  1426. * A value representing the sign of the value.
  1427. * -1 for negative, +1 for positive, 0 if value is 0
  1428. *
  1429. * @method Phaser.Math#sign
  1430. * @param {number} x
  1431. * @return {number}
  1432. */
  1433. sign: function ( x ) {
  1434. return ( x &lt; 0 ) ? -1 : ( ( x > 0 ) ? 1 : 0 );
  1435. },
  1436. /**
  1437. * Convert degrees to radians.
  1438. *
  1439. * @method Phaser.Math#degToRad
  1440. * @return {function}
  1441. */
  1442. degToRad: (function() {
  1443. var degreeToRadiansFactor = Math.PI / 180;
  1444. return function ( degrees ) {
  1445. return degrees * degreeToRadiansFactor;
  1446. };
  1447. }()),
  1448. /**
  1449. * Convert degrees to radians.
  1450. *
  1451. * @method Phaser.Math#radToDeg
  1452. * @return {function}
  1453. */
  1454. radToDeg: (function() {
  1455. var radianToDegreesFactor = 180 / Math.PI;
  1456. return function ( radians ) {
  1457. return radians * radianToDegreesFactor;
  1458. };
  1459. }())
  1460. };
  1461. </pre>
  1462. </article>
  1463. </section>
  1464. </div>
  1465. <div class="clearfix"></div>
  1466. <footer>

Large files files are truncated, but you can click here to view the full file