2
0

math_Vector2.js.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 X value.
  24. * @param {number} y Y value.
  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 X value.
  35. * @param {number} y Y value.
  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. * @param {number} scalar Scalar value.
  46. */
  47. Vector2.prototype.setScalar = function(scalar)
  48. {
  49. this.x = scalar;
  50. this.y = scalar;
  51. };
  52. /**
  53. * Create a clone of this vector object.
  54. *
  55. * @return {Vector2} A new vector with the same values as this one.
  56. */
  57. Vector2.prototype.clone = function()
  58. {
  59. return new Vector2(this.x, this.y);
  60. };
  61. /**
  62. * Copy the content of another vector into this one.
  63. *
  64. * @param {Vector2} v
  65. */
  66. Vector2.prototype.copy = function(v)
  67. {
  68. this.x = v.x;
  69. this.y = v.y;
  70. };
  71. /**
  72. * Add the content of another vector to this one.
  73. *
  74. * @param {Vector2} v The other vector.
  75. */
  76. Vector2.prototype.add = function(v)
  77. {
  78. this.x += v.x;
  79. this.y += v.y;
  80. };
  81. /**
  82. * Add a scalar value to booth vector components.
  83. *
  84. * @param {number} s Scalar value.
  85. */
  86. Vector2.prototype.addScalar = function(s)
  87. {
  88. this.x += s;
  89. this.y += s;
  90. };
  91. /**
  92. * Add two vectors and store the result in this vector.
  93. *
  94. * @param {Vector2} a The first vector.
  95. * @param {Vector2} b The second vector.
  96. */
  97. Vector2.prototype.addVectors = function(a, b)
  98. {
  99. this.x = a.x + b.x;
  100. this.y = a.y + b.y;
  101. };
  102. /**
  103. * Scale a vector components and add the result to this vector.
  104. *
  105. * @param {Vector2} v The other vector.
  106. * @param {number} s Scalar value.
  107. */
  108. Vector2.prototype.addScaledVector = function(v, s)
  109. {
  110. this.x += v.x * s;
  111. this.y += v.y * s;
  112. };
  113. /**
  114. * Subtract the content of another vector to this one.
  115. *
  116. * @param {Vector2} v The other vector.
  117. */
  118. Vector2.prototype.sub = function(v)
  119. {
  120. this.x -= v.x;
  121. this.y -= v.y;
  122. };
  123. /**
  124. * Subtract a scalar value to booth vector components.
  125. *
  126. * @param {number} s Scalar value.
  127. */
  128. Vector2.prototype.subScalar = function(s)
  129. {
  130. this.x -= s;
  131. this.y -= s;
  132. };
  133. /**
  134. * Subtract two vectors and store the result in this vector.
  135. *
  136. * @param {Vector2} a The first vector.
  137. * @param {Vector2} b The second vector.
  138. */
  139. Vector2.prototype.subVectors = function(a, b)
  140. {
  141. this.x = a.x - b.x;
  142. this.y = a.y - b.y;
  143. };
  144. /**
  145. * Multiply the content of another vector to this one.
  146. *
  147. * @param {Vector2} v The other vector.
  148. */
  149. Vector2.prototype.multiply = function(v)
  150. {
  151. this.x *= v.x;
  152. this.y *= v.y;
  153. };
  154. /**
  155. * Multiply a scalar value by booth vector components.
  156. *
  157. * @param {number} scalar Scalar value.
  158. */
  159. Vector2.prototype.multiplyScalar = function(scalar)
  160. {
  161. this.x *= scalar;
  162. this.y *= scalar;
  163. };
  164. /**
  165. * Divide the content of another vector from this one.
  166. *
  167. * @param {Vector2} v
  168. */
  169. Vector2.prototype.divide = function(v)
  170. {
  171. this.x /= v.x;
  172. this.y /= v.y;
  173. };
  174. /**
  175. * Divide a scalar value by booth vector components.
  176. *
  177. * @param {number} s
  178. */
  179. Vector2.prototype.divideScalar = function(scalar)
  180. {
  181. return this.multiplyScalar(1 / scalar);
  182. };
  183. /**
  184. * Set the minimum of x and y coordinates between two vectors.
  185. *
  186. * X is set as the min between this vector and the other vector.
  187. *
  188. * @param {Vector2} v
  189. */
  190. Vector2.prototype.min = function(v)
  191. {
  192. this.x = Math.min(this.x, v.x);
  193. this.y = Math.min(this.y, v.y);
  194. };
  195. /**
  196. * Set the maximum of x and y coordinates between two vectors.
  197. *
  198. * X is set as the max between this vector and the other vector.
  199. *
  200. * @param {Vector2} v
  201. */
  202. Vector2.prototype.max = function(v)
  203. {
  204. this.x = Math.max(this.x, v.x);
  205. this.y = Math.max(this.y, v.y);
  206. };
  207. /**
  208. * Clamp the vector coordinates to the range defined by two vectors.
  209. *
  210. * Applied to x and y independently.
  211. *
  212. * @param {Vector2} min Minimum value.
  213. * @param {Vector2} max Maximum value.
  214. */
  215. Vector2.prototype.clamp = function(min, max)
  216. {
  217. // assumes min &lt; max, componentwise
  218. this.x = Math.max(min.x, Math.min(max.x, this.x));
  219. this.y = Math.max(min.y, Math.min(max.y, this.y));
  220. };
  221. /**
  222. * Clamp the vector coordinates to the range defined by two scalars.
  223. *
  224. * @param {number} minVal Minimum value.
  225. * @param {number} maxVal Maximum value.
  226. */
  227. Vector2.prototype.clampScalar = function(minVal, maxVal)
  228. {
  229. this.x = Math.max(minVal, Math.min(maxVal, this.x));
  230. this.y = Math.max(minVal, Math.min(maxVal, this.y));
  231. };
  232. Vector2.prototype.clampLength = function(min, max)
  233. {
  234. var length = this.length();
  235. return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length)));
  236. };
  237. /**
  238. * Round the vector coordinates to integer by flooring to the smaller integer.
  239. */
  240. Vector2.prototype.floor = function()
  241. {
  242. this.x = Math.floor(this.x);
  243. this.y = Math.floor(this.y);
  244. };
  245. /**
  246. * Round the vector coordinates to integer by ceiling to the bigger integer.
  247. */
  248. Vector2.prototype.ceil = function()
  249. {
  250. this.x = Math.ceil(this.x);
  251. this.y = Math.ceil(this.y);
  252. };
  253. /**
  254. * Round the vector coordinates to their closest integer.
  255. */
  256. Vector2.prototype.round = function()
  257. {
  258. this.x = Math.round(this.x);
  259. this.y = Math.round(this.y);
  260. };
  261. /**
  262. * Negate the coordinates of this vector.
  263. */
  264. Vector2.prototype.negate = function()
  265. {
  266. this.x = -this.x;
  267. this.y = -this.y;
  268. return this;
  269. };
  270. /**
  271. * Dot multiplication between this vector and another vector.
  272. *
  273. * @param {Vector2} vector
  274. * @return {number} Result of the dot multiplication.
  275. */
  276. Vector2.prototype.dot = function(v)
  277. {
  278. return this.x * v.x + this.y * v.y;
  279. };
  280. /**
  281. * Cross multiplication between this vector and another vector.
  282. *
  283. * @param {Vector2} vector
  284. * @return {number} Result of the cross multiplication.
  285. */
  286. Vector2.prototype.cross = function(v)
  287. {
  288. return this.x * v.y - this.y * v.x;
  289. };
  290. /**
  291. * Squared length of the vector.
  292. *
  293. * Faster for comparions.
  294. *
  295. * @return {number} Squared length of the vector.
  296. */
  297. Vector2.prototype.lengthSq = function()
  298. {
  299. return this.x * this.x + this.y * this.y;
  300. };
  301. /**
  302. * Length of the vector.
  303. *
  304. * @return {number} Length of the vector.
  305. */
  306. Vector2.prototype.length = function()
  307. {
  308. return Math.sqrt(this.x * this.x + this.y * this.y);
  309. };
  310. /**
  311. * Manhattan length of the vector.
  312. *
  313. * @return {number} Manhattan length of the vector.
  314. */
  315. Vector2.prototype.manhattanLength = function()
  316. {
  317. return Math.abs(this.x) + Math.abs(this.y);
  318. };
  319. /**
  320. * Normalize the vector (make it length one).
  321. *
  322. * @return {Vector2} This vector.
  323. */
  324. Vector2.prototype.normalize = function()
  325. {
  326. return this.divideScalar(this.length() || 1);
  327. };
  328. /**
  329. * Computes the angle in radians with respect to the positive x-axis.
  330. *
  331. * @param {boolean} forcePositive If true, the angle will be forced to be positive.
  332. * @return {number} Angle in radians.
  333. */
  334. Vector2.prototype.angle = function(forcePositive)
  335. {
  336. var angle = Math.atan2(this.y, this.x);
  337. if(forcePositive &amp;&amp; angle &lt; 0)
  338. {
  339. angle += 2 * Math.PI;
  340. }
  341. return angle;
  342. };
  343. /**
  344. * Distance between two vector positions.
  345. *
  346. * @param {Vector2} v Vector to compute the distance to.
  347. * @return {number} Distance between the two vectors.
  348. */
  349. Vector2.prototype.distanceTo = function(v)
  350. {
  351. return Math.sqrt(this.distanceToSquared(v));
  352. };
  353. /**
  354. * Distance between two vector positions squared.
  355. *
  356. * Faster for comparisons.
  357. *
  358. * @param {Vector2} v Vector to compute the distance to.
  359. * @return {number} Distance between the two vectors squared.
  360. */
  361. Vector2.prototype.distanceToSquared = function(v)
  362. {
  363. var dx = this.x - v.x;
  364. var dy = this.y - v.y;
  365. return dx * dx + dy * dy;
  366. };
  367. /**
  368. * Manhattan distance between two vector positions.
  369. *
  370. * @param {Vector2} v Vector to compute the distance to.
  371. * @return {number} Manhattan distance between the two vectors.
  372. */
  373. Vector2.prototype.manhattanDistanceTo = function(v)
  374. {
  375. return Math.abs(this.x - v.x) + Math.abs(this.y - v.y);
  376. };
  377. /**
  378. * Scale the vector to have a defined length value.
  379. *
  380. * @param {number} length Length to scale the vector to.
  381. * @return {Vector2} This vector.
  382. */
  383. Vector2.prototype.setLength = function(length)
  384. {
  385. return this.normalize().multiplyScalar(length);
  386. };
  387. /**
  388. * Lerp this vector to another vector.
  389. *
  390. * @param {Vector2} v Vector to lerp to.
  391. * @param {number} alpha Lerp factor.
  392. */
  393. Vector2.prototype.lerp = function(v, alpha)
  394. {
  395. this.x += (v.x - this.x) * alpha;
  396. this.y += (v.y - this.y) * alpha;
  397. };
  398. /**
  399. * Lerp between this vector and another vector.
  400. *
  401. * @param {Vector2} v1 Vector to lerp from.
  402. * @param {Vector2} v2 Vector to lerp to.
  403. * @param {number} alpha Lerp factor.
  404. * @return {Vector2} This vector.
  405. *
  406. Vector2.prototype.lerpVectors = function(v1, v2, alpha)
  407. {
  408. return this.subVectors(v2, v1).multiplyScalar(alpha).add(v1);
  409. };
  410. /**
  411. * Check if two vectors are equal.
  412. *
  413. * @param {Vector2} v Vector to compare with.
  414. */
  415. Vector2.prototype.equals = function(v)
  416. {
  417. return ((v.x === this.x) &amp;&amp; (v.y === this.y));
  418. };
  419. /**
  420. * Set vector value from array [x, y].
  421. *
  422. * The vector can be converted to array using the toArray() method.
  423. *
  424. * @param {number[]} array Array to set the vector value from.
  425. */
  426. Vector2.prototype.fromArray = function(array)
  427. {
  428. this.set(array[0], array[1]);
  429. };
  430. /**
  431. * Convert this vector to an array. Useful for serialization and storage.
  432. *
  433. * Values stored as [x, y].
  434. *
  435. * @return {number[]} Array containing the values of the vector.
  436. */
  437. Vector2.prototype.toArray = function()
  438. {
  439. return [this.x, this.y];
  440. };
  441. /**
  442. * Rotate the vector around a central point.
  443. *
  444. * @param {Vector2} center Point to rotate around.
  445. * @param {number} angle Angle in radians.
  446. */
  447. Vector2.prototype.rotateAround = function(center, angle)
  448. {
  449. var c = Math.cos(angle);
  450. var s = Math.sin(angle);
  451. var x = this.x - center.x;
  452. var y = this.y - center.y;
  453. this.x = x * c - y * s + center.x;
  454. this.y = x * s + y * c + center.y;
  455. };
  456. export {Vector2};
  457. </code></pre>
  458. </article>
  459. </section>
  460. </div>
  461. <nav>
  462. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AnimationTimer.html">AnimationTimer</a></li><li><a href="BarGraph.html">BarGraph</a></li><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="ColorStyle.html">ColorStyle</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="Gauge.html">Gauge</a></li><li><a href="GradientColorStop.html">GradientColorStop</a></li><li><a href="GradientStyle.html">GradientStyle</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="LinearGradientStyle.html">LinearGradientStyle</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="Path.html">Path</a></li><li><a href="Pattern.html">Pattern</a></li><li><a href="PatternStyle.html">PatternStyle</a></li><li><a href="PieChart.html">PieChart</a></li><li><a href="Pointer.html">Pointer</a></li><li><a href="QuadraticCurve.html">QuadraticCurve</a></li><li><a href="RadialGradientStyle.html">RadialGradientStyle</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="RoundedBox.html">RoundedBox</a></li><li><a href="ScatterGraph.html">ScatterGraph</a></li><li><a href="Style.html">Style</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>
  463. </nav>
  464. <br class="clear">
  465. <footer>
  466. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Sat Sep 17 2022 14:24:36 GMT+0100 (Hora de verão da Europa Ocidental)
  467. </footer>
  468. <script> prettyPrint(); </script>
  469. <script src="scripts/linenumber.js"> </script>
  470. </body>
  471. </html>