math_Vector2.js.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: math/Vector2.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: math/Vector2.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>"use strict";
  20. /**
  21. * Class representing a 2D vector. A 2D vector is an ordered pair of numbers (labeled x and y), which can be used to represent points in space, directions, etc.
  22. *
  23. * @class
  24. */
  25. function Vector2(x, y)
  26. {
  27. this.x = x || 0;
  28. this.y = y || 0;
  29. }
  30. Vector2.prototype.set = function(x, y)
  31. {
  32. this.x = x;
  33. this.y = y;
  34. };
  35. Vector2.prototype.setScalar = function(scalar)
  36. {
  37. this.x = scalar;
  38. this.y = scalar;
  39. };
  40. Vector2.prototype.clone = function()
  41. {
  42. return new Vector2(this.x, this.y);
  43. };
  44. Vector2.prototype.copy = function(v)
  45. {
  46. this.x = v.x;
  47. this.y = v.y;
  48. };
  49. Vector2.prototype.add = function(v)
  50. {
  51. this.x += v.x;
  52. this.y += v.y;
  53. };
  54. Vector2.prototype.addScalar = function(s)
  55. {
  56. this.x += s;
  57. this.y += s;
  58. };
  59. Vector2.prototype.addVectors = function(a, b)
  60. {
  61. this.x = a.x + b.x;
  62. this.y = a.y + b.y;
  63. };
  64. Vector2.prototype.addScaledVector = function(v, s)
  65. {
  66. this.x += v.x * s;
  67. this.y += v.y * s;
  68. };
  69. Vector2.prototype.sub = function(v)
  70. {
  71. this.x -= v.x;
  72. this.y -= v.y;
  73. };
  74. Vector2.prototype.subScalar = function(s)
  75. {
  76. this.x -= s;
  77. this.y -= s;
  78. };
  79. Vector2.prototype.subVectors = function(a, b)
  80. {
  81. this.x = a.x - b.x;
  82. this.y = a.y - b.y;
  83. };
  84. Vector2.prototype.multiply = function(v)
  85. {
  86. this.x *= v.x;
  87. this.y *= v.y;
  88. };
  89. Vector2.prototype.multiplyScalar = function(scalar)
  90. {
  91. this.x *= scalar;
  92. this.y *= scalar;
  93. };
  94. Vector2.prototype.divide = function(v)
  95. {
  96. this.x /= v.x;
  97. this.y /= v.y;
  98. };
  99. Vector2.prototype.divideScalar = function(scalar)
  100. {
  101. return this.multiplyScalar(1 / scalar);
  102. };
  103. Vector2.prototype.min = function(v)
  104. {
  105. this.x = Math.min(this.x, v.x);
  106. this.y = Math.min(this.y, v.y);
  107. };
  108. Vector2.prototype.max = function(v)
  109. {
  110. this.x = Math.max(this.x, v.x);
  111. this.y = Math.max(this.y, v.y);
  112. };
  113. Vector2.prototype.clamp = function(min, max)
  114. {
  115. // assumes min &lt; max, componentwise
  116. this.x = Math.max(min.x, Math.min(max.x, this.x));
  117. this.y = Math.max(min.y, Math.min(max.y, this.y));
  118. };
  119. Vector2.prototype.clampScalar = function(minVal, maxVal)
  120. {
  121. this.x = Math.max(minVal, Math.min(maxVal, this.x));
  122. this.y = Math.max(minVal, Math.min(maxVal, this.y));
  123. };
  124. Vector2.prototype.clampLength = function(min, max)
  125. {
  126. var length = this.length();
  127. return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length)));
  128. };
  129. Vector2.prototype.floor = function()
  130. {
  131. this.x = Math.floor(this.x);
  132. this.y = Math.floor(this.y);
  133. };
  134. Vector2.prototype.ceil = function()
  135. {
  136. this.x = Math.ceil(this.x);
  137. this.y = Math.ceil(this.y);
  138. };
  139. Vector2.prototype.round = function()
  140. {
  141. this.x = Math.round(this.x);
  142. this.y = Math.round(this.y);
  143. };
  144. Vector2.prototype.roundToZero = function()
  145. {
  146. this.x = (this.x &lt; 0) ? Math.ceil(this.x) : Math.floor(this.x);
  147. this.y = (this.y &lt; 0) ? Math.ceil(this.y) : Math.floor(this.y);
  148. };
  149. Vector2.prototype.negate = function()
  150. {
  151. this.x = -this.x;
  152. this.y = -this.y;
  153. return this;
  154. };
  155. Vector2.prototype.dot = function(v)
  156. {
  157. return this.x * v.x + this.y * v.y;
  158. };
  159. Vector2.prototype.cross = function(v)
  160. {
  161. return this.x * v.y - this.y * v.x;
  162. };
  163. Vector2.prototype.lengthSq = function()
  164. {
  165. return this.x * this.x + this.y * this.y;
  166. };
  167. Vector2.prototype.length = function()
  168. {
  169. return Math.sqrt(this.x * this.x + this.y * this.y);
  170. };
  171. Vector2.prototype.manhattanLength = function()
  172. {
  173. return Math.abs(this.x) + Math.abs(this.y);
  174. };
  175. Vector2.prototype.normalize = function()
  176. {
  177. return this.divideScalar(this.length() || 1);
  178. };
  179. /**
  180. * Computes the angle in radians with respect to the positive x-axis
  181. */
  182. Vector2.prototype.angle = function()
  183. {
  184. var angle = Math.atan2(this.y, this.x);
  185. if(angle &lt; 0)
  186. {
  187. angle += 2 * Math.PI;
  188. }
  189. return angle;
  190. };
  191. Vector2.prototype.distanceTo = function(v)
  192. {
  193. return Math.sqrt(this.distanceToSquared(v));
  194. };
  195. Vector2.prototype.distanceToSquared = function(v)
  196. {
  197. var dx = this.x - v.x;
  198. var dy = this.y - v.y;
  199. return dx * dx + dy * dy;
  200. };
  201. Vector2.prototype.manhattanDistanceTo = function(v)
  202. {
  203. return Math.abs(this.x - v.x) + Math.abs(this.y - v.y);
  204. };
  205. Vector2.prototype.setLength = function(length)
  206. {
  207. return this.normalize().multiplyScalar(length);
  208. };
  209. Vector2.prototype.lerp = function(v, alpha)
  210. {
  211. this.x += (v.x - this.x) * alpha;
  212. this.y += (v.y - this.y) * alpha;
  213. };
  214. Vector2.prototype.lerpVectors = function(v1, v2, alpha)
  215. {
  216. return this.subVectors(v2, v1).multiplyScalar(alpha).add(v1);
  217. };
  218. Vector2.prototype.equals = function(v)
  219. {
  220. return ((v.x === this.x) &amp;&amp; (v.y === this.y));
  221. };
  222. Vector2.prototype.fromArray = function(array, offset)
  223. {
  224. if(offset === undefined) offset = 0;
  225. this.x = array[offset];
  226. this.y = array[offset + 1];
  227. };
  228. Vector2.prototype.toArray = function(array, offset)
  229. {
  230. if(array === undefined) array = [];
  231. if(offset === undefined) offset = 0;
  232. array[offset] = this.x;
  233. array[offset + 1] = this.y;
  234. return array;
  235. };
  236. Vector2.prototype.rotateAround = function(center, angle)
  237. {
  238. var c = Math.cos(angle);
  239. var s = Math.sin(angle);
  240. var x = this.x - center.x;
  241. var y = this.y - center.y;
  242. this.x = x * c - y * s + center.x;
  243. this.y = x * s + y * c + center.y;
  244. };
  245. export {Vector2};
  246. </code></pre>
  247. </article>
  248. </section>
  249. </div>
  250. <nav>
  251. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Box.html">Box</a></li><li><a href="Box2.html">Box2</a></li><li><a href="BoxMask.html">BoxMask</a></li><li><a href="Circle.html">Circle</a></li><li><a href="DOM.html">DOM</a></li><li><a href="EventManager.html">EventManager</a></li><li><a href="Helpers.html">Helpers</a></li><li><a href="Image.html">Image</a></li><li><a href="Key.html">Key</a></li><li><a href="Line.html">Line</a></li><li><a href="Mask.html">Mask</a></li><li><a href="Matrix.html">Matrix</a></li><li><a href="Object2D.html">Object2D</a></li><li><a href="Pattern.html">Pattern</a></li><li><a href="Pointer.html">Pointer</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="Text.html">Text</a></li><li><a href="UUID.html">UUID</a></li><li><a href="Vector2.html">Vector2</a></li><li><a href="Viewport.html">Viewport</a></li></ul>
  252. </nav>
  253. <br class="clear">
  254. <footer>
  255. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.2</a> on Thu Jun 06 2019 19:35:50 GMT+0100 (Western European Summer Time)
  256. </footer>
  257. <script> prettyPrint(); </script>
  258. <script src="scripts/linenumber.js"> </script>
  259. </body>
  260. </html>