@wentools/spatial 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +119 -0
  3. package/esm/bounding_box.d.ts +12 -0
  4. package/esm/bounding_box.d.ts.map +1 -0
  5. package/esm/bounding_box.js +24 -0
  6. package/esm/circle.d.ts +47 -0
  7. package/esm/circle.d.ts.map +1 -0
  8. package/esm/circle.js +55 -0
  9. package/esm/distance.d.ts +31 -0
  10. package/esm/distance.d.ts.map +1 -0
  11. package/esm/distance.js +36 -0
  12. package/esm/intersection.d.ts +32 -0
  13. package/esm/intersection.d.ts.map +1 -0
  14. package/esm/intersection.js +77 -0
  15. package/esm/line.d.ts +39 -0
  16. package/esm/line.d.ts.map +1 -0
  17. package/esm/line.js +71 -0
  18. package/esm/mod.d.ts +18 -0
  19. package/esm/mod.d.ts.map +1 -0
  20. package/esm/mod.js +12 -0
  21. package/esm/package.json +3 -0
  22. package/esm/point/mod.d.ts +6 -0
  23. package/esm/point/mod.d.ts.map +1 -0
  24. package/esm/point/mod.js +4 -0
  25. package/esm/point/point.d.ts +54 -0
  26. package/esm/point/point.d.ts.map +1 -0
  27. package/esm/point/point.js +61 -0
  28. package/esm/rectangle.d.ts +125 -0
  29. package/esm/rectangle.d.ts.map +1 -0
  30. package/esm/rectangle.js +142 -0
  31. package/esm/vector/mod.d.ts +6 -0
  32. package/esm/vector/mod.d.ts.map +1 -0
  33. package/esm/vector/mod.js +4 -0
  34. package/esm/vector/vector.d.ts +69 -0
  35. package/esm/vector/vector.d.ts.map +1 -0
  36. package/esm/vector/vector.js +84 -0
  37. package/package.json +33 -0
  38. package/script/bounding_box.d.ts +12 -0
  39. package/script/bounding_box.d.ts.map +1 -0
  40. package/script/bounding_box.js +27 -0
  41. package/script/circle.d.ts +47 -0
  42. package/script/circle.d.ts.map +1 -0
  43. package/script/circle.js +65 -0
  44. package/script/distance.d.ts +31 -0
  45. package/script/distance.d.ts.map +1 -0
  46. package/script/distance.js +43 -0
  47. package/script/intersection.d.ts +32 -0
  48. package/script/intersection.d.ts.map +1 -0
  49. package/script/intersection.js +83 -0
  50. package/script/line.d.ts +39 -0
  51. package/script/line.d.ts.map +1 -0
  52. package/script/line.js +79 -0
  53. package/script/mod.d.ts +18 -0
  54. package/script/mod.d.ts.map +1 -0
  55. package/script/mod.js +79 -0
  56. package/script/package.json +3 -0
  57. package/script/point/mod.d.ts +6 -0
  58. package/script/point/mod.d.ts.map +1 -0
  59. package/script/point/mod.js +15 -0
  60. package/script/point/point.d.ts +54 -0
  61. package/script/point/point.d.ts.map +1 -0
  62. package/script/point/point.js +71 -0
  63. package/script/rectangle.d.ts +125 -0
  64. package/script/rectangle.d.ts.map +1 -0
  65. package/script/rectangle.js +162 -0
  66. package/script/vector/mod.d.ts +6 -0
  67. package/script/vector/mod.d.ts.map +1 -0
  68. package/script/vector/mod.js +20 -0
  69. package/script/vector/vector.d.ts +69 -0
  70. package/script/vector/vector.d.ts.map +1 -0
  71. package/script/vector/vector.js +99 -0
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ /**
3
+ * Point2D entity - represents a point in 2D space.
4
+ * x typically represents horizontal position (column in grids)
5
+ * y typically represents vertical position (row in grids)
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.clampPoint = exports.rotatePoint = exports.midpoint = exports.lerpPoint = exports.pointsEqual = exports.translatePoint = exports.createOrigin = exports.createPoint = void 0;
9
+ /**
10
+ * Creates a point from x/y coordinates.
11
+ */
12
+ const createPoint = (x, y) => ({ x, y });
13
+ exports.createPoint = createPoint;
14
+ /**
15
+ * Creates a point at the origin (0, 0).
16
+ */
17
+ const createOrigin = () => ({ x: 0, y: 0 });
18
+ exports.createOrigin = createOrigin;
19
+ /**
20
+ * Translates a point by the given offset.
21
+ */
22
+ const translatePoint = (point, dx, dy) => ({
23
+ x: point.x + dx,
24
+ y: point.y + dy,
25
+ });
26
+ exports.translatePoint = translatePoint;
27
+ /**
28
+ * Checks if two points are equal.
29
+ */
30
+ const pointsEqual = (a, b) => a.x === b.x && a.y === b.y;
31
+ exports.pointsEqual = pointsEqual;
32
+ /**
33
+ * Linearly interpolates between two points.
34
+ * t=0 returns a, t=1 returns b. Values outside [0,1] extrapolate.
35
+ */
36
+ const lerpPoint = (a, b, t) => ({
37
+ x: a.x + (b.x - a.x) * t,
38
+ y: a.y + (b.y - a.y) * t,
39
+ });
40
+ exports.lerpPoint = lerpPoint;
41
+ /**
42
+ * Returns the midpoint between two points.
43
+ */
44
+ const midpoint = (a, b) => ({
45
+ x: (a.x + b.x) / 2,
46
+ y: (a.y + b.y) / 2,
47
+ });
48
+ exports.midpoint = midpoint;
49
+ /**
50
+ * Rotates a point around an origin by the given angle in radians.
51
+ * Defaults to rotating around (0, 0).
52
+ */
53
+ const rotatePoint = (point, angle, origin = { x: 0, y: 0 }) => {
54
+ const cos = Math.cos(angle);
55
+ const sin = Math.sin(angle);
56
+ const dx = point.x - origin.x;
57
+ const dy = point.y - origin.y;
58
+ return {
59
+ x: origin.x + dx * cos - dy * sin,
60
+ y: origin.y + dx * sin + dy * cos,
61
+ };
62
+ };
63
+ exports.rotatePoint = rotatePoint;
64
+ /**
65
+ * Clamps a point to be within the given rectangular bounds.
66
+ */
67
+ const clampPoint = (point, bounds) => ({
68
+ x: Math.max(bounds.x, Math.min(point.x, bounds.x + bounds.width)),
69
+ y: Math.max(bounds.y, Math.min(point.y, bounds.y + bounds.height)),
70
+ });
71
+ exports.clampPoint = clampPoint;
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Rectangle entity - represents a rectangle in 2D space.
3
+ */
4
+ import type { Point2D } from './point/mod.js';
5
+ /**
6
+ * A rectangle defined by position and dimensions.
7
+ */
8
+ type Rectangle = {
9
+ x: number;
10
+ y: number;
11
+ width: number;
12
+ height: number;
13
+ };
14
+ /**
15
+ * Creates a rectangle from position and dimensions.
16
+ */
17
+ declare const createRectangle: (x: number, y: number, width: number, height: number) => Rectangle;
18
+ /**
19
+ * Creates a rectangle from two corner points.
20
+ */
21
+ declare const createRectangleFromPoints: (topLeft: Point2D, bottomRight: Point2D) => Rectangle;
22
+ /**
23
+ * Gets the area of a rectangle.
24
+ */
25
+ declare const getRectangleArea: (rect: Rectangle) => number;
26
+ /**
27
+ * Gets the perimeter of a rectangle.
28
+ */
29
+ declare const getRectanglePerimeter: (rect: Rectangle) => number;
30
+ /**
31
+ * Gets the center point of a rectangle.
32
+ */
33
+ declare const getRectangleCenter: (rect: Rectangle) => Point2D;
34
+ /**
35
+ * Checks if a point is inside a rectangle (inclusive of edges).
36
+ */
37
+ declare const isPointInRectangle: (point: Point2D, rect: Rectangle) => boolean;
38
+ /**
39
+ * Checks if two rectangles overlap.
40
+ */
41
+ declare const rectanglesOverlap: (a: Rectangle, b: Rectangle) => boolean;
42
+ /**
43
+ * Checks if rectangle a fully contains rectangle b.
44
+ */
45
+ declare const rectangleContains: (outer: Rectangle, inner: Rectangle) => boolean;
46
+ /**
47
+ * Calculates the intersection of two rectangles.
48
+ * Returns null if rectangles don't overlap.
49
+ */
50
+ declare const rectangleIntersection: (a: Rectangle, b: Rectangle) => Rectangle | null;
51
+ /**
52
+ * Calculates the union (bounding box) of two rectangles.
53
+ */
54
+ declare const rectangleUnion: (a: Rectangle, b: Rectangle) => Rectangle;
55
+ /**
56
+ * Calculate top-left position to center an inner element within a container.
57
+ * Returns the (x, y) coordinates where the inner element should be placed
58
+ * so that it is centered within the container.
59
+ *
60
+ * @param container - Object with width and height properties
61
+ * @param inner - Object with width and height properties
62
+ * @returns Point representing top-left position for centered placement
63
+ */
64
+ declare const centerRectangleInContainer: (container: {
65
+ width: number;
66
+ height: number;
67
+ }, inner: {
68
+ width: number;
69
+ height: number;
70
+ }) => Point2D;
71
+ /**
72
+ * Calculate horizontal center alignment position.
73
+ * Returns the x coordinate to position an element horizontally centered relative to a reference element.
74
+ *
75
+ * @param reference - Object with x and width properties (the element to center against)
76
+ * @param element - Object with width property (the element being positioned)
77
+ * @returns x coordinate for horizontally centered placement
78
+ */
79
+ declare const centerHorizontally: (reference: {
80
+ x: number;
81
+ width: number;
82
+ }, element: {
83
+ width: number;
84
+ }) => number;
85
+ /**
86
+ * Calculate vertical center alignment position.
87
+ * Returns the y coordinate to position an element vertically centered relative to a reference element.
88
+ *
89
+ * @param reference - Object with y and height properties (the element to center against)
90
+ * @param element - Object with height property (the element being positioned)
91
+ * @returns y coordinate for vertically centered placement
92
+ */
93
+ declare const centerVertically: (reference: {
94
+ y: number;
95
+ height: number;
96
+ }, element: {
97
+ height: number;
98
+ }) => number;
99
+ /**
100
+ * Moves a rectangle by the given offset.
101
+ */
102
+ declare const translateRectangle: (rect: Rectangle, dx: number, dy: number) => Rectangle;
103
+ /**
104
+ * Grows a rectangle uniformly by the given amount (padding).
105
+ * Each side expands outward by `amount`. Negative values shrink.
106
+ */
107
+ declare const expandRectangle: (rect: Rectangle, amount: number) => Rectangle;
108
+ /**
109
+ * Shrinks a rectangle uniformly by the given amount (margin).
110
+ * Each side moves inward by `amount`. Negative values expand.
111
+ */
112
+ declare const insetRectangle: (rect: Rectangle, amount: number) => Rectangle;
113
+ /**
114
+ * Scales a rectangle by a factor from an origin point.
115
+ * Default origin is the rectangle's center.
116
+ */
117
+ declare const scaleRectangle: (rect: Rectangle, factor: number, origin?: Point2D) => Rectangle;
118
+ /**
119
+ * Returns the 4 corner points of a rectangle in clockwise order:
120
+ * topLeft, topRight, bottomRight, bottomLeft.
121
+ */
122
+ declare const getRectangleCorners: (rect: Rectangle) => [Point2D, Point2D, Point2D, Point2D];
123
+ export type { Rectangle };
124
+ export { createRectangle, createRectangleFromPoints, getRectangleArea, getRectanglePerimeter, getRectangleCenter, isPointInRectangle, rectanglesOverlap, rectangleContains, rectangleIntersection, rectangleUnion, centerRectangleInContainer, centerHorizontally, centerVertically, translateRectangle, expandRectangle, insetRectangle, scaleRectangle, getRectangleCorners, };
125
+ //# sourceMappingURL=rectangle.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BAAyB,EACzB,gBAAgB,EAChB,qBAAqB,EACrB,kBAAkB,EAClB,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,EACjB,qBAAqB,EACrB,cAAc,EACd,0BAA0B,EAC1B,kBAAkB,EAClB,gBAAgB,EAChB,kBAAkB,EAClB,eAAe,EACf,cAAc,EACd,cAAc,EACd,mBAAmB,GACnB,CAAA"}
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ /**
3
+ * Rectangle entity - represents a rectangle in 2D space.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getRectangleCorners = exports.scaleRectangle = exports.insetRectangle = exports.expandRectangle = exports.translateRectangle = exports.centerVertically = exports.centerHorizontally = exports.centerRectangleInContainer = exports.rectangleUnion = exports.rectangleIntersection = exports.rectangleContains = exports.rectanglesOverlap = exports.isPointInRectangle = exports.getRectangleCenter = exports.getRectanglePerimeter = exports.getRectangleArea = exports.createRectangleFromPoints = exports.createRectangle = void 0;
7
+ const mod_js_1 = require("./point/mod.js");
8
+ /**
9
+ * Creates a rectangle from position and dimensions.
10
+ */
11
+ const createRectangle = (x, y, width, height) => ({
12
+ x,
13
+ y,
14
+ width,
15
+ height,
16
+ });
17
+ exports.createRectangle = createRectangle;
18
+ /**
19
+ * Creates a rectangle from two corner points.
20
+ */
21
+ const createRectangleFromPoints = (topLeft, bottomRight) => ({
22
+ x: topLeft.x,
23
+ y: topLeft.y,
24
+ width: bottomRight.x - topLeft.x,
25
+ height: bottomRight.y - topLeft.y,
26
+ });
27
+ exports.createRectangleFromPoints = createRectangleFromPoints;
28
+ /**
29
+ * Gets the area of a rectangle.
30
+ */
31
+ const getRectangleArea = (rect) => rect.width * rect.height;
32
+ exports.getRectangleArea = getRectangleArea;
33
+ /**
34
+ * Gets the perimeter of a rectangle.
35
+ */
36
+ const getRectanglePerimeter = (rect) => 2 * (rect.width + rect.height);
37
+ exports.getRectanglePerimeter = getRectanglePerimeter;
38
+ /**
39
+ * Gets the center point of a rectangle.
40
+ */
41
+ const getRectangleCenter = (rect) => ({
42
+ x: rect.x + rect.width / 2,
43
+ y: rect.y + rect.height / 2,
44
+ });
45
+ exports.getRectangleCenter = getRectangleCenter;
46
+ /**
47
+ * Checks if a point is inside a rectangle (inclusive of edges).
48
+ */
49
+ const isPointInRectangle = (point, rect) => point.x >= rect.x &&
50
+ point.x <= rect.x + rect.width &&
51
+ point.y >= rect.y &&
52
+ point.y <= rect.y + rect.height;
53
+ exports.isPointInRectangle = isPointInRectangle;
54
+ /**
55
+ * Checks if two rectangles overlap.
56
+ */
57
+ const rectanglesOverlap = (a, b) => !(a.x + a.width < b.x || b.x + b.width < a.x || a.y + a.height < b.y || b.y + b.height < a.y);
58
+ exports.rectanglesOverlap = rectanglesOverlap;
59
+ /**
60
+ * Checks if rectangle a fully contains rectangle b.
61
+ */
62
+ const rectangleContains = (outer, inner) => inner.x >= outer.x &&
63
+ inner.y >= outer.y &&
64
+ inner.x + inner.width <= outer.x + outer.width &&
65
+ inner.y + inner.height <= outer.y + outer.height;
66
+ exports.rectangleContains = rectangleContains;
67
+ /**
68
+ * Calculates the intersection of two rectangles.
69
+ * Returns null if rectangles don't overlap.
70
+ */
71
+ const rectangleIntersection = (a, b) => {
72
+ const x = Math.max(a.x, b.x);
73
+ const y = Math.max(a.y, b.y);
74
+ const right = Math.min(a.x + a.width, b.x + b.width);
75
+ const bottom = Math.min(a.y + a.height, b.y + b.height);
76
+ if (right < x || bottom < y)
77
+ return null;
78
+ return createRectangle(x, y, right - x, bottom - y);
79
+ };
80
+ exports.rectangleIntersection = rectangleIntersection;
81
+ /**
82
+ * Calculates the union (bounding box) of two rectangles.
83
+ */
84
+ const rectangleUnion = (a, b) => {
85
+ const x = Math.min(a.x, b.x);
86
+ const y = Math.min(a.y, b.y);
87
+ const right = Math.max(a.x + a.width, b.x + b.width);
88
+ const bottom = Math.max(a.y + a.height, b.y + b.height);
89
+ return createRectangle(x, y, right - x, bottom - y);
90
+ };
91
+ exports.rectangleUnion = rectangleUnion;
92
+ /**
93
+ * Calculate top-left position to center an inner element within a container.
94
+ * Returns the (x, y) coordinates where the inner element should be placed
95
+ * so that it is centered within the container.
96
+ *
97
+ * @param container - Object with width and height properties
98
+ * @param inner - Object with width and height properties
99
+ * @returns Point representing top-left position for centered placement
100
+ */
101
+ const centerRectangleInContainer = (container, inner) => ({
102
+ x: Math.floor((container.width - inner.width) / 2),
103
+ y: Math.floor((container.height - inner.height) / 2),
104
+ });
105
+ exports.centerRectangleInContainer = centerRectangleInContainer;
106
+ /**
107
+ * Calculate horizontal center alignment position.
108
+ * Returns the x coordinate to position an element horizontally centered relative to a reference element.
109
+ *
110
+ * @param reference - Object with x and width properties (the element to center against)
111
+ * @param element - Object with width property (the element being positioned)
112
+ * @returns x coordinate for horizontally centered placement
113
+ */
114
+ const centerHorizontally = (reference, element) => reference.x + reference.width / 2 - element.width / 2;
115
+ exports.centerHorizontally = centerHorizontally;
116
+ /**
117
+ * Calculate vertical center alignment position.
118
+ * Returns the y coordinate to position an element vertically centered relative to a reference element.
119
+ *
120
+ * @param reference - Object with y and height properties (the element to center against)
121
+ * @param element - Object with height property (the element being positioned)
122
+ * @returns y coordinate for vertically centered placement
123
+ */
124
+ const centerVertically = (reference, element) => reference.y + reference.height / 2 - element.height / 2;
125
+ exports.centerVertically = centerVertically;
126
+ /**
127
+ * Moves a rectangle by the given offset.
128
+ */
129
+ const translateRectangle = (rect, dx, dy) => createRectangle(rect.x + dx, rect.y + dy, rect.width, rect.height);
130
+ exports.translateRectangle = translateRectangle;
131
+ /**
132
+ * Grows a rectangle uniformly by the given amount (padding).
133
+ * Each side expands outward by `amount`. Negative values shrink.
134
+ */
135
+ const expandRectangle = (rect, amount) => createRectangle(rect.x - amount, rect.y - amount, rect.width + 2 * amount, rect.height + 2 * amount);
136
+ exports.expandRectangle = expandRectangle;
137
+ /**
138
+ * Shrinks a rectangle uniformly by the given amount (margin).
139
+ * Each side moves inward by `amount`. Negative values expand.
140
+ */
141
+ const insetRectangle = (rect, amount) => createRectangle(rect.x + amount, rect.y + amount, rect.width - 2 * amount, rect.height - 2 * amount);
142
+ exports.insetRectangle = insetRectangle;
143
+ /**
144
+ * Scales a rectangle by a factor from an origin point.
145
+ * Default origin is the rectangle's center.
146
+ */
147
+ const scaleRectangle = (rect, factor, origin) => {
148
+ const scaleOrigin = origin ?? getRectangleCenter(rect);
149
+ return createRectangle(scaleOrigin.x + (rect.x - scaleOrigin.x) * factor, scaleOrigin.y + (rect.y - scaleOrigin.y) * factor, rect.width * factor, rect.height * factor);
150
+ };
151
+ exports.scaleRectangle = scaleRectangle;
152
+ /**
153
+ * Returns the 4 corner points of a rectangle in clockwise order:
154
+ * topLeft, topRight, bottomRight, bottomLeft.
155
+ */
156
+ const getRectangleCorners = (rect) => [
157
+ (0, mod_js_1.createPoint)(rect.x, rect.y),
158
+ (0, mod_js_1.createPoint)(rect.x + rect.width, rect.y),
159
+ (0, mod_js_1.createPoint)(rect.x + rect.width, rect.y + rect.height),
160
+ (0, mod_js_1.createPoint)(rect.x, rect.y + rect.height),
161
+ ];
162
+ exports.getRectangleCorners = getRectangleCorners;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Vector2D entity exports.
3
+ */
4
+ export type { Vector2D } from './vector.js';
5
+ export { createVector, createZeroVector, vectorMagnitude, addVectors, subtractVectors, scaleVector, normalizeVector, dotProduct, crossProduct2D, perpendicular, vectorFromAngle, vectorToAngle, rotateVector, } from './vector.js';
6
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/vector/mod.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,YAAY,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EACN,YAAY,EACZ,gBAAgB,EAChB,eAAe,EACf,UAAU,EACV,eAAe,EACf,WAAW,EACX,eAAe,EACf,UAAU,EACV,cAAc,EACd,aAAa,EACb,eAAe,EACf,aAAa,EACb,YAAY,GACZ,MAAM,aAAa,CAAA"}
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ /**
3
+ * Vector2D entity exports.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.rotateVector = exports.vectorToAngle = exports.vectorFromAngle = exports.perpendicular = exports.crossProduct2D = exports.dotProduct = exports.normalizeVector = exports.scaleVector = exports.subtractVectors = exports.addVectors = exports.vectorMagnitude = exports.createZeroVector = exports.createVector = void 0;
7
+ var vector_js_1 = require("./vector.js");
8
+ Object.defineProperty(exports, "createVector", { enumerable: true, get: function () { return vector_js_1.createVector; } });
9
+ Object.defineProperty(exports, "createZeroVector", { enumerable: true, get: function () { return vector_js_1.createZeroVector; } });
10
+ Object.defineProperty(exports, "vectorMagnitude", { enumerable: true, get: function () { return vector_js_1.vectorMagnitude; } });
11
+ Object.defineProperty(exports, "addVectors", { enumerable: true, get: function () { return vector_js_1.addVectors; } });
12
+ Object.defineProperty(exports, "subtractVectors", { enumerable: true, get: function () { return vector_js_1.subtractVectors; } });
13
+ Object.defineProperty(exports, "scaleVector", { enumerable: true, get: function () { return vector_js_1.scaleVector; } });
14
+ Object.defineProperty(exports, "normalizeVector", { enumerable: true, get: function () { return vector_js_1.normalizeVector; } });
15
+ Object.defineProperty(exports, "dotProduct", { enumerable: true, get: function () { return vector_js_1.dotProduct; } });
16
+ Object.defineProperty(exports, "crossProduct2D", { enumerable: true, get: function () { return vector_js_1.crossProduct2D; } });
17
+ Object.defineProperty(exports, "perpendicular", { enumerable: true, get: function () { return vector_js_1.perpendicular; } });
18
+ Object.defineProperty(exports, "vectorFromAngle", { enumerable: true, get: function () { return vector_js_1.vectorFromAngle; } });
19
+ Object.defineProperty(exports, "vectorToAngle", { enumerable: true, get: function () { return vector_js_1.vectorToAngle; } });
20
+ Object.defineProperty(exports, "rotateVector", { enumerable: true, get: function () { return vector_js_1.rotateVector; } });
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Vector2D entity - represents a 2D vector with x/y components.
3
+ * Same structure as Point2D but different semantic meaning for direction/magnitude.
4
+ */
5
+ /**
6
+ * A 2D vector with x/y components.
7
+ */
8
+ type Vector2D = {
9
+ x: number;
10
+ y: number;
11
+ };
12
+ /**
13
+ * Creates a vector from x/y components.
14
+ */
15
+ declare const createVector: (x: number, y: number) => Vector2D;
16
+ /**
17
+ * Creates a zero vector (0, 0).
18
+ */
19
+ declare const createZeroVector: () => Vector2D;
20
+ /**
21
+ * Calculates the magnitude (length) of a vector.
22
+ */
23
+ declare const vectorMagnitude: (vector: Vector2D) => number;
24
+ /**
25
+ * Adds two vectors.
26
+ */
27
+ declare const addVectors: (a: Vector2D, b: Vector2D) => Vector2D;
28
+ /**
29
+ * Subtracts vector b from vector a.
30
+ */
31
+ declare const subtractVectors: (a: Vector2D, b: Vector2D) => Vector2D;
32
+ /**
33
+ * Scales a vector by a scalar value.
34
+ */
35
+ declare const scaleVector: (vector: Vector2D, scalar: number) => Vector2D;
36
+ /**
37
+ * Normalizes a vector to unit length.
38
+ * Returns zero vector if input vector has zero length.
39
+ */
40
+ declare const normalizeVector: (vector: Vector2D) => Vector2D;
41
+ /**
42
+ * Returns the dot product of two vectors.
43
+ */
44
+ declare const dotProduct: (a: Vector2D, b: Vector2D) => number;
45
+ /**
46
+ * Returns the z-component of the 2D cross product.
47
+ * Positive = b is counter-clockwise from a, negative = clockwise, zero = parallel.
48
+ */
49
+ declare const crossProduct2D: (a: Vector2D, b: Vector2D) => number;
50
+ /**
51
+ * Returns a vector rotated 90° counter-clockwise.
52
+ */
53
+ declare const perpendicular: (v: Vector2D) => Vector2D;
54
+ /**
55
+ * Creates a vector from a polar angle and optional magnitude (default 1).
56
+ * Angle is in radians.
57
+ */
58
+ declare const vectorFromAngle: (angle: number, magnitude?: number) => Vector2D;
59
+ /**
60
+ * Returns the polar angle of a vector in radians.
61
+ */
62
+ declare const vectorToAngle: (v: Vector2D) => number;
63
+ /**
64
+ * Rotates a vector by an angle in radians.
65
+ */
66
+ declare const rotateVector: (v: Vector2D, angle: number) => Vector2D;
67
+ export type { Vector2D };
68
+ export { createVector, createZeroVector, vectorMagnitude, addVectors, subtractVectors, scaleVector, normalizeVector, dotProduct, crossProduct2D, perpendicular, vectorFromAngle, vectorToAngle, rotateVector, };
69
+ //# sourceMappingURL=vector.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vector.d.ts","sourceRoot":"","sources":["../../src/vector/vector.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,KAAK,QAAQ,GAAG;IACf,CAAC,EAAE,MAAM,CAAA;IACT,CAAC,EAAE,MAAM,CAAA;CACT,CAAA;AAED;;GAEG;AACH,QAAA,MAAM,YAAY,GAAI,GAAG,MAAM,EAAE,GAAG,MAAM,KAAG,QAAsB,CAAA;AAEnE;;GAEG;AACH,QAAA,MAAM,gBAAgB,QAAO,QAA4B,CAAA;AAEzD;;GAEG;AACH,QAAA,MAAM,eAAe,GAAI,QAAQ,QAAQ,KAAG,MACS,CAAA;AAErD;;GAEG;AACH,QAAA,MAAM,UAAU,GAAI,GAAG,QAAQ,EAAE,GAAG,QAAQ,KAAG,QAG7C,CAAA;AAEF;;GAEG;AACH,QAAA,MAAM,eAAe,GAAI,GAAG,QAAQ,EAAE,GAAG,QAAQ,KAAG,QAGlD,CAAA;AAEF;;GAEG;AACH,QAAA,MAAM,WAAW,GAAI,QAAQ,QAAQ,EAAE,QAAQ,MAAM,KAAG,QAGtD,CAAA;AAEF;;;GAGG;AACH,QAAA,MAAM,eAAe,GAAI,QAAQ,QAAQ,KAAG,QAI3C,CAAA;AAED;;GAEG;AACH,QAAA,MAAM,UAAU,GAAI,GAAG,QAAQ,EAAE,GAAG,QAAQ,KAAG,MAA+B,CAAA;AAE9E;;;GAGG;AACH,QAAA,MAAM,cAAc,GAAI,GAAG,QAAQ,EAAE,GAAG,QAAQ,KAAG,MAA+B,CAAA;AAElF;;GAEG;AACH,QAAA,MAAM,aAAa,GAAI,GAAG,QAAQ,KAAG,QAAiD,CAAA;AAEtF;;;GAGG;AACH,QAAA,MAAM,eAAe,GAAI,OAAO,MAAM,EAAE,YAAW,MAAU,KAAG,QAG9D,CAAA;AAEF;;GAEG;AACH,QAAA,MAAM,aAAa,GAAI,GAAG,QAAQ,KAAG,MAA8B,CAAA;AAEnE;;GAEG;AACH,QAAA,MAAM,YAAY,GAAI,GAAG,QAAQ,EAAE,OAAO,MAAM,KAAG,QAOlD,CAAA;AAED,YAAY,EAAE,QAAQ,EAAE,CAAA;AACxB,OAAO,EACN,YAAY,EACZ,gBAAgB,EAChB,eAAe,EACf,UAAU,EACV,eAAe,EACf,WAAW,EACX,eAAe,EACf,UAAU,EACV,cAAc,EACd,aAAa,EACb,eAAe,EACf,aAAa,EACb,YAAY,GACZ,CAAA"}
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ /**
3
+ * Vector2D entity - represents a 2D vector with x/y components.
4
+ * Same structure as Point2D but different semantic meaning for direction/magnitude.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.rotateVector = exports.vectorToAngle = exports.vectorFromAngle = exports.perpendicular = exports.crossProduct2D = exports.dotProduct = exports.normalizeVector = exports.scaleVector = exports.subtractVectors = exports.addVectors = exports.vectorMagnitude = exports.createZeroVector = exports.createVector = void 0;
8
+ /**
9
+ * Creates a vector from x/y components.
10
+ */
11
+ const createVector = (x, y) => ({ x, y });
12
+ exports.createVector = createVector;
13
+ /**
14
+ * Creates a zero vector (0, 0).
15
+ */
16
+ const createZeroVector = () => ({ x: 0, y: 0 });
17
+ exports.createZeroVector = createZeroVector;
18
+ /**
19
+ * Calculates the magnitude (length) of a vector.
20
+ */
21
+ const vectorMagnitude = (vector) => Math.sqrt(vector.x * vector.x + vector.y * vector.y);
22
+ exports.vectorMagnitude = vectorMagnitude;
23
+ /**
24
+ * Adds two vectors.
25
+ */
26
+ const addVectors = (a, b) => ({
27
+ x: a.x + b.x,
28
+ y: a.y + b.y,
29
+ });
30
+ exports.addVectors = addVectors;
31
+ /**
32
+ * Subtracts vector b from vector a.
33
+ */
34
+ const subtractVectors = (a, b) => ({
35
+ x: a.x - b.x,
36
+ y: a.y - b.y,
37
+ });
38
+ exports.subtractVectors = subtractVectors;
39
+ /**
40
+ * Scales a vector by a scalar value.
41
+ */
42
+ const scaleVector = (vector, scalar) => ({
43
+ x: vector.x * scalar,
44
+ y: vector.y * scalar,
45
+ });
46
+ exports.scaleVector = scaleVector;
47
+ /**
48
+ * Normalizes a vector to unit length.
49
+ * Returns zero vector if input vector has zero length.
50
+ */
51
+ const normalizeVector = (vector) => {
52
+ const magnitude = vectorMagnitude(vector);
53
+ if (magnitude === 0)
54
+ return createZeroVector();
55
+ return scaleVector(vector, 1 / magnitude);
56
+ };
57
+ exports.normalizeVector = normalizeVector;
58
+ /**
59
+ * Returns the dot product of two vectors.
60
+ */
61
+ const dotProduct = (a, b) => a.x * b.x + a.y * b.y;
62
+ exports.dotProduct = dotProduct;
63
+ /**
64
+ * Returns the z-component of the 2D cross product.
65
+ * Positive = b is counter-clockwise from a, negative = clockwise, zero = parallel.
66
+ */
67
+ const crossProduct2D = (a, b) => a.x * b.y - a.y * b.x;
68
+ exports.crossProduct2D = crossProduct2D;
69
+ /**
70
+ * Returns a vector rotated 90° counter-clockwise.
71
+ */
72
+ const perpendicular = (v) => ({ x: v.y === 0 ? 0 : -v.y, y: v.x });
73
+ exports.perpendicular = perpendicular;
74
+ /**
75
+ * Creates a vector from a polar angle and optional magnitude (default 1).
76
+ * Angle is in radians.
77
+ */
78
+ const vectorFromAngle = (angle, magnitude = 1) => ({
79
+ x: Math.cos(angle) * magnitude,
80
+ y: Math.sin(angle) * magnitude,
81
+ });
82
+ exports.vectorFromAngle = vectorFromAngle;
83
+ /**
84
+ * Returns the polar angle of a vector in radians.
85
+ */
86
+ const vectorToAngle = (v) => Math.atan2(v.y, v.x);
87
+ exports.vectorToAngle = vectorToAngle;
88
+ /**
89
+ * Rotates a vector by an angle in radians.
90
+ */
91
+ const rotateVector = (v, angle) => {
92
+ const cos = Math.cos(angle);
93
+ const sin = Math.sin(angle);
94
+ return {
95
+ x: v.x * cos - v.y * sin,
96
+ y: v.x * sin + v.y * cos,
97
+ };
98
+ };
99
+ exports.rotateVector = rotateVector;