@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.
- package/LICENSE +21 -0
- package/README.md +119 -0
- package/esm/bounding_box.d.ts +12 -0
- package/esm/bounding_box.d.ts.map +1 -0
- package/esm/bounding_box.js +24 -0
- package/esm/circle.d.ts +47 -0
- package/esm/circle.d.ts.map +1 -0
- package/esm/circle.js +55 -0
- package/esm/distance.d.ts +31 -0
- package/esm/distance.d.ts.map +1 -0
- package/esm/distance.js +36 -0
- package/esm/intersection.d.ts +32 -0
- package/esm/intersection.d.ts.map +1 -0
- package/esm/intersection.js +77 -0
- package/esm/line.d.ts +39 -0
- package/esm/line.d.ts.map +1 -0
- package/esm/line.js +71 -0
- package/esm/mod.d.ts +18 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +12 -0
- package/esm/package.json +3 -0
- package/esm/point/mod.d.ts +6 -0
- package/esm/point/mod.d.ts.map +1 -0
- package/esm/point/mod.js +4 -0
- package/esm/point/point.d.ts +54 -0
- package/esm/point/point.d.ts.map +1 -0
- package/esm/point/point.js +61 -0
- package/esm/rectangle.d.ts +125 -0
- package/esm/rectangle.d.ts.map +1 -0
- package/esm/rectangle.js +142 -0
- package/esm/vector/mod.d.ts +6 -0
- package/esm/vector/mod.d.ts.map +1 -0
- package/esm/vector/mod.js +4 -0
- package/esm/vector/vector.d.ts +69 -0
- package/esm/vector/vector.d.ts.map +1 -0
- package/esm/vector/vector.js +84 -0
- package/package.json +33 -0
- package/script/bounding_box.d.ts +12 -0
- package/script/bounding_box.d.ts.map +1 -0
- package/script/bounding_box.js +27 -0
- package/script/circle.d.ts +47 -0
- package/script/circle.d.ts.map +1 -0
- package/script/circle.js +65 -0
- package/script/distance.d.ts +31 -0
- package/script/distance.d.ts.map +1 -0
- package/script/distance.js +43 -0
- package/script/intersection.d.ts +32 -0
- package/script/intersection.d.ts.map +1 -0
- package/script/intersection.js +83 -0
- package/script/line.d.ts +39 -0
- package/script/line.d.ts.map +1 -0
- package/script/line.js +79 -0
- package/script/mod.d.ts +18 -0
- package/script/mod.d.ts.map +1 -0
- package/script/mod.js +79 -0
- package/script/package.json +3 -0
- package/script/point/mod.d.ts +6 -0
- package/script/point/mod.d.ts.map +1 -0
- package/script/point/mod.js +15 -0
- package/script/point/point.d.ts +54 -0
- package/script/point/point.d.ts.map +1 -0
- package/script/point/point.js +71 -0
- package/script/rectangle.d.ts +125 -0
- package/script/rectangle.d.ts.map +1 -0
- package/script/rectangle.js +162 -0
- package/script/vector/mod.d.ts +6 -0
- package/script/vector/mod.d.ts.map +1 -0
- package/script/vector/mod.js +20 -0
- package/script/vector/vector.d.ts +69 -0
- package/script/vector/vector.d.ts.map +1 -0
- package/script/vector/vector.js +99 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Point2D entity - represents a point in 2D space.
|
|
3
|
+
* x typically represents horizontal position (column in grids)
|
|
4
|
+
* y typically represents vertical position (row in grids)
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* A point in 2D space using x/y coordinates.
|
|
8
|
+
*/
|
|
9
|
+
type Point2D = {
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Creates a point from x/y coordinates.
|
|
15
|
+
*/
|
|
16
|
+
declare const createPoint: (x: number, y: number) => Point2D;
|
|
17
|
+
/**
|
|
18
|
+
* Creates a point at the origin (0, 0).
|
|
19
|
+
*/
|
|
20
|
+
declare const createOrigin: () => Point2D;
|
|
21
|
+
/**
|
|
22
|
+
* Translates a point by the given offset.
|
|
23
|
+
*/
|
|
24
|
+
declare const translatePoint: (point: Point2D, dx: number, dy: number) => Point2D;
|
|
25
|
+
/**
|
|
26
|
+
* Checks if two points are equal.
|
|
27
|
+
*/
|
|
28
|
+
declare const pointsEqual: (a: Point2D, b: Point2D) => boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Linearly interpolates between two points.
|
|
31
|
+
* t=0 returns a, t=1 returns b. Values outside [0,1] extrapolate.
|
|
32
|
+
*/
|
|
33
|
+
declare const lerpPoint: (a: Point2D, b: Point2D, t: number) => Point2D;
|
|
34
|
+
/**
|
|
35
|
+
* Returns the midpoint between two points.
|
|
36
|
+
*/
|
|
37
|
+
declare const midpoint: (a: Point2D, b: Point2D) => Point2D;
|
|
38
|
+
/**
|
|
39
|
+
* Rotates a point around an origin by the given angle in radians.
|
|
40
|
+
* Defaults to rotating around (0, 0).
|
|
41
|
+
*/
|
|
42
|
+
declare const rotatePoint: (point: Point2D, angle: number, origin?: Point2D) => Point2D;
|
|
43
|
+
/**
|
|
44
|
+
* Clamps a point to be within the given rectangular bounds.
|
|
45
|
+
*/
|
|
46
|
+
declare const clampPoint: (point: Point2D, bounds: {
|
|
47
|
+
x: number;
|
|
48
|
+
y: number;
|
|
49
|
+
width: number;
|
|
50
|
+
height: number;
|
|
51
|
+
}) => Point2D;
|
|
52
|
+
export type { Point2D };
|
|
53
|
+
export { createPoint, createOrigin, translatePoint, pointsEqual, lerpPoint, midpoint, rotatePoint, clampPoint };
|
|
54
|
+
//# sourceMappingURL=point.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"point.d.ts","sourceRoot":"","sources":["../../src/point/point.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;GAEG;AACH,KAAK,OAAO,GAAG;IACd,CAAC,EAAE,MAAM,CAAA;IACT,CAAC,EAAE,MAAM,CAAA;CACT,CAAA;AAED;;GAEG;AACH,QAAA,MAAM,WAAW,GAAI,GAAG,MAAM,EAAE,GAAG,MAAM,KAAG,OAAqB,CAAA;AAEjE;;GAEG;AACH,QAAA,MAAM,YAAY,QAAO,OAA2B,CAAA;AAEpD;;GAEG;AACH,QAAA,MAAM,cAAc,GAAI,OAAO,OAAO,EAAE,IAAI,MAAM,EAAE,IAAI,MAAM,KAAG,OAG/D,CAAA;AAEF;;GAEG;AACH,QAAA,MAAM,WAAW,GAAI,GAAG,OAAO,EAAE,GAAG,OAAO,KAAG,OAAqC,CAAA;AAEnF;;;GAGG;AACH,QAAA,MAAM,SAAS,GAAI,GAAG,OAAO,EAAE,GAAG,OAAO,EAAE,GAAG,MAAM,KAAG,OAGrD,CAAA;AAEF;;GAEG;AACH,QAAA,MAAM,QAAQ,GAAI,GAAG,OAAO,EAAE,GAAG,OAAO,KAAG,OAGzC,CAAA;AAEF;;;GAGG;AACH,QAAA,MAAM,WAAW,GAAI,OAAO,OAAO,EAAE,OAAO,MAAM,EAAE,SAAQ,OAAwB,KAAG,OAStF,CAAA;AAED;;GAEG;AACH,QAAA,MAAM,UAAU,GACf,OAAO,OAAO,EACd,QAAQ;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,KAC7D,OAGD,CAAA;AAEF,YAAY,EAAE,OAAO,EAAE,CAAA;AACvB,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,cAAc,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAE,WAAW,EAAE,UAAU,EAAE,CAAA"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Point2D entity - represents a point in 2D space.
|
|
3
|
+
* x typically represents horizontal position (column in grids)
|
|
4
|
+
* y typically represents vertical position (row in grids)
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Creates a point from x/y coordinates.
|
|
8
|
+
*/
|
|
9
|
+
const createPoint = (x, y) => ({ x, y });
|
|
10
|
+
/**
|
|
11
|
+
* Creates a point at the origin (0, 0).
|
|
12
|
+
*/
|
|
13
|
+
const createOrigin = () => ({ x: 0, y: 0 });
|
|
14
|
+
/**
|
|
15
|
+
* Translates a point by the given offset.
|
|
16
|
+
*/
|
|
17
|
+
const translatePoint = (point, dx, dy) => ({
|
|
18
|
+
x: point.x + dx,
|
|
19
|
+
y: point.y + dy,
|
|
20
|
+
});
|
|
21
|
+
/**
|
|
22
|
+
* Checks if two points are equal.
|
|
23
|
+
*/
|
|
24
|
+
const pointsEqual = (a, b) => a.x === b.x && a.y === b.y;
|
|
25
|
+
/**
|
|
26
|
+
* Linearly interpolates between two points.
|
|
27
|
+
* t=0 returns a, t=1 returns b. Values outside [0,1] extrapolate.
|
|
28
|
+
*/
|
|
29
|
+
const lerpPoint = (a, b, t) => ({
|
|
30
|
+
x: a.x + (b.x - a.x) * t,
|
|
31
|
+
y: a.y + (b.y - a.y) * t,
|
|
32
|
+
});
|
|
33
|
+
/**
|
|
34
|
+
* Returns the midpoint between two points.
|
|
35
|
+
*/
|
|
36
|
+
const midpoint = (a, b) => ({
|
|
37
|
+
x: (a.x + b.x) / 2,
|
|
38
|
+
y: (a.y + b.y) / 2,
|
|
39
|
+
});
|
|
40
|
+
/**
|
|
41
|
+
* Rotates a point around an origin by the given angle in radians.
|
|
42
|
+
* Defaults to rotating around (0, 0).
|
|
43
|
+
*/
|
|
44
|
+
const rotatePoint = (point, angle, origin = { x: 0, y: 0 }) => {
|
|
45
|
+
const cos = Math.cos(angle);
|
|
46
|
+
const sin = Math.sin(angle);
|
|
47
|
+
const dx = point.x - origin.x;
|
|
48
|
+
const dy = point.y - origin.y;
|
|
49
|
+
return {
|
|
50
|
+
x: origin.x + dx * cos - dy * sin,
|
|
51
|
+
y: origin.y + dx * sin + dy * cos,
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Clamps a point to be within the given rectangular bounds.
|
|
56
|
+
*/
|
|
57
|
+
const clampPoint = (point, bounds) => ({
|
|
58
|
+
x: Math.max(bounds.x, Math.min(point.x, bounds.x + bounds.width)),
|
|
59
|
+
y: Math.max(bounds.y, Math.min(point.y, bounds.y + bounds.height)),
|
|
60
|
+
});
|
|
61
|
+
export { createPoint, createOrigin, translatePoint, pointsEqual, lerpPoint, midpoint, rotatePoint, 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.d.ts.map
|
|
@@ -0,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,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"}
|
package/esm/rectangle.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rectangle entity - represents a rectangle in 2D space.
|
|
3
|
+
*/
|
|
4
|
+
import { createPoint } from './point/mod.js';
|
|
5
|
+
/**
|
|
6
|
+
* Creates a rectangle from position and dimensions.
|
|
7
|
+
*/
|
|
8
|
+
const createRectangle = (x, y, width, height) => ({
|
|
9
|
+
x,
|
|
10
|
+
y,
|
|
11
|
+
width,
|
|
12
|
+
height,
|
|
13
|
+
});
|
|
14
|
+
/**
|
|
15
|
+
* Creates a rectangle from two corner points.
|
|
16
|
+
*/
|
|
17
|
+
const createRectangleFromPoints = (topLeft, bottomRight) => ({
|
|
18
|
+
x: topLeft.x,
|
|
19
|
+
y: topLeft.y,
|
|
20
|
+
width: bottomRight.x - topLeft.x,
|
|
21
|
+
height: bottomRight.y - topLeft.y,
|
|
22
|
+
});
|
|
23
|
+
/**
|
|
24
|
+
* Gets the area of a rectangle.
|
|
25
|
+
*/
|
|
26
|
+
const getRectangleArea = (rect) => rect.width * rect.height;
|
|
27
|
+
/**
|
|
28
|
+
* Gets the perimeter of a rectangle.
|
|
29
|
+
*/
|
|
30
|
+
const getRectanglePerimeter = (rect) => 2 * (rect.width + rect.height);
|
|
31
|
+
/**
|
|
32
|
+
* Gets the center point of a rectangle.
|
|
33
|
+
*/
|
|
34
|
+
const getRectangleCenter = (rect) => ({
|
|
35
|
+
x: rect.x + rect.width / 2,
|
|
36
|
+
y: rect.y + rect.height / 2,
|
|
37
|
+
});
|
|
38
|
+
/**
|
|
39
|
+
* Checks if a point is inside a rectangle (inclusive of edges).
|
|
40
|
+
*/
|
|
41
|
+
const isPointInRectangle = (point, rect) => point.x >= rect.x &&
|
|
42
|
+
point.x <= rect.x + rect.width &&
|
|
43
|
+
point.y >= rect.y &&
|
|
44
|
+
point.y <= rect.y + rect.height;
|
|
45
|
+
/**
|
|
46
|
+
* Checks if two rectangles overlap.
|
|
47
|
+
*/
|
|
48
|
+
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);
|
|
49
|
+
/**
|
|
50
|
+
* Checks if rectangle a fully contains rectangle b.
|
|
51
|
+
*/
|
|
52
|
+
const rectangleContains = (outer, inner) => inner.x >= outer.x &&
|
|
53
|
+
inner.y >= outer.y &&
|
|
54
|
+
inner.x + inner.width <= outer.x + outer.width &&
|
|
55
|
+
inner.y + inner.height <= outer.y + outer.height;
|
|
56
|
+
/**
|
|
57
|
+
* Calculates the intersection of two rectangles.
|
|
58
|
+
* Returns null if rectangles don't overlap.
|
|
59
|
+
*/
|
|
60
|
+
const rectangleIntersection = (a, b) => {
|
|
61
|
+
const x = Math.max(a.x, b.x);
|
|
62
|
+
const y = Math.max(a.y, b.y);
|
|
63
|
+
const right = Math.min(a.x + a.width, b.x + b.width);
|
|
64
|
+
const bottom = Math.min(a.y + a.height, b.y + b.height);
|
|
65
|
+
if (right < x || bottom < y)
|
|
66
|
+
return null;
|
|
67
|
+
return createRectangle(x, y, right - x, bottom - y);
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Calculates the union (bounding box) of two rectangles.
|
|
71
|
+
*/
|
|
72
|
+
const rectangleUnion = (a, b) => {
|
|
73
|
+
const x = Math.min(a.x, b.x);
|
|
74
|
+
const y = Math.min(a.y, b.y);
|
|
75
|
+
const right = Math.max(a.x + a.width, b.x + b.width);
|
|
76
|
+
const bottom = Math.max(a.y + a.height, b.y + b.height);
|
|
77
|
+
return createRectangle(x, y, right - x, bottom - y);
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Calculate top-left position to center an inner element within a container.
|
|
81
|
+
* Returns the (x, y) coordinates where the inner element should be placed
|
|
82
|
+
* so that it is centered within the container.
|
|
83
|
+
*
|
|
84
|
+
* @param container - Object with width and height properties
|
|
85
|
+
* @param inner - Object with width and height properties
|
|
86
|
+
* @returns Point representing top-left position for centered placement
|
|
87
|
+
*/
|
|
88
|
+
const centerRectangleInContainer = (container, inner) => ({
|
|
89
|
+
x: Math.floor((container.width - inner.width) / 2),
|
|
90
|
+
y: Math.floor((container.height - inner.height) / 2),
|
|
91
|
+
});
|
|
92
|
+
/**
|
|
93
|
+
* Calculate horizontal center alignment position.
|
|
94
|
+
* Returns the x coordinate to position an element horizontally centered relative to a reference element.
|
|
95
|
+
*
|
|
96
|
+
* @param reference - Object with x and width properties (the element to center against)
|
|
97
|
+
* @param element - Object with width property (the element being positioned)
|
|
98
|
+
* @returns x coordinate for horizontally centered placement
|
|
99
|
+
*/
|
|
100
|
+
const centerHorizontally = (reference, element) => reference.x + reference.width / 2 - element.width / 2;
|
|
101
|
+
/**
|
|
102
|
+
* Calculate vertical center alignment position.
|
|
103
|
+
* Returns the y coordinate to position an element vertically centered relative to a reference element.
|
|
104
|
+
*
|
|
105
|
+
* @param reference - Object with y and height properties (the element to center against)
|
|
106
|
+
* @param element - Object with height property (the element being positioned)
|
|
107
|
+
* @returns y coordinate for vertically centered placement
|
|
108
|
+
*/
|
|
109
|
+
const centerVertically = (reference, element) => reference.y + reference.height / 2 - element.height / 2;
|
|
110
|
+
/**
|
|
111
|
+
* Moves a rectangle by the given offset.
|
|
112
|
+
*/
|
|
113
|
+
const translateRectangle = (rect, dx, dy) => createRectangle(rect.x + dx, rect.y + dy, rect.width, rect.height);
|
|
114
|
+
/**
|
|
115
|
+
* Grows a rectangle uniformly by the given amount (padding).
|
|
116
|
+
* Each side expands outward by `amount`. Negative values shrink.
|
|
117
|
+
*/
|
|
118
|
+
const expandRectangle = (rect, amount) => createRectangle(rect.x - amount, rect.y - amount, rect.width + 2 * amount, rect.height + 2 * amount);
|
|
119
|
+
/**
|
|
120
|
+
* Shrinks a rectangle uniformly by the given amount (margin).
|
|
121
|
+
* Each side moves inward by `amount`. Negative values expand.
|
|
122
|
+
*/
|
|
123
|
+
const insetRectangle = (rect, amount) => createRectangle(rect.x + amount, rect.y + amount, rect.width - 2 * amount, rect.height - 2 * amount);
|
|
124
|
+
/**
|
|
125
|
+
* Scales a rectangle by a factor from an origin point.
|
|
126
|
+
* Default origin is the rectangle's center.
|
|
127
|
+
*/
|
|
128
|
+
const scaleRectangle = (rect, factor, origin) => {
|
|
129
|
+
const scaleOrigin = origin ?? getRectangleCenter(rect);
|
|
130
|
+
return createRectangle(scaleOrigin.x + (rect.x - scaleOrigin.x) * factor, scaleOrigin.y + (rect.y - scaleOrigin.y) * factor, rect.width * factor, rect.height * factor);
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* Returns the 4 corner points of a rectangle in clockwise order:
|
|
134
|
+
* topLeft, topRight, bottomRight, bottomLeft.
|
|
135
|
+
*/
|
|
136
|
+
const getRectangleCorners = (rect) => [
|
|
137
|
+
createPoint(rect.x, rect.y),
|
|
138
|
+
createPoint(rect.x + rect.width, rect.y),
|
|
139
|
+
createPoint(rect.x + rect.width, rect.y + rect.height),
|
|
140
|
+
createPoint(rect.x, rect.y + rect.height),
|
|
141
|
+
];
|
|
142
|
+
export { createRectangle, createRectangleFromPoints, getRectangleArea, getRectanglePerimeter, getRectangleCenter, isPointInRectangle, rectanglesOverlap, rectangleContains, rectangleIntersection, rectangleUnion, centerRectangleInContainer, centerHorizontally, centerVertically, translateRectangle, expandRectangle, insetRectangle, scaleRectangle, 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,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=v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AAG,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,84 @@
|
|
|
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
|
+
* Creates a vector from x/y components.
|
|
7
|
+
*/
|
|
8
|
+
const createVector = (x, y) => ({ x, y });
|
|
9
|
+
/**
|
|
10
|
+
* Creates a zero vector (0, 0).
|
|
11
|
+
*/
|
|
12
|
+
const createZeroVector = () => ({ x: 0, y: 0 });
|
|
13
|
+
/**
|
|
14
|
+
* Calculates the magnitude (length) of a vector.
|
|
15
|
+
*/
|
|
16
|
+
const vectorMagnitude = (vector) => Math.sqrt(vector.x * vector.x + vector.y * vector.y);
|
|
17
|
+
/**
|
|
18
|
+
* Adds two vectors.
|
|
19
|
+
*/
|
|
20
|
+
const addVectors = (a, b) => ({
|
|
21
|
+
x: a.x + b.x,
|
|
22
|
+
y: a.y + b.y,
|
|
23
|
+
});
|
|
24
|
+
/**
|
|
25
|
+
* Subtracts vector b from vector a.
|
|
26
|
+
*/
|
|
27
|
+
const subtractVectors = (a, b) => ({
|
|
28
|
+
x: a.x - b.x,
|
|
29
|
+
y: a.y - b.y,
|
|
30
|
+
});
|
|
31
|
+
/**
|
|
32
|
+
* Scales a vector by a scalar value.
|
|
33
|
+
*/
|
|
34
|
+
const scaleVector = (vector, scalar) => ({
|
|
35
|
+
x: vector.x * scalar,
|
|
36
|
+
y: vector.y * scalar,
|
|
37
|
+
});
|
|
38
|
+
/**
|
|
39
|
+
* Normalizes a vector to unit length.
|
|
40
|
+
* Returns zero vector if input vector has zero length.
|
|
41
|
+
*/
|
|
42
|
+
const normalizeVector = (vector) => {
|
|
43
|
+
const magnitude = vectorMagnitude(vector);
|
|
44
|
+
if (magnitude === 0)
|
|
45
|
+
return createZeroVector();
|
|
46
|
+
return scaleVector(vector, 1 / magnitude);
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Returns the dot product of two vectors.
|
|
50
|
+
*/
|
|
51
|
+
const dotProduct = (a, b) => a.x * b.x + a.y * b.y;
|
|
52
|
+
/**
|
|
53
|
+
* Returns the z-component of the 2D cross product.
|
|
54
|
+
* Positive = b is counter-clockwise from a, negative = clockwise, zero = parallel.
|
|
55
|
+
*/
|
|
56
|
+
const crossProduct2D = (a, b) => a.x * b.y - a.y * b.x;
|
|
57
|
+
/**
|
|
58
|
+
* Returns a vector rotated 90° counter-clockwise.
|
|
59
|
+
*/
|
|
60
|
+
const perpendicular = (v) => ({ x: v.y === 0 ? 0 : -v.y, y: v.x });
|
|
61
|
+
/**
|
|
62
|
+
* Creates a vector from a polar angle and optional magnitude (default 1).
|
|
63
|
+
* Angle is in radians.
|
|
64
|
+
*/
|
|
65
|
+
const vectorFromAngle = (angle, magnitude = 1) => ({
|
|
66
|
+
x: Math.cos(angle) * magnitude,
|
|
67
|
+
y: Math.sin(angle) * magnitude,
|
|
68
|
+
});
|
|
69
|
+
/**
|
|
70
|
+
* Returns the polar angle of a vector in radians.
|
|
71
|
+
*/
|
|
72
|
+
const vectorToAngle = (v) => Math.atan2(v.y, v.x);
|
|
73
|
+
/**
|
|
74
|
+
* Rotates a vector by an angle in radians.
|
|
75
|
+
*/
|
|
76
|
+
const rotateVector = (v, angle) => {
|
|
77
|
+
const cos = Math.cos(angle);
|
|
78
|
+
const sin = Math.sin(angle);
|
|
79
|
+
return {
|
|
80
|
+
x: v.x * cos - v.y * sin,
|
|
81
|
+
y: v.x * sin + v.y * cos,
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
export { createVector, createZeroVector, vectorMagnitude, addVectors, subtractVectors, scaleVector, normalizeVector, dotProduct, crossProduct2D, perpendicular, vectorFromAngle, vectorToAngle, rotateVector, };
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wentools/spatial",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Pure 2D geometry primitives — points, vectors, rectangles, circles, lines, and distance metrics",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"geometry",
|
|
7
|
+
"2d",
|
|
8
|
+
"spatial",
|
|
9
|
+
"point",
|
|
10
|
+
"vector",
|
|
11
|
+
"rectangle",
|
|
12
|
+
"circle",
|
|
13
|
+
"line",
|
|
14
|
+
"distance",
|
|
15
|
+
"typescript"
|
|
16
|
+
],
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://gitlab.com/wentools/spatial"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"main": "./script/mod.js",
|
|
23
|
+
"module": "./esm/mod.js",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"import": "./esm/mod.js",
|
|
27
|
+
"require": "./script/mod.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"scripts": {},
|
|
31
|
+
"peerDependencies": {},
|
|
32
|
+
"_generatedBy": "dnt@dev"
|
|
33
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounding box calculation for collections of points.
|
|
3
|
+
*/
|
|
4
|
+
import type { Point2D } from './point/mod.js';
|
|
5
|
+
import type { Rectangle } from './rectangle.js';
|
|
6
|
+
/**
|
|
7
|
+
* Calculates the bounding box for a set of points.
|
|
8
|
+
* Returns null if no points provided.
|
|
9
|
+
*/
|
|
10
|
+
declare const getBoundingBox: (points: Point2D[]) => Rectangle | null;
|
|
11
|
+
export { getBoundingBox };
|
|
12
|
+
//# sourceMappingURL=bounding_box.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bounding_box.d.ts","sourceRoot":"","sources":["../src/bounding_box.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAG/C;;;GAGG;AACH,QAAA,MAAM,cAAc,GAAI,QAAQ,OAAO,EAAE,KAAG,SAAS,GAAG,IAgBvD,CAAA;AAED,OAAO,EAAE,cAAc,EAAE,CAAA"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Bounding box calculation for collections of points.
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getBoundingBox = void 0;
|
|
7
|
+
const rectangle_js_1 = require("./rectangle.js");
|
|
8
|
+
/**
|
|
9
|
+
* Calculates the bounding box for a set of points.
|
|
10
|
+
* Returns null if no points provided.
|
|
11
|
+
*/
|
|
12
|
+
const getBoundingBox = (points) => {
|
|
13
|
+
if (points.length === 0)
|
|
14
|
+
return null;
|
|
15
|
+
let minX = Infinity;
|
|
16
|
+
let maxX = -Infinity;
|
|
17
|
+
let minY = Infinity;
|
|
18
|
+
let maxY = -Infinity;
|
|
19
|
+
for (const point of points) {
|
|
20
|
+
minX = Math.min(minX, point.x);
|
|
21
|
+
maxX = Math.max(maxX, point.x);
|
|
22
|
+
minY = Math.min(minY, point.y);
|
|
23
|
+
maxY = Math.max(maxY, point.y);
|
|
24
|
+
}
|
|
25
|
+
return (0, rectangle_js_1.createRectangle)(minX, minY, maxX - minX, maxY - minY);
|
|
26
|
+
};
|
|
27
|
+
exports.getBoundingBox = getBoundingBox;
|