math_Vector2.js.html 10 KB

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