vector.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.mid = exports.calcBBox = exports.angleBetween = exports.angleWithQuadrant = exports.angle = exports.dist = exports.add = exports.sub = void 0;
  4. function sub([x1, y1], [x2, y2]) {
  5. return [x1 - x2, y1 - y2];
  6. }
  7. exports.sub = sub;
  8. function add([x1, y1], [x2, y2]) {
  9. return [x1 + x2, y1 + y2];
  10. }
  11. exports.add = add;
  12. function dist([x0, y0], [x1, y1]) {
  13. return Math.sqrt(Math.pow((x0 - x1), 2) + Math.pow((y0 - y1), 2));
  14. }
  15. exports.dist = dist;
  16. /**
  17. * Calculate angle of vector [x, y].
  18. */
  19. function angle([x, y]) {
  20. return Math.atan2(y, x);
  21. }
  22. exports.angle = angle;
  23. /**
  24. * Calculate angle of [x, y], then + Math.PI / 2.
  25. * Because of the difference between `Geometric coordinate system` and `Visualization coordinate system`.
  26. * @returns
  27. */
  28. function angleWithQuadrant([x, y]) {
  29. return angle([x, y]) + Math.PI / 2;
  30. }
  31. exports.angleWithQuadrant = angleWithQuadrant;
  32. function angleBetween(v0, v1) {
  33. const a0 = angle(v0);
  34. const a1 = angle(v1);
  35. if (a0 < a1)
  36. return a1 - a0;
  37. return Math.PI * 2 - (a0 - a1);
  38. }
  39. exports.angleBetween = angleBetween;
  40. function calcBBox(points) {
  41. let minX = Infinity;
  42. let maxX = -Infinity;
  43. let minY = Infinity;
  44. let maxY = -Infinity;
  45. for (const [x, y] of points) {
  46. minX = Math.min(x, minX);
  47. maxX = Math.max(x, maxX);
  48. minY = Math.min(y, minY);
  49. maxY = Math.max(y, maxY);
  50. }
  51. const width = maxX - minX;
  52. const height = maxY - minY;
  53. return [minX, minY, width, height];
  54. }
  55. exports.calcBBox = calcBBox;
  56. /**
  57. * Get the center of two points.
  58. */
  59. function mid([x1, y1], [x2, y2]) {
  60. return [(x1 + x2) / 2, (y1 + y2) / 2];
  61. }
  62. exports.mid = mid;
  63. //# sourceMappingURL=vector.js.map