UNPKG

1.77 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.mid = exports.calcBBox = exports.angleBetween = exports.angleWithQuadrant = exports.angle = exports.dist = exports.add = exports.sub = void 0;
4function sub([x1, y1], [x2, y2]) {
5 return [x1 - x2, y1 - y2];
6}
7exports.sub = sub;
8function add([x1, y1], [x2, y2]) {
9 return [x1 + x2, y1 + y2];
10}
11exports.add = add;
12function dist([x0, y0], [x1, y1]) {
13 return Math.sqrt(Math.pow((x0 - x1), 2) + Math.pow((y0 - y1), 2));
14}
15exports.dist = dist;
16/**
17 * Calculate angle of vector [x, y].
18 */
19function angle([x, y]) {
20 return Math.atan2(y, x);
21}
22exports.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 */
28function angleWithQuadrant([x, y]) {
29 return angle([x, y]) + Math.PI / 2;
30}
31exports.angleWithQuadrant = angleWithQuadrant;
32function 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}
39exports.angleBetween = angleBetween;
40function 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}
55exports.calcBBox = calcBBox;
56/**
57 * Get the center of two points.
58 */
59function mid([x1, y1], [x2, y2]) {
60 return [(x1 + x2) / 2, (y1 + y2) / 2];
61}
62exports.mid = mid;
63//# sourceMappingURL=vector.js.map
\No newline at end of file