math_Vector2.js.html 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. /**
  221. * Round the vector coordinates to integer by flooring to the smaller integer.
  222. */
  223. Vector2.prototype.floor = function()
  224. {
  225. this.x = Math.floor(this.x);
  226. this.y = Math.floor(this.y);
  227. };
  228. /**
  229. * Round the vector coordinates to integer by ceiling to the bigger integer.
  230. */
  231. Vector2.prototype.ceil = function()
  232. {
  233. this.x = Math.ceil(this.x);
  234. this.y = Math.ceil(this.y);
  235. };
  236. /**
  237. * Round the vector coordinates to their closest integer.
  238. */
  239. Vector2.prototype.round = function()
  240. {
  241. this.x = Math.round(this.x);
  242. this.y = Math.round(this.y);
  243. };
  244. /**
  245. * Negate the coordinates of this vector.
  246. */
  247. Vector2.prototype.negate = function()
  248. {
  249. this.x = -this.x;
  250. this.y = -this.y;
  251. return this;
  252. };
  253. /**
  254. * Dot multiplication between this vector and another vector.
  255. *
  256. * @param {Vector2} vector
  257. * @return {number} Result of the dot multiplication.
  258. */
  259. Vector2.prototype.dot = function(v)
  260. {
  261. return this.x * v.x + this.y * v.y;
  262. };
  263. /**
  264. * Cross multiplication between this vector and another vector.
  265. *
  266. * @param {Vector2} vector
  267. * @return {number} Result of the cross multiplication.
  268. */
  269. Vector2.prototype.cross = function(v)
  270. {
  271. return this.x * v.y - this.y * v.x;
  272. };
  273. /**
  274. * Squared length of the vector.
  275. *
  276. * Faster for comparions.
  277. */
  278. Vector2.prototype.lengthSq = function()
  279. {
  280. return this.x * this.x + this.y * this.y;
  281. };
  282. /**
  283. * Length of the vector.
  284. */
  285. Vector2.prototype.length = function()
  286. {
  287. return Math.sqrt(this.x * this.x + this.y * this.y);
  288. };
  289. /**
  290. * Manhattan length of the vector.
  291. */
  292. Vector2.prototype.manhattanLength = function()
  293. {
  294. return Math.abs(this.x) + Math.abs(this.y);
  295. };
  296. /**
  297. * Normalize the vector (make it length one).
  298. */
  299. Vector2.prototype.normalize = function()
  300. {
  301. return this.divideScalar(this.length() || 1);
  302. };
  303. /**
  304. * Computes the angle in radians with respect to the positive x-axis
  305. */
  306. Vector2.prototype.angle = function()
  307. {
  308. var angle = Math.atan2(this.y, this.x);
  309. if(angle &lt; 0)
  310. {
  311. angle += 2 * Math.PI;
  312. }
  313. return angle;
  314. };
  315. /**
  316. * Distance between two vector positions.
  317. */
  318. Vector2.prototype.distanceTo = function(v)
  319. {
  320. return Math.sqrt(this.distanceToSquared(v));
  321. };
  322. /**
  323. * Distance between two vector positions squared.
  324. *
  325. * Faster for comparisons.
  326. */
  327. Vector2.prototype.distanceToSquared = function(v)
  328. {
  329. var dx = this.x - v.x;
  330. var dy = this.y - v.y;
  331. return dx * dx + dy * dy;
  332. };
  333. /**
  334. * Manhattan distance between two vector positions.
  335. */
  336. Vector2.prototype.manhattanDistanceTo = function(v)
  337. {
  338. return Math.abs(this.x - v.x) + Math.abs(this.y - v.y);
  339. };
  340. /**
  341. * Scale the vector to have a defined length value.
  342. */
  343. Vector2.prototype.setLength = function(length)
  344. {
  345. return this.normalize().multiplyScalar(length);
  346. };
  347. Vector2.prototype.lerp = function(v, alpha)
  348. {
  349. this.x += (v.x - this.x) * alpha;
  350. this.y += (v.y - this.y) * alpha;
  351. };
  352. Vector2.prototype.lerpVectors = function(v1, v2, alpha)
  353. {
  354. return this.subVectors(v2, v1).multiplyScalar(alpha).add(v1);
  355. };
  356. /**
  357. * Check if two vectors are equal.
  358. *
  359. * @param {Vector2} v
  360. */
  361. Vector2.prototype.equals = function(v)
  362. {
  363. return ((v.x === this.x) &amp;&amp; (v.y === this.y));
  364. };
  365. /**
  366. * Set vector value from array with a offset.
  367. *
  368. * @param {array} array
  369. * @param {number} [offset]
  370. */
  371. Vector2.prototype.fromArray = function(array, offset)
  372. {
  373. if(offset === undefined) offset = 0;
  374. this.x = array[offset];
  375. this.y = array[offset + 1];
  376. };
  377. /**
  378. * Convert this vector to an array.
  379. *
  380. * @param {array} array
  381. * @param {number} [offset]
  382. */
  383. Vector2.prototype.toArray = function(array, offset)
  384. {
  385. if(array === undefined) array = [];
  386. if(offset === undefined) offset = 0;
  387. array[offset] = this.x;
  388. array[offset + 1] = this.y;
  389. return array;
  390. };
  391. /**
  392. * Rotate the vector around a central point.
  393. *
  394. * @param {Vector2} center
  395. * @param {number} angle
  396. */
  397. Vector2.prototype.rotateAround = function(center, angle)
  398. {
  399. var c = Math.cos(angle);
  400. var s = Math.sin(angle);
  401. var x = this.x - center.x;
  402. var y = this.y - center.y;
  403. this.x = x * c - y * s + center.x;
  404. this.y = x * s + y * c + center.y;
  405. };
  406. export {Vector2};
  407. </code></pre>
  408. </article>
  409. </section>
  410. </div>
  411. <nav>
  412. <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><li><a href="ViewportControls.html">ViewportControls</a></li></ul>
  413. </nav>
  414. <br class="clear">
  415. <footer>
  416. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.2</a> on Thu Jun 13 2019 11:55:28 GMT+0100 (Western European Summer Time)
  417. </footer>
  418. <script> prettyPrint(); </script>
  419. <script src="scripts/linenumber.js"> </script>
  420. </body>
  421. </html>