math_Vector2.js.html 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. * @param {number} x
  25. * @param {number} y
  26. */
  27. function Vector2(x, y)
  28. {
  29. this.x = x || 0;
  30. this.y = y || 0;
  31. }
  32. /**
  33. * Set vector x and y values.
  34. *
  35. * @param {number} x
  36. * @param {number} y
  37. */
  38. Vector2.prototype.set = function(x, y)
  39. {
  40. this.x = x;
  41. this.y = y;
  42. };
  43. /**
  44. * Set a scalar value into the x and y values.
  45. */
  46. Vector2.prototype.setScalar = function(scalar)
  47. {
  48. this.x = scalar;
  49. this.y = scalar;
  50. };
  51. /**
  52. * Create a clone of this vector object.
  53. */
  54. Vector2.prototype.clone = function()
  55. {
  56. return new Vector2(this.x, this.y);
  57. };
  58. /**
  59. * Copy the content of another vector into this one.
  60. *
  61. * @param {Vector2} v
  62. */
  63. Vector2.prototype.copy = function(v)
  64. {
  65. this.x = v.x;
  66. this.y = v.y;
  67. };
  68. /**
  69. * Add the content of another vector to this one.
  70. *
  71. * @param {Vector2} v
  72. */
  73. Vector2.prototype.add = function(v)
  74. {
  75. this.x += v.x;
  76. this.y += v.y;
  77. };
  78. /**
  79. * Add a scalar value to booth vector components.
  80. *
  81. * @param {number} s
  82. */
  83. Vector2.prototype.addScalar = function(s)
  84. {
  85. this.x += s;
  86. this.y += s;
  87. };
  88. /**
  89. * Add two vectors and store the result in this vector.
  90. *
  91. * @param {Vector2} a
  92. * @param {Vector2} b
  93. */
  94. Vector2.prototype.addVectors = function(a, b)
  95. {
  96. this.x = a.x + b.x;
  97. this.y = a.y + b.y;
  98. };
  99. /**
  100. * Scale a vector components and add the result to this vector.
  101. *
  102. * @param {Vector2} v
  103. * @param {number} s
  104. */
  105. Vector2.prototype.addScaledVector = function(v, s)
  106. {
  107. this.x += v.x * s;
  108. this.y += v.y * s;
  109. };
  110. /**
  111. * Subtract the content of another vector to this one.
  112. *
  113. * @param {Vector2} v
  114. */
  115. Vector2.prototype.sub = function(v)
  116. {
  117. this.x -= v.x;
  118. this.y -= v.y;
  119. };
  120. /**
  121. * Subtract a scalar value to booth vector components.
  122. *
  123. * @param {number} s
  124. */
  125. Vector2.prototype.subScalar = function(s)
  126. {
  127. this.x -= s;
  128. this.y -= s;
  129. };
  130. /**
  131. * Subtract two vectors and store the result in this vector.
  132. *
  133. * @param {Vector2} a
  134. * @param {Vector2} b
  135. */
  136. Vector2.prototype.subVectors = function(a, b)
  137. {
  138. this.x = a.x - b.x;
  139. this.y = a.y - b.y;
  140. };
  141. /**
  142. * Multiply the content of another vector to this one.
  143. *
  144. * @param {Vector2} v
  145. */
  146. Vector2.prototype.multiply = function(v)
  147. {
  148. this.x *= v.x;
  149. this.y *= v.y;
  150. };
  151. /**
  152. * Multiply a scalar value by booth vector components.
  153. *
  154. * @param {number} s
  155. */
  156. Vector2.prototype.multiplyScalar = function(scalar)
  157. {
  158. this.x *= scalar;
  159. this.y *= scalar;
  160. };
  161. /**
  162. * Divide the content of another vector from this one.
  163. *
  164. * @param {Vector2} v
  165. */
  166. Vector2.prototype.divide = function(v)
  167. {
  168. this.x /= v.x;
  169. this.y /= v.y;
  170. };
  171. /**
  172. * Divide a scalar value by booth vector components.
  173. *
  174. * @param {number} s
  175. */
  176. Vector2.prototype.divideScalar = function(scalar)
  177. {
  178. return this.multiplyScalar(1 / scalar);
  179. };
  180. /**
  181. * Set the minimum of x and y coordinates between two vectors.
  182. *
  183. * X is set as the min between this vector and the other vector.
  184. *
  185. * @param {Vector2} v
  186. */
  187. Vector2.prototype.min = function(v)
  188. {
  189. this.x = Math.min(this.x, v.x);
  190. this.y = Math.min(this.y, v.y);
  191. };
  192. /**
  193. * Set the maximum of x and y coordinates between two vectors.
  194. *
  195. * X is set as the max between this vector and the other vector.
  196. *
  197. * @param {Vector2} v
  198. */
  199. Vector2.prototype.max = function(v)
  200. {
  201. this.x = Math.max(this.x, v.x);
  202. this.y = Math.max(this.y, v.y);
  203. };
  204. Vector2.prototype.clamp = function(min, max)
  205. {
  206. // assumes min &lt; max, componentwise
  207. this.x = Math.max(min.x, Math.min(max.x, this.x));
  208. this.y = Math.max(min.y, Math.min(max.y, this.y));
  209. };
  210. Vector2.prototype.clampScalar = function(minVal, maxVal)
  211. {
  212. this.x = Math.max(minVal, Math.min(maxVal, this.x));
  213. this.y = Math.max(minVal, Math.min(maxVal, this.y));
  214. };
  215. Vector2.prototype.clampLength = function(min, max)
  216. {
  217. var length = this.length();
  218. return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length)));
  219. };
  220. Vector2.prototype.floor = function()
  221. {
  222. this.x = Math.floor(this.x);
  223. this.y = Math.floor(this.y);
  224. };
  225. Vector2.prototype.ceil = function()
  226. {
  227. this.x = Math.ceil(this.x);
  228. this.y = Math.ceil(this.y);
  229. };
  230. Vector2.prototype.round = function()
  231. {
  232. this.x = Math.round(this.x);
  233. this.y = Math.round(this.y);
  234. };
  235. Vector2.prototype.roundToZero = function()
  236. {
  237. this.x = (this.x &lt; 0) ? Math.ceil(this.x) : Math.floor(this.x);
  238. this.y = (this.y &lt; 0) ? Math.ceil(this.y) : Math.floor(this.y);
  239. };
  240. Vector2.prototype.negate = function()
  241. {
  242. this.x = -this.x;
  243. this.y = -this.y;
  244. return this;
  245. };
  246. Vector2.prototype.dot = function(v)
  247. {
  248. return this.x * v.x + this.y * v.y;
  249. };
  250. Vector2.prototype.cross = function(v)
  251. {
  252. return this.x * v.y - this.y * v.x;
  253. };
  254. Vector2.prototype.lengthSq = function()
  255. {
  256. return this.x * this.x + this.y * this.y;
  257. };
  258. Vector2.prototype.length = function()
  259. {
  260. return Math.sqrt(this.x * this.x + this.y * this.y);
  261. };
  262. Vector2.prototype.manhattanLength = function()
  263. {
  264. return Math.abs(this.x) + Math.abs(this.y);
  265. };
  266. Vector2.prototype.normalize = function()
  267. {
  268. return this.divideScalar(this.length() || 1);
  269. };
  270. /**
  271. * Computes the angle in radians with respect to the positive x-axis
  272. */
  273. Vector2.prototype.angle = function()
  274. {
  275. var angle = Math.atan2(this.y, this.x);
  276. if(angle &lt; 0)
  277. {
  278. angle += 2 * Math.PI;
  279. }
  280. return angle;
  281. };
  282. Vector2.prototype.distanceTo = function(v)
  283. {
  284. return Math.sqrt(this.distanceToSquared(v));
  285. };
  286. Vector2.prototype.distanceToSquared = function(v)
  287. {
  288. var dx = this.x - v.x;
  289. var dy = this.y - v.y;
  290. return dx * dx + dy * dy;
  291. };
  292. Vector2.prototype.manhattanDistanceTo = function(v)
  293. {
  294. return Math.abs(this.x - v.x) + Math.abs(this.y - v.y);
  295. };
  296. Vector2.prototype.setLength = function(length)
  297. {
  298. return this.normalize().multiplyScalar(length);
  299. };
  300. Vector2.prototype.lerp = function(v, alpha)
  301. {
  302. this.x += (v.x - this.x) * alpha;
  303. this.y += (v.y - this.y) * alpha;
  304. };
  305. Vector2.prototype.lerpVectors = function(v1, v2, alpha)
  306. {
  307. return this.subVectors(v2, v1).multiplyScalar(alpha).add(v1);
  308. };
  309. /**
  310. * Check if two vectors are equal.
  311. *
  312. * @param {Vector2} v
  313. */
  314. Vector2.prototype.equals = function(v)
  315. {
  316. return ((v.x === this.x) &amp;&amp; (v.y === this.y));
  317. };
  318. /**
  319. * Set vector value from array with a offset.
  320. *
  321. * @param {array} array
  322. * @param {number} [offset]
  323. */
  324. Vector2.prototype.fromArray = function(array, offset)
  325. {
  326. if(offset === undefined) offset = 0;
  327. this.x = array[offset];
  328. this.y = array[offset + 1];
  329. };
  330. /**
  331. * Convert this vector to an array.
  332. *
  333. * @param {array} array
  334. * @param {number} [offset]
  335. */
  336. Vector2.prototype.toArray = function(array, offset)
  337. {
  338. if(array === undefined) array = [];
  339. if(offset === undefined) offset = 0;
  340. array[offset] = this.x;
  341. array[offset + 1] = this.y;
  342. return array;
  343. };
  344. /**
  345. * Rotate the vector around a central point.
  346. *
  347. * @param {Vector2} center
  348. * @param {number} angle
  349. */
  350. Vector2.prototype.rotateAround = function(center, angle)
  351. {
  352. var c = Math.cos(angle);
  353. var s = Math.sin(angle);
  354. var x = this.x - center.x;
  355. var y = this.y - center.y;
  356. this.x = x * c - y * s + center.x;
  357. this.y = x * s + y * c + center.y;
  358. };
  359. export {Vector2};
  360. </code></pre>
  361. </article>
  362. </section>
  363. </div>
  364. <nav>
  365. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BezierCurve.html">BezierCurve</a></li><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="Graph.html">Graph</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>
  366. </nav>
  367. <br class="clear">
  368. <footer>
  369. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.2</a> on Tue Jun 11 2019 14:45:44 GMT+0100 (Western European Summer Time)
  370. </footer>
  371. <script> prettyPrint(); </script>
  372. <script src="scripts/linenumber.js"> </script>
  373. </body>
  374. </html>