@gitborlando/geo 4.0.0 → 4.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.
@@ -0,0 +1,36 @@
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 }}
@@ -0,0 +1,44 @@
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 }}
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env sh
2
+
3
+ npx pretty-quick --staged
@@ -0,0 +1,3 @@
1
+ {
2
+ "cSpell.words": ["aabb"]
3
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gitborlando/geo",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {
@@ -18,12 +18,28 @@
18
18
  "license": "ISC",
19
19
  "devDependencies": {
20
20
  "@vitest/ui": "^3.2.4",
21
- "vitest": "^3.2.4"
21
+ "vitest": "^3.2.4",
22
+ "@changesets/cli": "^2.29.5",
23
+ "@types/node": "^24.0.15",
24
+ "husky": "^9.1.7",
25
+ "pretty-quick": "^4.2.2",
26
+ "tsup": "^8.5.0",
27
+ "tsx": "^4.20.3",
28
+ "typescript": "^5.8.3"
29
+ },
30
+ "prettier": {
31
+ "printWidth": 85,
32
+ "jsxBracketSameLine": true,
33
+ "useTabs": false,
34
+ "tabWidth": 2,
35
+ "semi": false,
36
+ "singleQuote": true,
37
+ "jsxSingleQuote": true
22
38
  },
23
39
  "scripts": {
24
40
  "dev": "tsup --watch",
25
41
  "build": "tsup",
26
- "clean": "rm -rf node_modules",
42
+ "clean": "rm -rf node_modules pnpm-lock.yaml",
27
43
  "test": "vitest",
28
44
  "test:run": "vitest run",
29
45
  "test:ui": "vitest --ui"
@@ -1,8 +1,8 @@
1
1
  // 测试轴对齐包围盒功能
2
2
 
3
3
  import { describe, expect, it } from 'vitest'
4
- import { AABB } from '../src/aabb'
5
- import { OBB } from '../src/obb'
4
+ import { AABB } from '../aabb'
5
+ import { OBB } from '../obb'
6
6
 
7
7
  describe('AABB', () => {
8
8
  it('should create AABB correctly', () => {
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest'
2
- import { Angle, PI } from '../src/angle'
2
+ import { Angle, PI } from '../angle'
3
3
 
4
4
  describe('Angle', () => {
5
5
  it('should export PI constant correctly', () => {
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest'
2
- import * as Geo from '../src/index'
2
+ import * as Geo from '../index'
3
3
 
4
4
  describe('Index exports', () => {
5
5
  it('should export all modules correctly', () => {
@@ -13,7 +13,7 @@ import {
13
13
  random,
14
14
  round,
15
15
  sqrt,
16
- } from '../src/math'
16
+ } from '../math'
17
17
 
18
18
  describe('Math utilities', () => {
19
19
  it('should export Math functions correctly', () => {
@@ -1,6 +1,6 @@
1
1
  import { describe, expect, it } from 'vitest'
2
- import { AABB } from '../src/aabb'
3
- import { IMatrix, Matrix } from '../src/matrix'
2
+ import { AABB } from '../aabb'
3
+ import { IMatrix, Matrix } from '../matrix'
4
4
 
5
5
  describe('Matrix', () => {
6
6
  it('should create identity matrix correctly', () => {
@@ -1,6 +1,6 @@
1
1
  import { describe, expect, it } from 'vitest'
2
- import { AABB } from '../src/aabb'
3
- import { OBB } from '../src/obb'
2
+ import { AABB } from '../aabb'
3
+ import { OBB } from '../obb'
4
4
 
5
5
  describe('OBB', () => {
6
6
  it('should create OBB correctly', () => {
@@ -4,7 +4,7 @@ import {
4
4
  pointsOnBezierCurves,
5
5
  simplify,
6
6
  simplifyPoints,
7
- } from '../src/points-of-bezier'
7
+ } from '../points-of-bezier'
8
8
 
9
9
  describe('Points of Bezier', () => {
10
10
  const createBezierPoints = (): Point[] => [
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest'
2
- import { IRect, IRectWithCenter, IXY } from '../src/types'
2
+ import { IRect, IRectWithCenter, IXY } from '../types'
3
3
 
4
4
  describe('Types', () => {
5
5
  it('should define IXY interface correctly', () => {
@@ -14,7 +14,7 @@ import {
14
14
  xy_opposite,
15
15
  xy_plus,
16
16
  xy_toArray,
17
- } from '../src/xy'
17
+ } from '../xy'
18
18
 
19
19
  describe('XY utilities', () => {
20
20
  it('should create xy point correctly', () => {
package/src/aabb.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { max, min } from './math'
2
2
  import { OBB } from './obb'
3
3
  import { IRectWithCenter } from './types'
4
- import { xy_ } from './xy'
4
+ import { XY } from './xy'
5
5
 
6
6
  export class AABB {
7
7
  constructor(
@@ -22,6 +22,15 @@ export class AABB {
22
22
  }
23
23
  }
24
24
 
25
+ static rectTuple(aabb: AABB) {
26
+ return [
27
+ aabb.minX,
28
+ aabb.minY,
29
+ aabb.maxX - aabb.minX,
30
+ aabb.maxY - aabb.minY,
31
+ ] as const
32
+ }
33
+
25
34
  static collide(one: AABB, another: AABB): boolean {
26
35
  return (
27
36
  one.minX <= another.maxX &&
@@ -77,8 +86,8 @@ export class AABB {
77
86
  }
78
87
 
79
88
  static fromOBB(obb: OBB) {
80
- const width = obb.projectionLengthAt(xy_(1, 0))
81
- const height = obb.projectionLengthAt(xy_(0, 1))
89
+ const width = obb.projectionLengthAt(XY._(1, 0))
90
+ const height = obb.projectionLengthAt(XY._(0, 1))
82
91
  return new AABB(
83
92
  obb.center.x - width / 2,
84
93
  obb.center.y - height / 2,
package/src/xy.ts CHANGED
@@ -106,6 +106,10 @@ export class XY {
106
106
  return { x: this.x, y: this.y }
107
107
  }
108
108
 
109
+ tuple() {
110
+ return [this.x, this.y] as const
111
+ }
112
+
109
113
  plus(...others: IXY[]) {
110
114
  const x = others.reduce((sum, cur) => sum + cur.x, this.x)
111
115
  const y = others.reduce((sum, cur) => sum + cur.y, this.y)
@@ -186,4 +190,12 @@ export class XY {
186
190
  static tuple(arr: [number, number]) {
187
191
  return XY.of(arr[0], arr[1])
188
192
  }
193
+
194
+ static xAxis(rotation: number) {
195
+ return XY.of(Angle.cos(rotation), Angle.sin(rotation))
196
+ }
197
+
198
+ static yAxis(rotation: number) {
199
+ return XY.of(-Angle.sin(rotation), Angle.cos(rotation))
200
+ }
189
201
  }
package/tsconfig.json ADDED
@@ -0,0 +1,31 @@
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/CHANGELOG.md DELETED
@@ -1,38 +0,0 @@
1
- # @gitborlando/geo
2
-
3
- ## 4.0.0
4
-
5
- ### Major Changes
6
-
7
- - 7773415: XY,AABB类破坏性更新
8
-
9
- ## 3.1.0
10
-
11
- ### Minor Changes
12
-
13
- - 2b4693c: 更新xy类
14
- - 0ee9da2: XY类小幅更新
15
-
16
- ## 3.0.0
17
-
18
- ### Major Changes
19
-
20
- - f121a3e: XY类破坏性更新
21
-
22
- ## 2.1.0
23
-
24
- ### Minor Changes
25
-
26
- - 7bd75bc: 新增一些方法
27
-
28
- ## 2.0.0
29
-
30
- ### Major Changes
31
-
32
- - b2c20d9: 修正了多个类的方法名称,统一为小写格式,更新了相关测试用例以匹配新的方法名。同时,更新了 package.json 中的导出路径,从 src 目录改为 dist 目录。
33
-
34
- ## 1.0.1
35
-
36
- ### Patch Changes
37
-
38
- - dd370ef: initial
package/dist/index.d.ts DELETED
@@ -1,250 +0,0 @@
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 collide(one: AABB, another: AABB): boolean;
66
- static include(one: AABB, another: AABB): number;
67
- static extend(aabb: AABB, ...expands: [number] | [number, number, number, number]): AABB;
68
- static merge(...aabbList: AABB[]): AABB;
69
- static fromOBB(obb: OBB): AABB;
70
- }
71
-
72
- declare const PI: number;
73
- declare const cos: (x: number) => number;
74
- declare const sin: (x: number) => number;
75
- declare const tan: (x: number) => number;
76
- declare const acos: (x: number) => number;
77
- declare const asin: (x: number) => number;
78
- declare const atan: (x: number) => number;
79
- declare const atan2: (y: number, x: number) => number;
80
- declare class Angle {
81
- static cos(angle: number): number;
82
- static sin(angle: number): number;
83
- static tan(angle: number): number;
84
- static acos(angle: number): number;
85
- static asin(angle: number): number;
86
- static atan(angle: number): number;
87
- static atan2(y: number, x: number): number;
88
- static angleFy(radians: number): number;
89
- static radianFy(angle: number): number;
90
- static normal(angle: number): number;
91
- static snap(angle: number, step?: number): number;
92
- static rotatePoint(ax: number, ay: number, ox: number, oy: number, angle: number): {
93
- x: number;
94
- y: number;
95
- };
96
- }
97
-
98
- declare const sqrt: (x: number) => number;
99
- declare const abs: (x: number) => number;
100
- declare const min: (...values: number[]) => number;
101
- declare const max: (...values: number[]) => number;
102
- declare const round: (x: number) => number;
103
- declare const floor: (x: number) => number;
104
- declare const ceil: (x: number) => number;
105
- declare const random: () => number;
106
- declare function pow2(number: number): number;
107
- declare function pow3(number: number): number;
108
- declare function multiply(...numbers: number[]): number;
109
- declare function divide(a: number, b: number): number;
110
- declare function numberHalfFix(number: number): number;
111
- declare function twoDecimal(number: number): number;
112
-
113
- type IMatrix = [number, number, number, number, number, number];
114
- declare class Matrix {
115
- static create(): IMatrix;
116
- static invert(matrix: IMatrix): IMatrix;
117
- static applyPoint(xy: IXY, matrix: IMatrix): {
118
- x: number;
119
- y: number;
120
- };
121
- static applyAABB(aabb: AABB, matrix: IMatrix): {
122
- minX: number;
123
- minY: number;
124
- maxX: number;
125
- maxY: number;
126
- };
127
- static invertPoint(xy: IXY, matrix: IMatrix): {
128
- x: number;
129
- y: number;
130
- };
131
- static invertAABB(aabb: AABB, matrix: IMatrix): {
132
- minX: number;
133
- minY: number;
134
- maxX: number;
135
- maxY: number;
136
- };
137
- }
138
-
139
- type Point = {
140
- x: number;
141
- y: number;
142
- };
143
- declare function simplify(points: readonly Point[], distance: number): Point[];
144
- declare function simplifyPoints(points: readonly Point[], start: number, end: number, epsilon: number, newPoints?: Point[]): Point[];
145
- declare function pointsOnBezierCurves(points: readonly Point[], tolerance?: number, distance?: number): Point[];
146
-
147
- declare function xy_(x?: number, y?: number): {
148
- x: number;
149
- y: number;
150
- };
151
- declare function xy_client(e: any): {
152
- x: any;
153
- y: any;
154
- };
155
- declare function xy_from(xy: IXY): {
156
- x: number;
157
- y: number;
158
- };
159
- declare function xy_center(xy: {
160
- centerX: number;
161
- centerY: number;
162
- }): {
163
- x: number;
164
- y: number;
165
- };
166
- declare function xy_mutate(self: IXY, another: IXY): void;
167
- declare function xy_plus(self: IXY, another: IXY): {
168
- x: number;
169
- y: number;
170
- };
171
- declare function xy_plus_mutate(self: IXY, another: IXY): void;
172
- declare function xy_plus_all(...xys: IXY[]): IXY;
173
- declare function xy_minus(self: IXY, another: IXY): {
174
- x: number;
175
- y: number;
176
- };
177
- declare function xy_minus_mutate(self: IXY, another: IXY): void;
178
- declare function xy_multiply(self: IXY, ...numbers: number[]): {
179
- x: number;
180
- y: number;
181
- };
182
- declare function xy_multiply_mutate(self: IXY, ...numbers: number[]): void;
183
- declare function xy_divide(self: IXY, ...numbers: number[]): {
184
- x: number;
185
- y: number;
186
- };
187
- declare function xy_distance(self: IXY, another?: IXY): number;
188
- declare function xy_rotate(self: IXY, origin: IXY, rotation: number): {
189
- x: number;
190
- y: number;
191
- };
192
- declare function xy_dot(self: IXY, another: IXY): number;
193
- declare function xy_symmetric(self: IXY, origin: IXY): {
194
- x: number;
195
- y: number;
196
- };
197
- declare function xy_opposite(self: IXY): {
198
- x: number;
199
- y: number;
200
- };
201
- declare function xy_getRotation(self: IXY, another: IXY, origin: IXY): number;
202
- declare function xy_toArray(self: IXY): [number, number];
203
- declare function xy_xAxis(rotation: number): {
204
- x: number;
205
- y: number;
206
- };
207
- declare function xy_yAxis(rotation: number): {
208
- x: number;
209
- y: number;
210
- };
211
- declare class XY {
212
- x: number;
213
- y: number;
214
- constructor(x: number, y: number);
215
- plain(): {
216
- x: number;
217
- y: number;
218
- };
219
- plus(...others: IXY[]): XY;
220
- minus(...others: IXY[]): XY;
221
- multiply(...numbers: number[]): XY;
222
- divide(...numbers: number[]): XY;
223
- rotate(origin: IXY, rotation: number): XY;
224
- symmetric(origin: IXY): XY;
225
- ratio(another: IXY, t: number): XY;
226
- getDot(another: IXY): number;
227
- getDistance(another: IXY): number;
228
- getAngle(another: IXY, origin: IXY): number;
229
- static _(x?: number, y?: number): {
230
- x: number;
231
- y: number;
232
- };
233
- static of(x: number, y: number): XY;
234
- static from(xy: IXY): XY;
235
- static center(xy: {
236
- centerX: number;
237
- centerY: number;
238
- }): XY;
239
- static leftTop(e: {
240
- left: number;
241
- top: number;
242
- }): XY;
243
- static client(e: {
244
- clientX: number;
245
- clientY: number;
246
- }): XY;
247
- static tuple(arr: [number, number]): XY;
248
- }
249
-
250
- 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 DELETED
@@ -1,570 +0,0 @@
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
- plus(...others) {
156
- const x = others.reduce((sum, cur) => sum + cur.x, this.x);
157
- const y = others.reduce((sum, cur) => sum + cur.y, this.y);
158
- return _XY.of(x, y);
159
- }
160
- minus(...others) {
161
- const x = others.reduce((sum, cur) => sum - cur.x, this.x);
162
- const y = others.reduce((sum, cur) => sum - cur.y, this.y);
163
- return _XY.of(x, y);
164
- }
165
- multiply(...numbers) {
166
- const n = numbers.reduce((a, b) => a * b, 1);
167
- return _XY.of(this.x * n, this.y * n);
168
- }
169
- divide(...numbers) {
170
- const n = numbers.reduce((a, b) => a * b, 1);
171
- return _XY.of(this.x / n, this.y / n);
172
- }
173
- rotate(origin, rotation) {
174
- if (rotation === 0) return _XY.from(this);
175
- return _XY.from(Angle.rotatePoint(this.x, this.y, origin.x, origin.y, rotation));
176
- }
177
- symmetric(origin) {
178
- return _XY.of(2 * origin.x - this.x, 2 * origin.y - this.y);
179
- }
180
- ratio(another, t) {
181
- const x = this.x + (another.x - this.x) * t;
182
- const y = this.y + (another.y - this.y) * t;
183
- return _XY.of(x, y);
184
- }
185
- getDot(another) {
186
- return this.x * another.x + this.y * another.y;
187
- }
188
- getDistance(another) {
189
- return sqrt((this.x - another.x) ** 2 + (this.y - another.y) ** 2);
190
- }
191
- getAngle(another, origin) {
192
- return Angle.angleFy(
193
- Math.atan2(this.y - origin.y, this.x - origin.x) - Math.atan2(another.y - origin.y, another.x - origin.x)
194
- );
195
- }
196
- static _(x = 0, y = 0) {
197
- return { x, y };
198
- }
199
- static of(x, y) {
200
- return new _XY(x, y);
201
- }
202
- static from(xy) {
203
- if (xy instanceof _XY) return xy;
204
- return _XY.of(xy.x, xy.y);
205
- }
206
- static center(xy) {
207
- return _XY.of(xy.centerX, xy.centerY);
208
- }
209
- static leftTop(e) {
210
- return _XY.of(e.left, e.top);
211
- }
212
- static client(e) {
213
- return _XY.of(e.clientX, e.clientY);
214
- }
215
- static tuple(arr) {
216
- return _XY.of(arr[0], arr[1]);
217
- }
218
- };
219
-
220
- // src/aabb.ts
221
- var AABB = class _AABB {
222
- constructor(minX, minY, maxX, maxY) {
223
- this.minX = minX;
224
- this.minY = minY;
225
- this.maxX = maxX;
226
- this.maxY = maxY;
227
- }
228
- static rect(aabb) {
229
- return {
230
- x: aabb.minX,
231
- y: aabb.minY,
232
- width: aabb.maxX - aabb.minX,
233
- height: aabb.maxY - aabb.minY,
234
- centerX: aabb.minX + (aabb.maxX - aabb.minX) / 2,
235
- centerY: aabb.minY + (aabb.maxY - aabb.minY) / 2
236
- };
237
- }
238
- static collide(one, another) {
239
- return one.minX <= another.maxX && one.maxX >= another.minX && one.minY <= another.maxY && one.maxY >= another.minY;
240
- }
241
- static include(one, another) {
242
- let result = 1;
243
- let [large, small] = [one, another];
244
- if (one.maxX - one.minX < another.maxX - another.minX) {
245
- result = 0;
246
- large = another;
247
- small = one;
248
- }
249
- const included = large.minX <= small.minX && large.maxX >= small.maxX && large.minY <= small.minY && large.maxY >= small.maxY;
250
- return included ? result : -1;
251
- }
252
- static extend(aabb, ...expands) {
253
- const { minX, minY, maxX, maxY } = aabb;
254
- if (expands.length === 1) {
255
- const expand = expands[0];
256
- return new _AABB(minX - expand, minY - expand, maxX + expand, maxY + expand);
257
- } else {
258
- return new _AABB(
259
- minX - expands[0],
260
- minY - expands[1],
261
- maxX + expands[2],
262
- maxY + expands[3]
263
- );
264
- }
265
- }
266
- static merge(...aabbList) {
267
- let [xMin, yMin, xMax, yMax] = [Infinity, Infinity, -Infinity, -Infinity];
268
- aabbList.forEach((aabb) => {
269
- xMin = min(xMin, aabb.minX);
270
- yMin = min(yMin, aabb.minY);
271
- xMax = max(xMax, aabb.maxX);
272
- yMax = max(yMax, aabb.maxY);
273
- });
274
- return new _AABB(xMin, yMin, xMax, yMax);
275
- }
276
- static fromOBB(obb) {
277
- const width = obb.projectionLengthAt(xy_(1, 0));
278
- const height = obb.projectionLengthAt(xy_(0, 1));
279
- return new _AABB(
280
- obb.center.x - width / 2,
281
- obb.center.y - height / 2,
282
- obb.center.x + width / 2,
283
- obb.center.y + height / 2
284
- );
285
- }
286
- };
287
-
288
- // src/matrix.ts
289
- var Matrix = class _Matrix {
290
- static create() {
291
- return [1, 0, 0, 1, 0, 0];
292
- }
293
- static invert(matrix) {
294
- const [a, b, c, d, e, f] = matrix;
295
- const invDet = 1 / (a * d - b * c);
296
- return [d, -b, -c, a, c * f - d * e, b * e - a * f].map(
297
- (i) => i * invDet
298
- );
299
- }
300
- static applyPoint(xy, matrix) {
301
- const { x, y } = xy;
302
- const [a, b, c, d, e, f] = matrix;
303
- return xy_(a * x + c * y + e, b * x + d * y + f);
304
- }
305
- static applyAABB(aabb, matrix) {
306
- const { minX, minY, maxX, maxY } = aabb;
307
- const xy1 = _Matrix.applyPoint(xy_(minX, minY), matrix);
308
- const xy2 = _Matrix.applyPoint(xy_(maxX, minY), matrix);
309
- const xy3 = _Matrix.applyPoint(xy_(maxX, maxY), matrix);
310
- const xy4 = _Matrix.applyPoint(xy_(minX, maxY), matrix);
311
- return {
312
- minX: min(xy1.x, xy2.x, xy3.x, xy4.x),
313
- minY: min(xy1.y, xy2.y, xy3.y, xy4.y),
314
- maxX: max(xy1.x, xy2.x, xy3.x, xy4.x),
315
- maxY: max(xy1.y, xy2.y, xy3.y, xy4.y)
316
- };
317
- }
318
- static invertPoint(xy, matrix) {
319
- return _Matrix.applyPoint(xy, _Matrix.invert(matrix));
320
- }
321
- static invertAABB(aabb, matrix) {
322
- return _Matrix.applyAABB(aabb, _Matrix.invert(matrix));
323
- }
324
- };
325
-
326
- // src/obb.ts
327
- var OBB = class _OBB {
328
- constructor(x, y, width, height, rotation) {
329
- this.x = x;
330
- this.y = y;
331
- this.width = width;
332
- this.height = height;
333
- this.rotation = rotation;
334
- this.center = this.#calcCenter();
335
- this.axis = this.#calcAxis();
336
- this.vertexes = this.calcVertexXY();
337
- this.aabb = AABB.fromOBB(this);
338
- }
339
- center;
340
- axis;
341
- aabb;
342
- vertexes;
343
- get xy() {
344
- return xy_(this.x, this.y);
345
- }
346
- #calcCenter = () => {
347
- const center = xy_(this.x + this.width / 2, this.y + this.height / 2);
348
- return xy_rotate(center, xy_(this.x, this.y), this.rotation);
349
- };
350
- #calcAxis = () => {
351
- const cos2 = Angle.cos(this.rotation);
352
- const sin2 = Angle.sin(this.rotation);
353
- const widthAxis = xy_(cos2, -sin2);
354
- const heightAxis = xy_(sin2, cos2);
355
- return this.axis = { widthAxis, heightAxis };
356
- };
357
- calcVertexXY = () => {
358
- const cos2 = Angle.cos(this.rotation);
359
- const sin2 = Angle.sin(this.rotation);
360
- const cosWidth = cos2 * this.width;
361
- const sinWidth = sin2 * this.width;
362
- const cosHeight = cos2 * this.height;
363
- const sinHeight = sin2 * this.height;
364
- const TL = xy_(this.x, this.y);
365
- const TR = xy_(this.x + cosWidth, this.y + sinWidth);
366
- const BR = xy_(this.x + cosWidth - sinHeight, this.y + sinWidth + cosHeight);
367
- const BL = xy_(this.x - sinHeight, this.y + cosHeight);
368
- return this.vertexes = [TL, TR, BR, BL];
369
- };
370
- clone = () => {
371
- return new _OBB(this.x, this.y, this.width, this.height, this.rotation);
372
- };
373
- projectionLengthAt = (anotherAxis) => {
374
- const { widthAxis, heightAxis } = this.axis;
375
- return Math.abs(xy_dot(widthAxis, anotherAxis)) * this.width + Math.abs(xy_dot(heightAxis, anotherAxis)) * this.height;
376
- };
377
- collide = (another) => {
378
- const centerVector = xy_minus(this.center, another.center);
379
- if (this.projectionLengthAt(another.axis.widthAxis) + another.width <= 2 * Math.abs(xy_dot(centerVector, another.axis.widthAxis)))
380
- return false;
381
- if (this.projectionLengthAt(another.axis.heightAxis) + another.height <= 2 * Math.abs(xy_dot(centerVector, another.axis.heightAxis)))
382
- return false;
383
- if (another.projectionLengthAt(this.axis.widthAxis) + this.width <= 2 * Math.abs(xy_dot(centerVector, this.axis.widthAxis)))
384
- return false;
385
- if (another.projectionLengthAt(this.axis.heightAxis) + this.height <= 2 * Math.abs(xy_dot(centerVector, this.axis.heightAxis)))
386
- return false;
387
- return true;
388
- };
389
- static identityOBB() {
390
- return new _OBB(0, 0, 0, 0, 0);
391
- }
392
- static fromRect(rect, rotation = 0) {
393
- const { x, y, width, height } = rect;
394
- return new _OBB(x, y, width, height, rotation);
395
- }
396
- static fromCenter(center, width, height, rotation = 0) {
397
- const dx = center.x - width / 2;
398
- const dy = center.y - height / 2;
399
- const xy = XY.of(dx, dy).rotate(center, rotation);
400
- return new _OBB(xy.x, xy.y, width, height, rotation);
401
- }
402
- static fromAABB(aabb) {
403
- const { minX, minY, maxX, maxY } = aabb;
404
- return new _OBB(minX, minY, maxX - minX, maxY - minY, 0);
405
- }
406
- };
407
-
408
- // src/points-of-bezier.ts
409
- function distance(p1, p2) {
410
- return Math.sqrt(distanceSq(p1, p2));
411
- }
412
- function distanceSq(p1, p2) {
413
- return Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2);
414
- }
415
- function distanceToSegmentSq(p, v, w) {
416
- const l2 = distanceSq(v, w);
417
- if (l2 === 0) {
418
- return distanceSq(p, v);
419
- }
420
- let t = ((p.x - v.x) * (w.x - v.x) + (p.y - v.y) * (w.y - v.y)) / l2;
421
- t = Math.max(0, Math.min(1, t));
422
- return distanceSq(p, lerp(v, w, t));
423
- }
424
- function lerp(a, b, t) {
425
- return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };
426
- }
427
- function flatness(points, offset) {
428
- const p1 = points[offset + 0];
429
- const p2 = points[offset + 1];
430
- const p3 = points[offset + 2];
431
- const p4 = points[offset + 3];
432
- let ux = 3 * p2.x - 2 * p1.x - p4.x;
433
- ux *= ux;
434
- let uy = 3 * p2.y - 2 * p1.y - p4.y;
435
- uy *= uy;
436
- let vx = 3 * p3.x - 2 * p4.x - p1.x;
437
- vx *= vx;
438
- let vy = 3 * p3.y - 2 * p4.y - p1.y;
439
- vy *= vy;
440
- if (ux < vx) {
441
- ux = vx;
442
- }
443
- if (uy < vy) {
444
- uy = vy;
445
- }
446
- return ux + uy;
447
- }
448
- function getPointsOnBezierCurveWithSplitting(points, offset, tolerance, newPoints) {
449
- const outPoints = newPoints || [];
450
- if (flatness(points, offset) < tolerance) {
451
- const p0 = points[offset + 0];
452
- if (outPoints.length) {
453
- const d = distance(outPoints[outPoints.length - 1], p0);
454
- if (d > 1) {
455
- outPoints.push(p0);
456
- }
457
- } else {
458
- outPoints.push(p0);
459
- }
460
- outPoints.push(points[offset + 3]);
461
- } else {
462
- const t = 0.5;
463
- const p1 = points[offset + 0];
464
- const p2 = points[offset + 1];
465
- const p3 = points[offset + 2];
466
- const p4 = points[offset + 3];
467
- const q1 = lerp(p1, p2, t);
468
- const q2 = lerp(p2, p3, t);
469
- const q3 = lerp(p3, p4, t);
470
- const r1 = lerp(q1, q2, t);
471
- const r2 = lerp(q2, q3, t);
472
- const red = lerp(r1, r2, t);
473
- getPointsOnBezierCurveWithSplitting([p1, q1, r1, red], 0, tolerance, outPoints);
474
- getPointsOnBezierCurveWithSplitting([red, r2, q3, p4], 0, tolerance, outPoints);
475
- }
476
- return outPoints;
477
- }
478
- function simplify(points, distance2) {
479
- return simplifyPoints(points, 0, points.length, distance2);
480
- }
481
- function simplifyPoints(points, start, end, epsilon, newPoints) {
482
- const outPoints = newPoints || [];
483
- const s = points[start];
484
- const e = points[end - 1];
485
- let maxDistSq = 0;
486
- let maxNdx = 1;
487
- for (let i = start + 1; i < end - 1; ++i) {
488
- const distSq = distanceToSegmentSq(points[i], s, e);
489
- if (distSq > maxDistSq) {
490
- maxDistSq = distSq;
491
- maxNdx = i;
492
- }
493
- }
494
- if (Math.sqrt(maxDistSq) > epsilon) {
495
- simplifyPoints(points, start, maxNdx + 1, epsilon, outPoints);
496
- simplifyPoints(points, maxNdx, end, epsilon, outPoints);
497
- } else {
498
- if (!outPoints.length) {
499
- outPoints.push(s);
500
- }
501
- outPoints.push(e);
502
- }
503
- return outPoints;
504
- }
505
- function pointsOnBezierCurves(points, tolerance = 0.15, distance2) {
506
- const newPoints = [];
507
- const numSegments = (points.length - 1) / 3;
508
- for (let i = 0; i < numSegments; i++) {
509
- const offset = i * 3;
510
- getPointsOnBezierCurveWithSplitting(points, offset, tolerance, newPoints);
511
- }
512
- if (distance2 && distance2 > 0) {
513
- return simplifyPoints(newPoints, 0, newPoints.length, distance2);
514
- }
515
- return newPoints;
516
- }
517
- export {
518
- AABB,
519
- Angle,
520
- Matrix,
521
- OBB,
522
- PI,
523
- XY,
524
- abs,
525
- acos,
526
- asin,
527
- atan,
528
- atan2,
529
- ceil,
530
- cos,
531
- divide,
532
- floor,
533
- max,
534
- min,
535
- multiply,
536
- numberHalfFix,
537
- pointsOnBezierCurves,
538
- pow2,
539
- pow3,
540
- random,
541
- round,
542
- simplify,
543
- simplifyPoints,
544
- sin,
545
- sqrt,
546
- tan,
547
- twoDecimal,
548
- xy_,
549
- xy_center,
550
- xy_client,
551
- xy_distance,
552
- xy_divide,
553
- xy_dot,
554
- xy_from,
555
- xy_getRotation,
556
- xy_minus,
557
- xy_minus_mutate,
558
- xy_multiply,
559
- xy_multiply_mutate,
560
- xy_mutate,
561
- xy_opposite,
562
- xy_plus,
563
- xy_plus_all,
564
- xy_plus_mutate,
565
- xy_rotate,
566
- xy_symmetric,
567
- xy_toArray,
568
- xy_xAxis,
569
- xy_yAxis
570
- };