@gitborlando/geo 1.0.1
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/CHANGELOG.md +7 -0
- package/__test__/aabb.test.ts +84 -0
- package/__test__/angle.test.ts +58 -0
- package/__test__/index.test.ts +85 -0
- package/__test__/math.test.ts +61 -0
- package/__test__/matrix.test.ts +73 -0
- package/__test__/obb.test.ts +95 -0
- package/__test__/points-of-bezier.test.ts +103 -0
- package/__test__/types.test.ts +31 -0
- package/__test__/xy.test.ts +143 -0
- package/dist/index.d.ts +245 -0
- package/dist/index.js +558 -0
- package/package.json +31 -0
- package/src/aabb.ts +89 -0
- package/src/angle.ts +55 -0
- package/src/index.ts +8 -0
- package/src/math.ts +22 -0
- package/src/matrix.ts +48 -0
- package/src/obb.ts +108 -0
- package/src/points-of-bezier.ts +156 -0
- package/src/types.ts +16 -0
- package/src/xy.ts +185 -0
- package/tsup.config.ts +10 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import {
|
|
3
|
+
XY,
|
|
4
|
+
xy_,
|
|
5
|
+
xy_center,
|
|
6
|
+
xy_client,
|
|
7
|
+
xy_distance,
|
|
8
|
+
xy_divide,
|
|
9
|
+
xy_dot,
|
|
10
|
+
xy_from,
|
|
11
|
+
xy_minus,
|
|
12
|
+
xy_multiply,
|
|
13
|
+
xy_mutate,
|
|
14
|
+
xy_opposite,
|
|
15
|
+
xy_plus,
|
|
16
|
+
xy_toArray,
|
|
17
|
+
} from '../src/xy'
|
|
18
|
+
|
|
19
|
+
describe('XY utilities', () => {
|
|
20
|
+
it('should create xy point correctly', () => {
|
|
21
|
+
const point = xy_(10, 20)
|
|
22
|
+
expect(point.x).toBe(10)
|
|
23
|
+
expect(point.y).toBe(20)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('should create xy from client event correctly', () => {
|
|
27
|
+
const event = { clientX: 100, clientY: 200 }
|
|
28
|
+
const point = xy_client(event)
|
|
29
|
+
expect(point.x).toBe(100)
|
|
30
|
+
expect(point.y).toBe(200)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('should copy xy point correctly', () => {
|
|
34
|
+
const original = { x: 5, y: 10 }
|
|
35
|
+
const copy = xy_from(original)
|
|
36
|
+
expect(copy.x).toBe(5)
|
|
37
|
+
expect(copy.y).toBe(10)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('should get center point correctly', () => {
|
|
41
|
+
const rect = { centerX: 50, centerY: 75 }
|
|
42
|
+
const center = xy_center(rect)
|
|
43
|
+
expect(center.x).toBe(50)
|
|
44
|
+
expect(center.y).toBe(75)
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('should mutate xy point correctly', () => {
|
|
48
|
+
const point1 = { x: 1, y: 2 }
|
|
49
|
+
const point2 = { x: 3, y: 4 }
|
|
50
|
+
xy_mutate(point1, point2)
|
|
51
|
+
expect(point1.x).toBe(3)
|
|
52
|
+
expect(point1.y).toBe(4)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('should add xy points correctly', () => {
|
|
56
|
+
const point1 = { x: 1, y: 2 }
|
|
57
|
+
const point2 = { x: 3, y: 4 }
|
|
58
|
+
const result = xy_plus(point1, point2)
|
|
59
|
+
expect(result.x).toBe(4)
|
|
60
|
+
expect(result.y).toBe(6)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('should subtract xy points correctly', () => {
|
|
64
|
+
const point1 = { x: 5, y: 8 }
|
|
65
|
+
const point2 = { x: 2, y: 3 }
|
|
66
|
+
const result = xy_minus(point1, point2)
|
|
67
|
+
expect(result.x).toBe(3)
|
|
68
|
+
expect(result.y).toBe(5)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it('should multiply xy point correctly', () => {
|
|
72
|
+
const point = { x: 2, y: 3 }
|
|
73
|
+
const result = xy_multiply(point, 2, 3)
|
|
74
|
+
expect(result.x).toBe(12)
|
|
75
|
+
expect(result.y).toBe(18)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('should divide xy point correctly', () => {
|
|
79
|
+
const point = { x: 12, y: 18 }
|
|
80
|
+
const result = xy_divide(point, 2, 3)
|
|
81
|
+
expect(result.x).toBe(2)
|
|
82
|
+
expect(result.y).toBe(3)
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('should calculate distance correctly', () => {
|
|
86
|
+
const point1 = { x: 0, y: 0 }
|
|
87
|
+
const point2 = { x: 3, y: 4 }
|
|
88
|
+
const distance = xy_distance(point1, point2)
|
|
89
|
+
expect(distance).toBe(5)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('should calculate dot product correctly', () => {
|
|
93
|
+
const point1 = { x: 2, y: 3 }
|
|
94
|
+
const point2 = { x: 4, y: 5 }
|
|
95
|
+
const dot = xy_dot(point1, point2)
|
|
96
|
+
expect(dot).toBe(23) // 2*4 + 3*5 = 8 + 15 = 23
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('should get opposite point correctly', () => {
|
|
100
|
+
const point = { x: 3, y: -4 }
|
|
101
|
+
const opposite = xy_opposite(point)
|
|
102
|
+
expect(opposite.x).toBe(-3)
|
|
103
|
+
expect(opposite.y).toBe(4)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('should convert to array correctly', () => {
|
|
107
|
+
const point = { x: 7, y: 11 }
|
|
108
|
+
const array = xy_toArray(point)
|
|
109
|
+
expect(array).toEqual([7, 11])
|
|
110
|
+
})
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
describe('XY class', () => {
|
|
114
|
+
it('should create XY instance correctly', () => {
|
|
115
|
+
const xy = new XY(10, 20)
|
|
116
|
+
expect(xy.x).toBe(10)
|
|
117
|
+
expect(xy.y).toBe(20)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
it('should create from point correctly', () => {
|
|
121
|
+
const xy = XY.From({ x: 5, y: 10 })
|
|
122
|
+
expect(xy.x).toBe(5)
|
|
123
|
+
expect(xy.y).toBe(10)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('should create from array correctly', () => {
|
|
127
|
+
const xy = XY.FromArray([15, 25])
|
|
128
|
+
expect(xy.x).toBe(15)
|
|
129
|
+
expect(xy.y).toBe(25)
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
it('should perform chained operations correctly', () => {
|
|
133
|
+
const xy = new XY(2, 3).plus({ x: 1, y: 2 }).multiply(2)
|
|
134
|
+
expect(xy.x).toBe(6) // (2+1)*2 = 6
|
|
135
|
+
expect(xy.y).toBe(10) // (3+2)*2 = 10
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
it('should calculate distance correctly', () => {
|
|
139
|
+
const xy = new XY(0, 0)
|
|
140
|
+
const distance = xy.distance({ x: 3, y: 4 })
|
|
141
|
+
expect(distance).toBe(5)
|
|
142
|
+
})
|
|
143
|
+
})
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
interface IXY {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
}
|
|
5
|
+
interface IRect {
|
|
6
|
+
x: number;
|
|
7
|
+
y: number;
|
|
8
|
+
width: number;
|
|
9
|
+
height: number;
|
|
10
|
+
}
|
|
11
|
+
interface IRectWithCenter extends IRect {
|
|
12
|
+
centerX: number;
|
|
13
|
+
centerY: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type IAxis = {
|
|
17
|
+
widthAxis: IXY;
|
|
18
|
+
heightAxis: IXY;
|
|
19
|
+
};
|
|
20
|
+
declare class OBB {
|
|
21
|
+
#private;
|
|
22
|
+
x: number;
|
|
23
|
+
y: number;
|
|
24
|
+
width: number;
|
|
25
|
+
height: number;
|
|
26
|
+
rotation: number;
|
|
27
|
+
center: IXY;
|
|
28
|
+
axis: IAxis;
|
|
29
|
+
aabb: AABB;
|
|
30
|
+
vertexes: [IXY, IXY, IXY, IXY];
|
|
31
|
+
constructor(x: number, y: number, width: number, height: number, rotation: number);
|
|
32
|
+
get xy(): {
|
|
33
|
+
x: number;
|
|
34
|
+
y: number;
|
|
35
|
+
};
|
|
36
|
+
calcVertexXY: () => [{
|
|
37
|
+
x: number;
|
|
38
|
+
y: number;
|
|
39
|
+
}, {
|
|
40
|
+
x: number;
|
|
41
|
+
y: number;
|
|
42
|
+
}, {
|
|
43
|
+
x: number;
|
|
44
|
+
y: number;
|
|
45
|
+
}, {
|
|
46
|
+
x: number;
|
|
47
|
+
y: number;
|
|
48
|
+
}];
|
|
49
|
+
clone: () => OBB;
|
|
50
|
+
projectionLengthAt: (anotherAxis: IXY) => number;
|
|
51
|
+
collide: (another: OBB) => boolean;
|
|
52
|
+
static IdentityOBB(): OBB;
|
|
53
|
+
static FromRect(rect: IRect, rotation?: number): OBB;
|
|
54
|
+
static FromAABB(aabb: AABB): OBB;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
declare class AABB {
|
|
58
|
+
minX: number;
|
|
59
|
+
minY: number;
|
|
60
|
+
maxX: number;
|
|
61
|
+
maxY: number;
|
|
62
|
+
constructor(minX: number, minY: number, maxX: number, maxY: number);
|
|
63
|
+
static Rect(aabb: AABB): IRectWithCenter;
|
|
64
|
+
static Collide(one: AABB, another: AABB): boolean;
|
|
65
|
+
static Include(one: AABB, another: AABB): number;
|
|
66
|
+
static Expand(aabb: AABB, ...expands: [number] | [number, number, number, number]): AABB;
|
|
67
|
+
static Merge(...aabbList: AABB[]): AABB;
|
|
68
|
+
static FromOBB(obb: OBB): AABB;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
declare const PI: number;
|
|
72
|
+
declare const cos: (x: number) => number;
|
|
73
|
+
declare const sin: (x: number) => number;
|
|
74
|
+
declare const tan: (x: number) => number;
|
|
75
|
+
declare const acos: (x: number) => number;
|
|
76
|
+
declare const asin: (x: number) => number;
|
|
77
|
+
declare const atan: (x: number) => number;
|
|
78
|
+
declare const atan2: (y: number, x: number) => number;
|
|
79
|
+
declare class Angle {
|
|
80
|
+
static Cos(angle: number): number;
|
|
81
|
+
static Sin(angle: number): number;
|
|
82
|
+
static Tan(angle: number): number;
|
|
83
|
+
static ACos(angle: number): number;
|
|
84
|
+
static ASin(angle: number): number;
|
|
85
|
+
static ATan(angle: number): number;
|
|
86
|
+
static ATan2(y: number, x: number): number;
|
|
87
|
+
static AngleFy(radians: number): number;
|
|
88
|
+
static RadianFy(angle: number): number;
|
|
89
|
+
static Normal(angle: number): number;
|
|
90
|
+
static Snap(angle: number, step?: number): number;
|
|
91
|
+
static RotatePoint(ax: number, ay: number, ox: number, oy: number, angle: number): {
|
|
92
|
+
x: number;
|
|
93
|
+
y: number;
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
declare const sqrt: (x: number) => number;
|
|
98
|
+
declare const abs: (x: number) => number;
|
|
99
|
+
declare const min: (...values: number[]) => number;
|
|
100
|
+
declare const max: (...values: number[]) => number;
|
|
101
|
+
declare const round: (x: number) => number;
|
|
102
|
+
declare const floor: (x: number) => number;
|
|
103
|
+
declare const ceil: (x: number) => number;
|
|
104
|
+
declare const random: () => number;
|
|
105
|
+
declare function pow2(number: number): number;
|
|
106
|
+
declare function pow3(number: number): number;
|
|
107
|
+
declare function multiply(...numbers: number[]): number;
|
|
108
|
+
declare function divide(a: number, b: number): number;
|
|
109
|
+
declare function numberHalfFix(number: number): number;
|
|
110
|
+
|
|
111
|
+
type IMatrix = [number, number, number, number, number, number];
|
|
112
|
+
declare class Matrix {
|
|
113
|
+
static Create(): IMatrix;
|
|
114
|
+
static Invert(matrix: IMatrix): IMatrix;
|
|
115
|
+
static ApplyPoint(xy: IXY, matrix: IMatrix): {
|
|
116
|
+
x: number;
|
|
117
|
+
y: number;
|
|
118
|
+
};
|
|
119
|
+
static ApplyAABB(aabb: AABB, matrix: IMatrix): {
|
|
120
|
+
minX: number;
|
|
121
|
+
minY: number;
|
|
122
|
+
maxX: number;
|
|
123
|
+
maxY: number;
|
|
124
|
+
};
|
|
125
|
+
static InvertPoint(xy: IXY, matrix: IMatrix): {
|
|
126
|
+
x: number;
|
|
127
|
+
y: number;
|
|
128
|
+
};
|
|
129
|
+
static InvertAABB(aabb: AABB, matrix: IMatrix): {
|
|
130
|
+
minX: number;
|
|
131
|
+
minY: number;
|
|
132
|
+
maxX: number;
|
|
133
|
+
maxY: number;
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
type Point = {
|
|
138
|
+
x: number;
|
|
139
|
+
y: number;
|
|
140
|
+
};
|
|
141
|
+
declare function simplify(points: readonly Point[], distance: number): Point[];
|
|
142
|
+
declare function simplifyPoints(points: readonly Point[], start: number, end: number, epsilon: number, newPoints?: Point[]): Point[];
|
|
143
|
+
declare function pointsOnBezierCurves(points: readonly Point[], tolerance?: number, distance?: number): Point[];
|
|
144
|
+
|
|
145
|
+
declare function xy_(x?: number, y?: number): {
|
|
146
|
+
x: number;
|
|
147
|
+
y: number;
|
|
148
|
+
};
|
|
149
|
+
declare function xy_client(e: any): {
|
|
150
|
+
x: any;
|
|
151
|
+
y: any;
|
|
152
|
+
};
|
|
153
|
+
declare function xy_from(xy: IXY): {
|
|
154
|
+
x: number;
|
|
155
|
+
y: number;
|
|
156
|
+
};
|
|
157
|
+
declare function xy_center(xy: {
|
|
158
|
+
centerX: number;
|
|
159
|
+
centerY: number;
|
|
160
|
+
}): {
|
|
161
|
+
x: number;
|
|
162
|
+
y: number;
|
|
163
|
+
};
|
|
164
|
+
declare function xy_mutate(self: IXY, another: IXY): void;
|
|
165
|
+
declare function xy_plus(self: IXY, another: IXY): {
|
|
166
|
+
x: number;
|
|
167
|
+
y: number;
|
|
168
|
+
};
|
|
169
|
+
declare function xy_plus_mutate(self: IXY, another: IXY): void;
|
|
170
|
+
declare function xy_plus_all(...xys: IXY[]): IXY;
|
|
171
|
+
declare function xy_minus(self: IXY, another: IXY): {
|
|
172
|
+
x: number;
|
|
173
|
+
y: number;
|
|
174
|
+
};
|
|
175
|
+
declare function xy_minus_mutate(self: IXY, another: IXY): void;
|
|
176
|
+
declare function xy_multiply(self: IXY, ...numbers: number[]): {
|
|
177
|
+
x: number;
|
|
178
|
+
y: number;
|
|
179
|
+
};
|
|
180
|
+
declare function xy_multiply_mutate(self: IXY, ...numbers: number[]): void;
|
|
181
|
+
declare function xy_divide(self: IXY, ...numbers: number[]): {
|
|
182
|
+
x: number;
|
|
183
|
+
y: number;
|
|
184
|
+
};
|
|
185
|
+
declare function xy_distance(self: IXY, another?: IXY): number;
|
|
186
|
+
declare function xy_rotate(self: IXY, origin: IXY, rotation: number): {
|
|
187
|
+
x: number;
|
|
188
|
+
y: number;
|
|
189
|
+
};
|
|
190
|
+
declare function xy_dot(self: IXY, another: IXY): number;
|
|
191
|
+
declare function xy_symmetric(self: IXY, origin: IXY): {
|
|
192
|
+
x: number;
|
|
193
|
+
y: number;
|
|
194
|
+
};
|
|
195
|
+
declare function xy_opposite(self: IXY): {
|
|
196
|
+
x: number;
|
|
197
|
+
y: number;
|
|
198
|
+
};
|
|
199
|
+
declare function xy_getRotation(self: IXY, another: IXY, origin: IXY): number;
|
|
200
|
+
declare function xy_toArray(self: IXY): [number, number];
|
|
201
|
+
declare function xy_xAxis(rotation: number): {
|
|
202
|
+
x: number;
|
|
203
|
+
y: number;
|
|
204
|
+
};
|
|
205
|
+
declare function xy_yAxis(rotation: number): {
|
|
206
|
+
x: number;
|
|
207
|
+
y: number;
|
|
208
|
+
};
|
|
209
|
+
declare class XY {
|
|
210
|
+
x: number;
|
|
211
|
+
y: number;
|
|
212
|
+
constructor(x: number, y: number);
|
|
213
|
+
from(xy: IXY): this;
|
|
214
|
+
client(e: {
|
|
215
|
+
clientX: number;
|
|
216
|
+
clientY: number;
|
|
217
|
+
}): this;
|
|
218
|
+
center(xy: {
|
|
219
|
+
centerX: number;
|
|
220
|
+
centerY: number;
|
|
221
|
+
}): this;
|
|
222
|
+
plus(another: IXY): this;
|
|
223
|
+
minus(another: IXY): this;
|
|
224
|
+
multiply(...numbers: number[]): this;
|
|
225
|
+
divide(...numbers: number[]): this;
|
|
226
|
+
distance(another: IXY): number;
|
|
227
|
+
rotate(origin: IXY, rotation: number): {
|
|
228
|
+
x: number;
|
|
229
|
+
y: number;
|
|
230
|
+
};
|
|
231
|
+
dot(another: IXY): number;
|
|
232
|
+
opposite(): {
|
|
233
|
+
x: number;
|
|
234
|
+
y: number;
|
|
235
|
+
};
|
|
236
|
+
symmetric(another: IXY, origin: IXY): {
|
|
237
|
+
x: number;
|
|
238
|
+
y: number;
|
|
239
|
+
};
|
|
240
|
+
angle(another: IXY, origin: IXY): number;
|
|
241
|
+
static From(xy: IXY): XY;
|
|
242
|
+
static FromArray(arr: [number, number]): XY;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export { AABB, Angle, type IMatrix, type IRect, type IRectWithCenter, type IXY, Matrix, OBB, PI, type Point, XY, abs, acos, asin, atan, atan2, ceil, cos, divide, floor, max, min, multiply, numberHalfFix, pointsOnBezierCurves, pow2, pow3, random, round, simplify, simplifyPoints, sin, sqrt, tan, xy_, xy_center, xy_client, xy_distance, xy_divide, xy_dot, xy_from, xy_getRotation, xy_minus, xy_minus_mutate, xy_multiply, xy_multiply_mutate, xy_mutate, xy_opposite, xy_plus, xy_plus_all, xy_plus_mutate, xy_rotate, xy_symmetric, xy_toArray, xy_xAxis, xy_yAxis };
|