@gitborlando/geo 4.1.0 → 4.1.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.
@@ -0,0 +1,254 @@
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 fromCenter(center: IXY, width: number, height: number, rotation?: number): OBB;
55
+ static fromAABB(aabb: AABB): OBB;
56
+ }
57
+
58
+ declare class AABB {
59
+ minX: number;
60
+ minY: number;
61
+ maxX: number;
62
+ maxY: number;
63
+ constructor(minX: number, minY: number, maxX: number, maxY: number);
64
+ static rect(aabb: AABB): IRectWithCenter;
65
+ static rectTuple(aabb: AABB): readonly [number, number, number, number];
66
+ static collide(one: AABB, another: AABB): boolean;
67
+ static include(one: AABB, another: AABB): number;
68
+ static extend(aabb: AABB, ...expands: [number] | [number, number, number, number]): AABB;
69
+ static merge(...aabbList: AABB[]): AABB;
70
+ static fromOBB(obb: OBB): AABB;
71
+ }
72
+
73
+ declare const PI: number;
74
+ declare const cos: (x: number) => number;
75
+ declare const sin: (x: number) => number;
76
+ declare const tan: (x: number) => number;
77
+ declare const acos: (x: number) => number;
78
+ declare const asin: (x: number) => number;
79
+ declare const atan: (x: number) => number;
80
+ declare const atan2: (y: number, x: number) => number;
81
+ declare class Angle {
82
+ static cos(angle: number): number;
83
+ static sin(angle: number): number;
84
+ static tan(angle: number): number;
85
+ static acos(angle: number): number;
86
+ static asin(angle: number): number;
87
+ static atan(angle: number): number;
88
+ static atan2(y: number, x: number): number;
89
+ static angleFy(radians: number): number;
90
+ static radianFy(angle: number): number;
91
+ static normal(angle: number): number;
92
+ static snap(angle: number, step?: number): number;
93
+ static rotatePoint(ax: number, ay: number, ox: number, oy: number, angle: number): {
94
+ x: number;
95
+ y: number;
96
+ };
97
+ }
98
+
99
+ declare const sqrt: (x: number) => number;
100
+ declare const abs: (x: number) => number;
101
+ declare const min: (...values: number[]) => number;
102
+ declare const max: (...values: number[]) => number;
103
+ declare const round: (x: number) => number;
104
+ declare const floor: (x: number) => number;
105
+ declare const ceil: (x: number) => number;
106
+ declare const random: () => number;
107
+ declare function pow2(number: number): number;
108
+ declare function pow3(number: number): number;
109
+ declare function multiply(...numbers: number[]): number;
110
+ declare function divide(a: number, b: number): number;
111
+ declare function numberHalfFix(number: number): number;
112
+ declare function twoDecimal(number: number): number;
113
+
114
+ type IMatrix = [number, number, number, number, number, number];
115
+ declare class Matrix {
116
+ static create(): IMatrix;
117
+ static invert(matrix: IMatrix): IMatrix;
118
+ static applyPoint(xy: IXY, matrix: IMatrix): {
119
+ x: number;
120
+ y: number;
121
+ };
122
+ static applyAABB(aabb: AABB, matrix: IMatrix): {
123
+ minX: number;
124
+ minY: number;
125
+ maxX: number;
126
+ maxY: number;
127
+ };
128
+ static invertPoint(xy: IXY, matrix: IMatrix): {
129
+ x: number;
130
+ y: number;
131
+ };
132
+ static invertAABB(aabb: AABB, matrix: IMatrix): {
133
+ minX: number;
134
+ minY: number;
135
+ maxX: number;
136
+ maxY: number;
137
+ };
138
+ }
139
+
140
+ type Point = {
141
+ x: number;
142
+ y: number;
143
+ };
144
+ declare function simplify(points: readonly Point[], distance: number): Point[];
145
+ declare function simplifyPoints(points: readonly Point[], start: number, end: number, epsilon: number, newPoints?: Point[]): Point[];
146
+ declare function pointsOnBezierCurves(points: readonly Point[], tolerance?: number, distance?: number): Point[];
147
+
148
+ declare function xy_(x?: number, y?: number): {
149
+ x: number;
150
+ y: number;
151
+ };
152
+ declare function xy_client(e: any): {
153
+ x: any;
154
+ y: any;
155
+ };
156
+ declare function xy_from(xy: IXY): {
157
+ x: number;
158
+ y: number;
159
+ };
160
+ declare function xy_center(xy: {
161
+ centerX: number;
162
+ centerY: number;
163
+ }): {
164
+ x: number;
165
+ y: number;
166
+ };
167
+ declare function xy_mutate(self: IXY, another: IXY): void;
168
+ declare function xy_plus(self: IXY, another: IXY): {
169
+ x: number;
170
+ y: number;
171
+ };
172
+ declare function xy_plus_mutate(self: IXY, another: IXY): void;
173
+ declare function xy_plus_all(...xys: IXY[]): IXY;
174
+ declare function xy_minus(self: IXY, another: IXY): {
175
+ x: number;
176
+ y: number;
177
+ };
178
+ declare function xy_minus_mutate(self: IXY, another: IXY): void;
179
+ declare function xy_multiply(self: IXY, ...numbers: number[]): {
180
+ x: number;
181
+ y: number;
182
+ };
183
+ declare function xy_multiply_mutate(self: IXY, ...numbers: number[]): void;
184
+ declare function xy_divide(self: IXY, ...numbers: number[]): {
185
+ x: number;
186
+ y: number;
187
+ };
188
+ declare function xy_distance(self: IXY, another?: IXY): number;
189
+ declare function xy_rotate(self: IXY, origin: IXY, rotation: number): {
190
+ x: number;
191
+ y: number;
192
+ };
193
+ declare function xy_dot(self: IXY, another: IXY): number;
194
+ declare function xy_symmetric(self: IXY, origin: IXY): {
195
+ x: number;
196
+ y: number;
197
+ };
198
+ declare function xy_opposite(self: IXY): {
199
+ x: number;
200
+ y: number;
201
+ };
202
+ declare function xy_getRotation(self: IXY, another: IXY, origin: IXY): number;
203
+ declare function xy_toArray(self: IXY): [number, number];
204
+ declare function xy_xAxis(rotation: number): {
205
+ x: number;
206
+ y: number;
207
+ };
208
+ declare function xy_yAxis(rotation: number): {
209
+ x: number;
210
+ y: number;
211
+ };
212
+ declare class XY {
213
+ x: number;
214
+ y: number;
215
+ constructor(x: number, y: number);
216
+ plain(): {
217
+ x: number;
218
+ y: number;
219
+ };
220
+ tuple(): readonly [number, number];
221
+ plus(...others: IXY[]): XY;
222
+ minus(...others: IXY[]): XY;
223
+ multiply(...numbers: number[]): XY;
224
+ divide(...numbers: number[]): XY;
225
+ rotate(origin: IXY, rotation: number): XY;
226
+ symmetric(origin: IXY): XY;
227
+ ratio(another: IXY, t: number): XY;
228
+ getDot(another: IXY): number;
229
+ getDistance(another: IXY): number;
230
+ getAngle(another: IXY, origin: IXY): number;
231
+ static _(x?: number, y?: number): {
232
+ x: number;
233
+ y: number;
234
+ };
235
+ static of(x: number, y: number): XY;
236
+ static from(xy: IXY): XY;
237
+ static center(xy: {
238
+ centerX: number;
239
+ centerY: number;
240
+ }): XY;
241
+ static leftTop(e: {
242
+ left: number;
243
+ top: number;
244
+ }): XY;
245
+ static client(e: {
246
+ clientX: number;
247
+ clientY: number;
248
+ }): XY;
249
+ static tuple(arr: [number, number]): XY;
250
+ static xAxis(rotation: number): XY;
251
+ static yAxis(rotation: number): XY;
252
+ }
253
+
254
+ 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, twoDecimal, 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 };
package/dist/index.js ADDED
@@ -0,0 +1,587 @@
1
+ // src/math.ts
2
+ var { sqrt, abs, min, max, round, floor, ceil, random } = Math;
3
+ function pow2(number) {
4
+ return Math.pow(number, 2);
5
+ }
6
+ function pow3(number) {
7
+ return Math.pow(number, 3);
8
+ }
9
+ function multiply(...numbers) {
10
+ return numbers.reduce((i, all) => all *= i, 1);
11
+ }
12
+ function divide(a, b) {
13
+ return b === 0 ? 1 : a / b;
14
+ }
15
+ function numberHalfFix(number) {
16
+ const integerPart = ~~number;
17
+ const floatPart = number - integerPart;
18
+ const halfFixed = floatPart >= 0.75 ? 1 : floatPart >= 0.25 ? 0.5 : 0;
19
+ return integerPart + halfFixed;
20
+ }
21
+ function twoDecimal(number) {
22
+ return Number(number.toFixed(Number.isInteger(number) ? 0 : 2));
23
+ }
24
+
25
+ // src/angle.ts
26
+ var { PI, cos, sin, tan, acos, asin, atan, atan2 } = Math;
27
+ var Angle = class _Angle {
28
+ static cos(angle) {
29
+ return cos(_Angle.radianFy(angle));
30
+ }
31
+ static sin(angle) {
32
+ return sin(_Angle.radianFy(angle));
33
+ }
34
+ static tan(angle) {
35
+ return tan(_Angle.radianFy(angle));
36
+ }
37
+ static acos(angle) {
38
+ return _Angle.angleFy(acos(_Angle.radianFy(angle)));
39
+ }
40
+ static asin(angle) {
41
+ return _Angle.angleFy(asin(_Angle.radianFy(angle)));
42
+ }
43
+ static atan(angle) {
44
+ return _Angle.angleFy(atan(_Angle.radianFy(angle)));
45
+ }
46
+ static atan2(y, x) {
47
+ return _Angle.angleFy(atan2(y, x));
48
+ }
49
+ static angleFy(radians) {
50
+ return radians * (180 / Math.PI);
51
+ }
52
+ static radianFy(angle) {
53
+ return angle * (Math.PI / 180);
54
+ }
55
+ static normal(angle) {
56
+ return (angle + 360) % 360;
57
+ }
58
+ static snap(angle, step = 90) {
59
+ return _Angle.normal(Math.round(angle / step) * step);
60
+ }
61
+ static rotatePoint(ax, ay, ox, oy, angle) {
62
+ const radian = _Angle.radianFy(angle);
63
+ return {
64
+ x: (ax - ox) * cos(radian) - (ay - oy) * sin(radian) + ox,
65
+ y: (ax - ox) * sin(radian) + (ay - oy) * cos(radian) + oy
66
+ };
67
+ }
68
+ };
69
+
70
+ // src/xy.ts
71
+ function xy_(x = 0, y = 0) {
72
+ return { x, y };
73
+ }
74
+ function xy_client(e) {
75
+ return { x: e.clientX, y: e.clientY };
76
+ }
77
+ function xy_from(xy) {
78
+ return { x: xy.x, y: xy.y };
79
+ }
80
+ function xy_center(xy) {
81
+ return { x: xy.centerX, y: xy.centerY };
82
+ }
83
+ function xy_mutate(self, another) {
84
+ self.x = another.x;
85
+ self.y = another.y;
86
+ }
87
+ function xy_plus(self, another) {
88
+ return { x: self.x + another.x, y: self.y + another.y };
89
+ }
90
+ function xy_plus_mutate(self, another) {
91
+ self.x = self.x + another.x;
92
+ self.y = self.y + another.y;
93
+ }
94
+ function xy_plus_all(...xys) {
95
+ return xys.reduce((a, b) => xy_plus(a, b));
96
+ }
97
+ function xy_minus(self, another) {
98
+ return { x: self.x - another.x, y: self.y - another.y };
99
+ }
100
+ function xy_minus_mutate(self, another) {
101
+ self.x = self.x - another.x;
102
+ self.y = self.y - another.y;
103
+ }
104
+ function xy_multiply(self, ...numbers) {
105
+ const n = numbers.reduce((a, b) => a * b, 1);
106
+ return { x: self.x * n, y: self.y * n };
107
+ }
108
+ function xy_multiply_mutate(self, ...numbers) {
109
+ const n = numbers.reduce((a, b) => a * b, 1);
110
+ self.x = self.x * n;
111
+ self.y = self.y * n;
112
+ }
113
+ function xy_divide(self, ...numbers) {
114
+ const n = numbers.reduce((a, b) => a * b, 1);
115
+ return { x: self.x / n, y: self.y / n };
116
+ }
117
+ function xy_distance(self, another = xy_(0, 0)) {
118
+ return Math.sqrt((self.x - another.x) ** 2 + (self.y - another.y) ** 2);
119
+ }
120
+ function xy_rotate(self, origin, rotation) {
121
+ if (rotation === 0) return self;
122
+ return Angle.rotatePoint(self.x, self.y, origin.x, origin.y, rotation);
123
+ }
124
+ function xy_dot(self, another) {
125
+ return self.x * another.x + self.y * another.y;
126
+ }
127
+ function xy_symmetric(self, origin) {
128
+ return { x: 2 * origin.x - self.x, y: 2 * origin.y - self.y };
129
+ }
130
+ function xy_opposite(self) {
131
+ return { x: -self.x, y: -self.y };
132
+ }
133
+ function xy_getRotation(self, another, origin) {
134
+ return Angle.angleFy(
135
+ Math.atan2(self.y - origin.y, self.x - origin.x) - Math.atan2(another.y - origin.y, another.x - origin.x)
136
+ );
137
+ }
138
+ function xy_toArray(self) {
139
+ return [self.x, self.y];
140
+ }
141
+ function xy_xAxis(rotation) {
142
+ return { x: Angle.cos(rotation), y: Angle.sin(rotation) };
143
+ }
144
+ function xy_yAxis(rotation) {
145
+ return { x: -Angle.sin(rotation), y: Angle.cos(rotation) };
146
+ }
147
+ var XY = class _XY {
148
+ constructor(x, y) {
149
+ this.x = x;
150
+ this.y = y;
151
+ }
152
+ plain() {
153
+ return { x: this.x, y: this.y };
154
+ }
155
+ tuple() {
156
+ return [this.x, this.y];
157
+ }
158
+ plus(...others) {
159
+ const x = others.reduce((sum, cur) => sum + cur.x, this.x);
160
+ const y = others.reduce((sum, cur) => sum + cur.y, this.y);
161
+ return _XY.of(x, y);
162
+ }
163
+ minus(...others) {
164
+ const x = others.reduce((sum, cur) => sum - cur.x, this.x);
165
+ const y = others.reduce((sum, cur) => sum - cur.y, this.y);
166
+ return _XY.of(x, y);
167
+ }
168
+ multiply(...numbers) {
169
+ const n = numbers.reduce((a, b) => a * b, 1);
170
+ return _XY.of(this.x * n, this.y * n);
171
+ }
172
+ divide(...numbers) {
173
+ const n = numbers.reduce((a, b) => a * b, 1);
174
+ return _XY.of(this.x / n, this.y / n);
175
+ }
176
+ rotate(origin, rotation) {
177
+ if (rotation === 0) return _XY.from(this);
178
+ return _XY.from(Angle.rotatePoint(this.x, this.y, origin.x, origin.y, rotation));
179
+ }
180
+ symmetric(origin) {
181
+ return _XY.of(2 * origin.x - this.x, 2 * origin.y - this.y);
182
+ }
183
+ ratio(another, t) {
184
+ const x = this.x + (another.x - this.x) * t;
185
+ const y = this.y + (another.y - this.y) * t;
186
+ return _XY.of(x, y);
187
+ }
188
+ getDot(another) {
189
+ return this.x * another.x + this.y * another.y;
190
+ }
191
+ getDistance(another) {
192
+ return sqrt((this.x - another.x) ** 2 + (this.y - another.y) ** 2);
193
+ }
194
+ getAngle(another, origin) {
195
+ return Angle.angleFy(
196
+ Math.atan2(this.y - origin.y, this.x - origin.x) - Math.atan2(another.y - origin.y, another.x - origin.x)
197
+ );
198
+ }
199
+ static _(x = 0, y = 0) {
200
+ return { x, y };
201
+ }
202
+ static of(x, y) {
203
+ return new _XY(x, y);
204
+ }
205
+ static from(xy) {
206
+ if (xy instanceof _XY) return xy;
207
+ return _XY.of(xy.x, xy.y);
208
+ }
209
+ static center(xy) {
210
+ return _XY.of(xy.centerX, xy.centerY);
211
+ }
212
+ static leftTop(e) {
213
+ return _XY.of(e.left, e.top);
214
+ }
215
+ static client(e) {
216
+ return _XY.of(e.clientX, e.clientY);
217
+ }
218
+ static tuple(arr) {
219
+ return _XY.of(arr[0], arr[1]);
220
+ }
221
+ static xAxis(rotation) {
222
+ return _XY.of(Angle.cos(rotation), Angle.sin(rotation));
223
+ }
224
+ static yAxis(rotation) {
225
+ return _XY.of(-Angle.sin(rotation), Angle.cos(rotation));
226
+ }
227
+ };
228
+
229
+ // src/aabb.ts
230
+ var AABB = class _AABB {
231
+ constructor(minX, minY, maxX, maxY) {
232
+ this.minX = minX;
233
+ this.minY = minY;
234
+ this.maxX = maxX;
235
+ this.maxY = maxY;
236
+ }
237
+ static rect(aabb) {
238
+ return {
239
+ x: aabb.minX,
240
+ y: aabb.minY,
241
+ width: aabb.maxX - aabb.minX,
242
+ height: aabb.maxY - aabb.minY,
243
+ centerX: aabb.minX + (aabb.maxX - aabb.minX) / 2,
244
+ centerY: aabb.minY + (aabb.maxY - aabb.minY) / 2
245
+ };
246
+ }
247
+ static rectTuple(aabb) {
248
+ return [
249
+ aabb.minX,
250
+ aabb.minY,
251
+ aabb.maxX - aabb.minX,
252
+ aabb.maxY - aabb.minY
253
+ ];
254
+ }
255
+ static collide(one, another) {
256
+ return one.minX <= another.maxX && one.maxX >= another.minX && one.minY <= another.maxY && one.maxY >= another.minY;
257
+ }
258
+ static include(one, another) {
259
+ let result = 1;
260
+ let [large, small] = [one, another];
261
+ if (one.maxX - one.minX < another.maxX - another.minX) {
262
+ result = 0;
263
+ large = another;
264
+ small = one;
265
+ }
266
+ const included = large.minX <= small.minX && large.maxX >= small.maxX && large.minY <= small.minY && large.maxY >= small.maxY;
267
+ return included ? result : -1;
268
+ }
269
+ static extend(aabb, ...expands) {
270
+ const { minX, minY, maxX, maxY } = aabb;
271
+ if (expands.length === 1) {
272
+ const expand = expands[0];
273
+ return new _AABB(minX - expand, minY - expand, maxX + expand, maxY + expand);
274
+ } else {
275
+ return new _AABB(
276
+ minX - expands[0],
277
+ minY - expands[1],
278
+ maxX + expands[2],
279
+ maxY + expands[3]
280
+ );
281
+ }
282
+ }
283
+ static merge(...aabbList) {
284
+ let [xMin, yMin, xMax, yMax] = [Infinity, Infinity, -Infinity, -Infinity];
285
+ aabbList.forEach((aabb) => {
286
+ xMin = min(xMin, aabb.minX);
287
+ yMin = min(yMin, aabb.minY);
288
+ xMax = max(xMax, aabb.maxX);
289
+ yMax = max(yMax, aabb.maxY);
290
+ });
291
+ return new _AABB(xMin, yMin, xMax, yMax);
292
+ }
293
+ static fromOBB(obb) {
294
+ const width = obb.projectionLengthAt(XY._(1, 0));
295
+ const height = obb.projectionLengthAt(XY._(0, 1));
296
+ return new _AABB(
297
+ obb.center.x - width / 2,
298
+ obb.center.y - height / 2,
299
+ obb.center.x + width / 2,
300
+ obb.center.y + height / 2
301
+ );
302
+ }
303
+ };
304
+
305
+ // src/matrix.ts
306
+ var Matrix = class _Matrix {
307
+ static create() {
308
+ return [1, 0, 0, 1, 0, 0];
309
+ }
310
+ static invert(matrix) {
311
+ const [a, b, c, d, e, f] = matrix;
312
+ const invDet = 1 / (a * d - b * c);
313
+ return [d, -b, -c, a, c * f - d * e, b * e - a * f].map(
314
+ (i) => i * invDet
315
+ );
316
+ }
317
+ static applyPoint(xy, matrix) {
318
+ const { x, y } = xy;
319
+ const [a, b, c, d, e, f] = matrix;
320
+ return xy_(a * x + c * y + e, b * x + d * y + f);
321
+ }
322
+ static applyAABB(aabb, matrix) {
323
+ const { minX, minY, maxX, maxY } = aabb;
324
+ const xy1 = _Matrix.applyPoint(xy_(minX, minY), matrix);
325
+ const xy2 = _Matrix.applyPoint(xy_(maxX, minY), matrix);
326
+ const xy3 = _Matrix.applyPoint(xy_(maxX, maxY), matrix);
327
+ const xy4 = _Matrix.applyPoint(xy_(minX, maxY), matrix);
328
+ return {
329
+ minX: min(xy1.x, xy2.x, xy3.x, xy4.x),
330
+ minY: min(xy1.y, xy2.y, xy3.y, xy4.y),
331
+ maxX: max(xy1.x, xy2.x, xy3.x, xy4.x),
332
+ maxY: max(xy1.y, xy2.y, xy3.y, xy4.y)
333
+ };
334
+ }
335
+ static invertPoint(xy, matrix) {
336
+ return _Matrix.applyPoint(xy, _Matrix.invert(matrix));
337
+ }
338
+ static invertAABB(aabb, matrix) {
339
+ return _Matrix.applyAABB(aabb, _Matrix.invert(matrix));
340
+ }
341
+ };
342
+
343
+ // src/obb.ts
344
+ var OBB = class _OBB {
345
+ constructor(x, y, width, height, rotation) {
346
+ this.x = x;
347
+ this.y = y;
348
+ this.width = width;
349
+ this.height = height;
350
+ this.rotation = rotation;
351
+ this.center = this.#calcCenter();
352
+ this.axis = this.#calcAxis();
353
+ this.vertexes = this.calcVertexXY();
354
+ this.aabb = AABB.fromOBB(this);
355
+ }
356
+ center;
357
+ axis;
358
+ aabb;
359
+ vertexes;
360
+ get xy() {
361
+ return xy_(this.x, this.y);
362
+ }
363
+ #calcCenter = () => {
364
+ const center = xy_(this.x + this.width / 2, this.y + this.height / 2);
365
+ return xy_rotate(center, xy_(this.x, this.y), this.rotation);
366
+ };
367
+ #calcAxis = () => {
368
+ const cos2 = Angle.cos(this.rotation);
369
+ const sin2 = Angle.sin(this.rotation);
370
+ const widthAxis = xy_(cos2, -sin2);
371
+ const heightAxis = xy_(sin2, cos2);
372
+ return this.axis = { widthAxis, heightAxis };
373
+ };
374
+ calcVertexXY = () => {
375
+ const cos2 = Angle.cos(this.rotation);
376
+ const sin2 = Angle.sin(this.rotation);
377
+ const cosWidth = cos2 * this.width;
378
+ const sinWidth = sin2 * this.width;
379
+ const cosHeight = cos2 * this.height;
380
+ const sinHeight = sin2 * this.height;
381
+ const TL = xy_(this.x, this.y);
382
+ const TR = xy_(this.x + cosWidth, this.y + sinWidth);
383
+ const BR = xy_(this.x + cosWidth - sinHeight, this.y + sinWidth + cosHeight);
384
+ const BL = xy_(this.x - sinHeight, this.y + cosHeight);
385
+ return this.vertexes = [TL, TR, BR, BL];
386
+ };
387
+ clone = () => {
388
+ return new _OBB(this.x, this.y, this.width, this.height, this.rotation);
389
+ };
390
+ projectionLengthAt = (anotherAxis) => {
391
+ const { widthAxis, heightAxis } = this.axis;
392
+ return Math.abs(xy_dot(widthAxis, anotherAxis)) * this.width + Math.abs(xy_dot(heightAxis, anotherAxis)) * this.height;
393
+ };
394
+ collide = (another) => {
395
+ const centerVector = xy_minus(this.center, another.center);
396
+ if (this.projectionLengthAt(another.axis.widthAxis) + another.width <= 2 * Math.abs(xy_dot(centerVector, another.axis.widthAxis)))
397
+ return false;
398
+ if (this.projectionLengthAt(another.axis.heightAxis) + another.height <= 2 * Math.abs(xy_dot(centerVector, another.axis.heightAxis)))
399
+ return false;
400
+ if (another.projectionLengthAt(this.axis.widthAxis) + this.width <= 2 * Math.abs(xy_dot(centerVector, this.axis.widthAxis)))
401
+ return false;
402
+ if (another.projectionLengthAt(this.axis.heightAxis) + this.height <= 2 * Math.abs(xy_dot(centerVector, this.axis.heightAxis)))
403
+ return false;
404
+ return true;
405
+ };
406
+ static identityOBB() {
407
+ return new _OBB(0, 0, 0, 0, 0);
408
+ }
409
+ static fromRect(rect, rotation = 0) {
410
+ const { x, y, width, height } = rect;
411
+ return new _OBB(x, y, width, height, rotation);
412
+ }
413
+ static fromCenter(center, width, height, rotation = 0) {
414
+ const dx = center.x - width / 2;
415
+ const dy = center.y - height / 2;
416
+ const xy = XY.of(dx, dy).rotate(center, rotation);
417
+ return new _OBB(xy.x, xy.y, width, height, rotation);
418
+ }
419
+ static fromAABB(aabb) {
420
+ const { minX, minY, maxX, maxY } = aabb;
421
+ return new _OBB(minX, minY, maxX - minX, maxY - minY, 0);
422
+ }
423
+ };
424
+
425
+ // src/points-of-bezier.ts
426
+ function distance(p1, p2) {
427
+ return Math.sqrt(distanceSq(p1, p2));
428
+ }
429
+ function distanceSq(p1, p2) {
430
+ return Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2);
431
+ }
432
+ function distanceToSegmentSq(p, v, w) {
433
+ const l2 = distanceSq(v, w);
434
+ if (l2 === 0) {
435
+ return distanceSq(p, v);
436
+ }
437
+ let t = ((p.x - v.x) * (w.x - v.x) + (p.y - v.y) * (w.y - v.y)) / l2;
438
+ t = Math.max(0, Math.min(1, t));
439
+ return distanceSq(p, lerp(v, w, t));
440
+ }
441
+ function lerp(a, b, t) {
442
+ return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };
443
+ }
444
+ function flatness(points, offset) {
445
+ const p1 = points[offset + 0];
446
+ const p2 = points[offset + 1];
447
+ const p3 = points[offset + 2];
448
+ const p4 = points[offset + 3];
449
+ let ux = 3 * p2.x - 2 * p1.x - p4.x;
450
+ ux *= ux;
451
+ let uy = 3 * p2.y - 2 * p1.y - p4.y;
452
+ uy *= uy;
453
+ let vx = 3 * p3.x - 2 * p4.x - p1.x;
454
+ vx *= vx;
455
+ let vy = 3 * p3.y - 2 * p4.y - p1.y;
456
+ vy *= vy;
457
+ if (ux < vx) {
458
+ ux = vx;
459
+ }
460
+ if (uy < vy) {
461
+ uy = vy;
462
+ }
463
+ return ux + uy;
464
+ }
465
+ function getPointsOnBezierCurveWithSplitting(points, offset, tolerance, newPoints) {
466
+ const outPoints = newPoints || [];
467
+ if (flatness(points, offset) < tolerance) {
468
+ const p0 = points[offset + 0];
469
+ if (outPoints.length) {
470
+ const d = distance(outPoints[outPoints.length - 1], p0);
471
+ if (d > 1) {
472
+ outPoints.push(p0);
473
+ }
474
+ } else {
475
+ outPoints.push(p0);
476
+ }
477
+ outPoints.push(points[offset + 3]);
478
+ } else {
479
+ const t = 0.5;
480
+ const p1 = points[offset + 0];
481
+ const p2 = points[offset + 1];
482
+ const p3 = points[offset + 2];
483
+ const p4 = points[offset + 3];
484
+ const q1 = lerp(p1, p2, t);
485
+ const q2 = lerp(p2, p3, t);
486
+ const q3 = lerp(p3, p4, t);
487
+ const r1 = lerp(q1, q2, t);
488
+ const r2 = lerp(q2, q3, t);
489
+ const red = lerp(r1, r2, t);
490
+ getPointsOnBezierCurveWithSplitting([p1, q1, r1, red], 0, tolerance, outPoints);
491
+ getPointsOnBezierCurveWithSplitting([red, r2, q3, p4], 0, tolerance, outPoints);
492
+ }
493
+ return outPoints;
494
+ }
495
+ function simplify(points, distance2) {
496
+ return simplifyPoints(points, 0, points.length, distance2);
497
+ }
498
+ function simplifyPoints(points, start, end, epsilon, newPoints) {
499
+ const outPoints = newPoints || [];
500
+ const s = points[start];
501
+ const e = points[end - 1];
502
+ let maxDistSq = 0;
503
+ let maxNdx = 1;
504
+ for (let i = start + 1; i < end - 1; ++i) {
505
+ const distSq = distanceToSegmentSq(points[i], s, e);
506
+ if (distSq > maxDistSq) {
507
+ maxDistSq = distSq;
508
+ maxNdx = i;
509
+ }
510
+ }
511
+ if (Math.sqrt(maxDistSq) > epsilon) {
512
+ simplifyPoints(points, start, maxNdx + 1, epsilon, outPoints);
513
+ simplifyPoints(points, maxNdx, end, epsilon, outPoints);
514
+ } else {
515
+ if (!outPoints.length) {
516
+ outPoints.push(s);
517
+ }
518
+ outPoints.push(e);
519
+ }
520
+ return outPoints;
521
+ }
522
+ function pointsOnBezierCurves(points, tolerance = 0.15, distance2) {
523
+ const newPoints = [];
524
+ const numSegments = (points.length - 1) / 3;
525
+ for (let i = 0; i < numSegments; i++) {
526
+ const offset = i * 3;
527
+ getPointsOnBezierCurveWithSplitting(points, offset, tolerance, newPoints);
528
+ }
529
+ if (distance2 && distance2 > 0) {
530
+ return simplifyPoints(newPoints, 0, newPoints.length, distance2);
531
+ }
532
+ return newPoints;
533
+ }
534
+ export {
535
+ AABB,
536
+ Angle,
537
+ Matrix,
538
+ OBB,
539
+ PI,
540
+ XY,
541
+ abs,
542
+ acos,
543
+ asin,
544
+ atan,
545
+ atan2,
546
+ ceil,
547
+ cos,
548
+ divide,
549
+ floor,
550
+ max,
551
+ min,
552
+ multiply,
553
+ numberHalfFix,
554
+ pointsOnBezierCurves,
555
+ pow2,
556
+ pow3,
557
+ random,
558
+ round,
559
+ simplify,
560
+ simplifyPoints,
561
+ sin,
562
+ sqrt,
563
+ tan,
564
+ twoDecimal,
565
+ xy_,
566
+ xy_center,
567
+ xy_client,
568
+ xy_distance,
569
+ xy_divide,
570
+ xy_dot,
571
+ xy_from,
572
+ xy_getRotation,
573
+ xy_minus,
574
+ xy_minus_mutate,
575
+ xy_multiply,
576
+ xy_multiply_mutate,
577
+ xy_mutate,
578
+ xy_opposite,
579
+ xy_plus,
580
+ xy_plus_all,
581
+ xy_plus_mutate,
582
+ xy_rotate,
583
+ xy_symmetric,
584
+ xy_toArray,
585
+ xy_xAxis,
586
+ xy_yAxis
587
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gitborlando/geo",
3
- "version": "4.1.0",
3
+ "version": "4.1.1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {
@@ -9,6 +9,10 @@
9
9
  "types": "./dist/index.d.ts"
10
10
  }
