vec2.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. "use strict";
  2. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.create = create;
  7. exports.clone = clone;
  8. exports.fromValues = fromValues;
  9. exports.copy = copy;
  10. exports.set = set;
  11. exports.add = add;
  12. exports.subtract = subtract;
  13. exports.multiply = multiply;
  14. exports.divide = divide;
  15. exports.ceil = ceil;
  16. exports.floor = floor;
  17. exports.min = min;
  18. exports.max = max;
  19. exports.round = round;
  20. exports.scale = scale;
  21. exports.scaleAndAdd = scaleAndAdd;
  22. exports.distance = distance;
  23. exports.squaredDistance = squaredDistance;
  24. exports.length = length;
  25. exports.squaredLength = squaredLength;
  26. exports.negate = negate;
  27. exports.inverse = inverse;
  28. exports.normalize = normalize;
  29. exports.dot = dot;
  30. exports.cross = cross;
  31. exports.lerp = lerp;
  32. exports.random = random;
  33. exports.transformMat2 = transformMat2;
  34. exports.transformMat2d = transformMat2d;
  35. exports.transformMat3 = transformMat3;
  36. exports.transformMat4 = transformMat4;
  37. exports.rotate = rotate;
  38. exports.angle = angle;
  39. exports.zero = zero;
  40. exports.str = str;
  41. exports.exactEquals = exactEquals;
  42. exports.equals = equals;
  43. exports.forEach = exports.sqrLen = exports.sqrDist = exports.dist = exports.div = exports.mul = exports.sub = exports.len = void 0;
  44. var glMatrix = _interopRequireWildcard(require("./common.js"));
  45. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  46. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  47. /**
  48. * 2 Dimensional Vector
  49. * @module vec2
  50. */
  51. /**
  52. * Creates a new, empty vec2
  53. *
  54. * @returns {vec2} a new 2D vector
  55. */
  56. function create() {
  57. var out = new glMatrix.ARRAY_TYPE(2);
  58. if (glMatrix.ARRAY_TYPE != Float32Array) {
  59. out[0] = 0;
  60. out[1] = 0;
  61. }
  62. return out;
  63. }
  64. /**
  65. * Creates a new vec2 initialized with values from an existing vector
  66. *
  67. * @param {ReadonlyVec2} a vector to clone
  68. * @returns {vec2} a new 2D vector
  69. */
  70. function clone(a) {
  71. var out = new glMatrix.ARRAY_TYPE(2);
  72. out[0] = a[0];
  73. out[1] = a[1];
  74. return out;
  75. }
  76. /**
  77. * Creates a new vec2 initialized with the given values
  78. *
  79. * @param {Number} x X component
  80. * @param {Number} y Y component
  81. * @returns {vec2} a new 2D vector
  82. */
  83. function fromValues(x, y) {
  84. var out = new glMatrix.ARRAY_TYPE(2);
  85. out[0] = x;
  86. out[1] = y;
  87. return out;
  88. }
  89. /**
  90. * Copy the values from one vec2 to another
  91. *
  92. * @param {vec2} out the receiving vector
  93. * @param {ReadonlyVec2} a the source vector
  94. * @returns {vec2} out
  95. */
  96. function copy(out, a) {
  97. out[0] = a[0];
  98. out[1] = a[1];
  99. return out;
  100. }
  101. /**
  102. * Set the components of a vec2 to the given values
  103. *
  104. * @param {vec2} out the receiving vector
  105. * @param {Number} x X component
  106. * @param {Number} y Y component
  107. * @returns {vec2} out
  108. */
  109. function set(out, x, y) {
  110. out[0] = x;
  111. out[1] = y;
  112. return out;
  113. }
  114. /**
  115. * Adds two vec2's
  116. *
  117. * @param {vec2} out the receiving vector
  118. * @param {ReadonlyVec2} a the first operand
  119. * @param {ReadonlyVec2} b the second operand
  120. * @returns {vec2} out
  121. */
  122. function add(out, a, b) {
  123. out[0] = a[0] + b[0];
  124. out[1] = a[1] + b[1];
  125. return out;
  126. }
  127. /**
  128. * Subtracts vector b from vector a
  129. *
  130. * @param {vec2} out the receiving vector
  131. * @param {ReadonlyVec2} a the first operand
  132. * @param {ReadonlyVec2} b the second operand
  133. * @returns {vec2} out
  134. */
  135. function subtract(out, a, b) {
  136. out[0] = a[0] - b[0];
  137. out[1] = a[1] - b[1];
  138. return out;
  139. }
  140. /**
  141. * Multiplies two vec2's
  142. *
  143. * @param {vec2} out the receiving vector
  144. * @param {ReadonlyVec2} a the first operand
  145. * @param {ReadonlyVec2} b the second operand
  146. * @returns {vec2} out
  147. */
  148. function multiply(out, a, b) {
  149. out[0] = a[0] * b[0];
  150. out[1] = a[1] * b[1];
  151. return out;
  152. }
  153. /**
  154. * Divides two vec2's
  155. *
  156. * @param {vec2} out the receiving vector
  157. * @param {ReadonlyVec2} a the first operand
  158. * @param {ReadonlyVec2} b the second operand
  159. * @returns {vec2} out
  160. */
  161. function divide(out, a, b) {
  162. out[0] = a[0] / b[0];
  163. out[1] = a[1] / b[1];
  164. return out;
  165. }
  166. /**
  167. * Math.ceil the components of a vec2
  168. *
  169. * @param {vec2} out the receiving vector
  170. * @param {ReadonlyVec2} a vector to ceil
  171. * @returns {vec2} out
  172. */
  173. function ceil(out, a) {
  174. out[0] = Math.ceil(a[0]);
  175. out[1] = Math.ceil(a[1]);
  176. return out;
  177. }
  178. /**
  179. * Math.floor the components of a vec2
  180. *
  181. * @param {vec2} out the receiving vector
  182. * @param {ReadonlyVec2} a vector to floor
  183. * @returns {vec2} out
  184. */
  185. function floor(out, a) {
  186. out[0] = Math.floor(a[0]);
  187. out[1] = Math.floor(a[1]);
  188. return out;
  189. }
  190. /**
  191. * Returns the minimum of two vec2's
  192. *
  193. * @param {vec2} out the receiving vector
  194. * @param {ReadonlyVec2} a the first operand
  195. * @param {ReadonlyVec2} b the second operand
  196. * @returns {vec2} out
  197. */
  198. function min(out, a, b) {
  199. out[0] = Math.min(a[0], b[0]);
  200. out[1] = Math.min(a[1], b[1]);
  201. return out;
  202. }
  203. /**
  204. * Returns the maximum of two vec2's
  205. *
  206. * @param {vec2} out the receiving vector
  207. * @param {ReadonlyVec2} a the first operand
  208. * @param {ReadonlyVec2} b the second operand
  209. * @returns {vec2} out
  210. */
  211. function max(out, a, b) {
  212. out[0] = Math.max(a[0], b[0]);
  213. out[1] = Math.max(a[1], b[1]);
  214. return out;
  215. }
  216. /**
  217. * Math.round the components of a vec2
  218. *
  219. * @param {vec2} out the receiving vector
  220. * @param {ReadonlyVec2} a vector to round
  221. * @returns {vec2} out
  222. */
  223. function round(out, a) {
  224. out[0] = Math.round(a[0]);
  225. out[1] = Math.round(a[1]);
  226. return out;
  227. }
  228. /**
  229. * Scales a vec2 by a scalar number
  230. *
  231. * @param {vec2} out the receiving vector
  232. * @param {ReadonlyVec2} a the vector to scale
  233. * @param {Number} b amount to scale the vector by
  234. * @returns {vec2} out
  235. */
  236. function scale(out, a, b) {
  237. out[0] = a[0] * b;
  238. out[1] = a[1] * b;
  239. return out;
  240. }
  241. /**
  242. * Adds two vec2's after scaling the second operand by a scalar value
  243. *
  244. * @param {vec2} out the receiving vector
  245. * @param {ReadonlyVec2} a the first operand
  246. * @param {ReadonlyVec2} b the second operand
  247. * @param {Number} scale the amount to scale b by before adding
  248. * @returns {vec2} out
  249. */
  250. function scaleAndAdd(out, a, b, scale) {
  251. out[0] = a[0] + b[0] * scale;
  252. out[1] = a[1] + b[1] * scale;
  253. return out;
  254. }
  255. /**
  256. * Calculates the euclidian distance between two vec2's
  257. *
  258. * @param {ReadonlyVec2} a the first operand
  259. * @param {ReadonlyVec2} b the second operand
  260. * @returns {Number} distance between a and b
  261. */
  262. function distance(a, b) {
  263. var x = b[0] - a[0],
  264. y = b[1] - a[1];
  265. return Math.hypot(x, y);
  266. }
  267. /**
  268. * Calculates the squared euclidian distance between two vec2's
  269. *
  270. * @param {ReadonlyVec2} a the first operand
  271. * @param {ReadonlyVec2} b the second operand
  272. * @returns {Number} squared distance between a and b
  273. */
  274. function squaredDistance(a, b) {
  275. var x = b[0] - a[0],
  276. y = b[1] - a[1];
  277. return x * x + y * y;
  278. }
  279. /**
  280. * Calculates the length of a vec2
  281. *
  282. * @param {ReadonlyVec2} a vector to calculate length of
  283. * @returns {Number} length of a
  284. */
  285. function length(a) {
  286. var x = a[0],
  287. y = a[1];
  288. return Math.hypot(x, y);
  289. }
  290. /**
  291. * Calculates the squared length of a vec2
  292. *
  293. * @param {ReadonlyVec2} a vector to calculate squared length of
  294. * @returns {Number} squared length of a
  295. */
  296. function squaredLength(a) {
  297. var x = a[0],
  298. y = a[1];
  299. return x * x + y * y;
  300. }
  301. /**
  302. * Negates the components of a vec2
  303. *
  304. * @param {vec2} out the receiving vector
  305. * @param {ReadonlyVec2} a vector to negate
  306. * @returns {vec2} out
  307. */
  308. function negate(out, a) {
  309. out[0] = -a[0];
  310. out[1] = -a[1];
  311. return out;
  312. }
  313. /**
  314. * Returns the inverse of the components of a vec2
  315. *
  316. * @param {vec2} out the receiving vector
  317. * @param {ReadonlyVec2} a vector to invert
  318. * @returns {vec2} out
  319. */
  320. function inverse(out, a) {
  321. out[0] = 1.0 / a[0];
  322. out[1] = 1.0 / a[1];
  323. return out;
  324. }
  325. /**
  326. * Normalize a vec2
  327. *
  328. * @param {vec2} out the receiving vector
  329. * @param {ReadonlyVec2} a vector to normalize
  330. * @returns {vec2} out
  331. */
  332. function normalize(out, a) {
  333. var x = a[0],
  334. y = a[1];
  335. var len = x * x + y * y;
  336. if (len > 0) {
  337. //TODO: evaluate use of glm_invsqrt here?
  338. len = 1 / Math.sqrt(len);
  339. }
  340. out[0] = a[0] * len;
  341. out[1] = a[1] * len;
  342. return out;
  343. }
  344. /**
  345. * Calculates the dot product of two vec2's
  346. *
  347. * @param {ReadonlyVec2} a the first operand
  348. * @param {ReadonlyVec2} b the second operand
  349. * @returns {Number} dot product of a and b
  350. */
  351. function dot(a, b) {
  352. return a[0] * b[0] + a[1] * b[1];
  353. }
  354. /**
  355. * Computes the cross product of two vec2's
  356. * Note that the cross product must by definition produce a 3D vector
  357. *
  358. * @param {vec3} out the receiving vector
  359. * @param {ReadonlyVec2} a the first operand
  360. * @param {ReadonlyVec2} b the second operand
  361. * @returns {vec3} out
  362. */
  363. function cross(out, a, b) {
  364. var z = a[0] * b[1] - a[1] * b[0];
  365. out[0] = out[1] = 0;
  366. out[2] = z;
  367. return out;
  368. }
  369. /**
  370. * Performs a linear interpolation between two vec2's
  371. *
  372. * @param {vec2} out the receiving vector
  373. * @param {ReadonlyVec2} a the first operand
  374. * @param {ReadonlyVec2} b the second operand
  375. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  376. * @returns {vec2} out
  377. */
  378. function lerp(out, a, b, t) {
  379. var ax = a[0],
  380. ay = a[1];
  381. out[0] = ax + t * (b[0] - ax);
  382. out[1] = ay + t * (b[1] - ay);
  383. return out;
  384. }
  385. /**
  386. * Generates a random vector with the given scale
  387. *
  388. * @param {vec2} out the receiving vector
  389. * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
  390. * @returns {vec2} out
  391. */
  392. function random(out, scale) {
  393. scale = scale || 1.0;
  394. var r = glMatrix.RANDOM() * 2.0 * Math.PI;
  395. out[0] = Math.cos(r) * scale;
  396. out[1] = Math.sin(r) * scale;
  397. return out;
  398. }
  399. /**
  400. * Transforms the vec2 with a mat2
  401. *
  402. * @param {vec2} out the receiving vector
  403. * @param {ReadonlyVec2} a the vector to transform
  404. * @param {ReadonlyMat2} m matrix to transform with
  405. * @returns {vec2} out
  406. */
  407. function transformMat2(out, a, m) {
  408. var x = a[0],
  409. y = a[1];
  410. out[0] = m[0] * x + m[2] * y;
  411. out[1] = m[1] * x + m[3] * y;
  412. return out;
  413. }
  414. /**
  415. * Transforms the vec2 with a mat2d
  416. *
  417. * @param {vec2} out the receiving vector
  418. * @param {ReadonlyVec2} a the vector to transform
  419. * @param {ReadonlyMat2d} m matrix to transform with
  420. * @returns {vec2} out
  421. */
  422. function transformMat2d(out, a, m) {
  423. var x = a[0],
  424. y = a[1];
  425. out[0] = m[0] * x + m[2] * y + m[4];
  426. out[1] = m[1] * x + m[3] * y + m[5];
  427. return out;
  428. }
  429. /**
  430. * Transforms the vec2 with a mat3
  431. * 3rd vector component is implicitly '1'
  432. *
  433. * @param {vec2} out the receiving vector
  434. * @param {ReadonlyVec2} a the vector to transform
  435. * @param {ReadonlyMat3} m matrix to transform with
  436. * @returns {vec2} out
  437. */
  438. function transformMat3(out, a, m) {
  439. var x = a[0],
  440. y = a[1];
  441. out[0] = m[0] * x + m[3] * y + m[6];
  442. out[1] = m[1] * x + m[4] * y + m[7];
  443. return out;
  444. }
  445. /**
  446. * Transforms the vec2 with a mat4
  447. * 3rd vector component is implicitly '0'
  448. * 4th vector component is implicitly '1'
  449. *
  450. * @param {vec2} out the receiving vector
  451. * @param {ReadonlyVec2} a the vector to transform
  452. * @param {ReadonlyMat4} m matrix to transform with
  453. * @returns {vec2} out
  454. */
  455. function transformMat4(out, a, m) {
  456. var x = a[0];
  457. var y = a[1];
  458. out[0] = m[0] * x + m[4] * y + m[12];
  459. out[1] = m[1] * x + m[5] * y + m[13];
  460. return out;
  461. }
  462. /**
  463. * Rotate a 2D vector
  464. * @param {vec2} out The receiving vec2
  465. * @param {ReadonlyVec2} a The vec2 point to rotate
  466. * @param {ReadonlyVec2} b The origin of the rotation
  467. * @param {Number} rad The angle of rotation in radians
  468. * @returns {vec2} out
  469. */
  470. function rotate(out, a, b, rad) {
  471. //Translate point to the origin
  472. var p0 = a[0] - b[0],
  473. p1 = a[1] - b[1],
  474. sinC = Math.sin(rad),
  475. cosC = Math.cos(rad); //perform rotation and translate to correct position
  476. out[0] = p0 * cosC - p1 * sinC + b[0];
  477. out[1] = p0 * sinC + p1 * cosC + b[1];
  478. return out;
  479. }
  480. /**
  481. * Get the angle between two 2D vectors
  482. * @param {ReadonlyVec2} a The first operand
  483. * @param {ReadonlyVec2} b The second operand
  484. * @returns {Number} The angle in radians
  485. */
  486. function angle(a, b) {
  487. var x1 = a[0],
  488. y1 = a[1],
  489. x2 = b[0],
  490. y2 = b[1],
  491. // mag is the product of the magnitudes of a and b
  492. mag = Math.sqrt(x1 * x1 + y1 * y1) * Math.sqrt(x2 * x2 + y2 * y2),
  493. // mag &&.. short circuits if mag == 0
  494. cosine = mag && (x1 * x2 + y1 * y2) / mag; // Math.min(Math.max(cosine, -1), 1) clamps the cosine between -1 and 1
  495. return Math.acos(Math.min(Math.max(cosine, -1), 1));
  496. }
  497. /**
  498. * Set the components of a vec2 to zero
  499. *
  500. * @param {vec2} out the receiving vector
  501. * @returns {vec2} out
  502. */
  503. function zero(out) {
  504. out[0] = 0.0;
  505. out[1] = 0.0;
  506. return out;
  507. }
  508. /**
  509. * Returns a string representation of a vector
  510. *
  511. * @param {ReadonlyVec2} a vector to represent as a string
  512. * @returns {String} string representation of the vector
  513. */
  514. function str(a) {
  515. return "vec2(" + a[0] + ", " + a[1] + ")";
  516. }
  517. /**
  518. * Returns whether or not the vectors exactly have the same elements in the same position (when compared with ===)
  519. *
  520. * @param {ReadonlyVec2} a The first vector.
  521. * @param {ReadonlyVec2} b The second vector.
  522. * @returns {Boolean} True if the vectors are equal, false otherwise.
  523. */
  524. function exactEquals(a, b) {
  525. return a[0] === b[0] && a[1] === b[1];
  526. }
  527. /**
  528. * Returns whether or not the vectors have approximately the same elements in the same position.
  529. *
  530. * @param {ReadonlyVec2} a The first vector.
  531. * @param {ReadonlyVec2} b The second vector.
  532. * @returns {Boolean} True if the vectors are equal, false otherwise.
  533. */
  534. function equals(a, b) {
  535. var a0 = a[0],
  536. a1 = a[1];
  537. var b0 = b[0],
  538. b1 = b[1];
  539. return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1));
  540. }
  541. /**
  542. * Alias for {@link vec2.length}
  543. * @function
  544. */
  545. var len = length;
  546. /**
  547. * Alias for {@link vec2.subtract}
  548. * @function
  549. */
  550. exports.len = len;
  551. var sub = subtract;
  552. /**
  553. * Alias for {@link vec2.multiply}
  554. * @function
  555. */
  556. exports.sub = sub;
  557. var mul = multiply;
  558. /**
  559. * Alias for {@link vec2.divide}
  560. * @function
  561. */
  562. exports.mul = mul;
  563. var div = divide;
  564. /**
  565. * Alias for {@link vec2.distance}
  566. * @function
  567. */
  568. exports.div = div;
  569. var dist = distance;
  570. /**
  571. * Alias for {@link vec2.squaredDistance}
  572. * @function
  573. */
  574. exports.dist = dist;
  575. var sqrDist = squaredDistance;
  576. /**
  577. * Alias for {@link vec2.squaredLength}
  578. * @function
  579. */
  580. exports.sqrDist = sqrDist;
  581. var sqrLen = squaredLength;
  582. /**
  583. * Perform some operation over an array of vec2s.
  584. *
  585. * @param {Array} a the array of vectors to iterate over
  586. * @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed
  587. * @param {Number} offset Number of elements to skip at the beginning of the array
  588. * @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array
  589. * @param {Function} fn Function to call for each vector in the array
  590. * @param {Object} [arg] additional argument to pass to fn
  591. * @returns {Array} a
  592. * @function
  593. */
  594. exports.sqrLen = sqrLen;
  595. var forEach = function () {
  596. var vec = create();
  597. return function (a, stride, offset, count, fn, arg) {
  598. var i, l;
  599. if (!stride) {
  600. stride = 2;
  601. }
  602. if (!offset) {
  603. offset = 0;
  604. }
  605. if (count) {
  606. l = Math.min(count * stride + offset, a.length);
  607. } else {
  608. l = a.length;
  609. }
  610. for (i = offset; i < l; i += stride) {
  611. vec[0] = a[i];
  612. vec[1] = a[i + 1];
  613. fn(vec, vec, arg);
  614. a[i] = vec[0];
  615. a[i + 1] = vec[1];
  616. }
  617. return a;
  618. };
  619. }();
  620. exports.forEach = forEach;