11
11
  },
12
+ "files": [
13
+ "dist",
14
+ "src"
15
+ ],
12
16
  "publishConfig": {
13
17
  "access": "public",
14
18
  "registry": "https://registry.npmjs.org"
@@ -1,36 +0,0 @@
1
- name: 发布包
2
-
3
- on:
4
- push:
5
- branches:
6
- - main
7
-
8
- jobs:
9
- publish:
10
- runs-on: ubuntu-latest
11
-
12
- steps:
13
- - name: 检出代码
14
- uses: actions/checkout@v4
15
-
16
- - name: 设置 Node.js
17
- uses: actions/setup-node@v4
18
- with:
19
- node-version: '20'
20
- registry-url: 'https://registry.npmjs.org'
21
-
22
- - name: 安装 pnpm
23
- uses: pnpm/action-setup@v2
24
- with:
25
- version: 8
26
-
27
- - name: 安装依赖
28
- run: pnpm install
29
-
30
- - name: 构建项目
31
- run: pnpm build
32
-
33
- - name: 发布到 npm
34
- run: pnpm publish --no-git-checks
35
- env:
36
- NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
@@ -1,44 +0,0 @@
1
- name: Release
2
-
3
- on:
4
- push:
5
- branches:
6
- - main
7
-
8
- jobs:
9
- release:
10
- name: Release
11
- runs-on: ubuntu-latest
12
- steps:
13
- - name: Checkout
14
- uses: actions/checkout@v3
15
- with:
16
- fetch-depth: 0
17
-
18
- - name: Setup Node.js
19
- uses: actions/setup-node@v3
20
- with:
21
- node-version: 20
22
-
23
- - name: Setup PNPM
24
- uses: pnpm/action-setup@v2
25
- with:
26
- version: 8
27
-
28
- - name: Install Dependencies
29
- run: pnpm install
30
-
31
- # 添加构建步骤
32
- - name: Build Packages
33
- run: pnpm build
34
-
35
- - name: Create Release Pull Request or Publish
36
- id: changeset
37
- uses: changesets/action@v1
38
- with:
39
- publish: pnpm changeset publish
40
- commit: 'chore: version packages'
41
- title: 'chore: version packages'
42
- env:
43
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44
- NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
package/.husky/pre-commit DELETED
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env sh
2
-
3
- npx pretty-quick --staged
@@ -1,3 +0,0 @@
1
- {
2
- "cSpell.words": ["aabb"]
3
- }
package/tsconfig.json DELETED
@@ -1,31 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "baseUrl": "./",
4
- "target": "ESNext",
5
- "useDefineForClassFields": true,
6
- "lib": ["ESNext", "DOM", "DOM.Iterable"],
7
- "module": "ESNext",
8
- "skipLibCheck": true,
9
- /* Bundler mode */
10
- "moduleResolution": "bundler",
11
- "allowImportingTsExtensions": true,
12
- "experimentalDecorators": true,
13
- "emitDecoratorMetadata": true,
14
- "isolatedModules": true,
15
- "moduleDetection": "force",
16
- "noEmit": true,
17
- "jsx": "react-jsx",
18
- /* Linting */
19
- "strict": true,
20
- "noUnusedLocals": false,
21
- "noUnusedParameters": false,
22
- "noFallthroughCasesInSwitch": false
23
- },
24
- "include": [
25
- "packages",
26
- "src",
27
- "tsup.config.ts",
28
- "vitest.config.ts",
29
- "src/__test__"
30
- ]
31
- }
package/tsup.config.ts DELETED
@@ -1,10 +0,0 @@
1
- import { defineConfig } from 'tsup'
2
-
3
- export default defineConfig({
4
- entry: ['src/index.ts'],
5
- outDir: 'dist',
6
- format: ['esm'],
7
- dts: true,
8
- splitting: true,
9
- clean: true,
10
- })
package/vitest.config.ts DELETED
@@ -1,8 +0,0 @@
1
- import { defineConfig } from 'vitest/config'
2
-
3
- export default defineConfig({
4
- test: {
5
- environment: 'node',
6
- globals: true,
7
- },
8
- })