@gitborlando/geo 2.0.0 → 3.0.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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @gitborlando/geo
2
2
 
3
+ ## 3.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - f121a3e: XY类破坏性更新
8
+
9
+ ## 2.1.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 7bd75bc: 新增一些方法
14
+
3
15
  ## 2.0.0
4
16
 
5
17
  ### Major Changes
@@ -124,7 +124,7 @@ describe('XY class', () => {
124
124
  })
125
125
 
126
126
  it('should create from array correctly', () => {
127
- const xy = XY.fromArray([15, 25])
127
+ const xy = XY.tuple([15, 25])
128
128
  expect(xy.x).toBe(15)
129
129
  expect(xy.y).toBe(25)
130
130
  })
@@ -137,7 +137,7 @@ describe('XY class', () => {
137
137
 
138
138
  it('should calculate distance correctly', () => {
139
139
  const xy = new XY(0, 0)
140
- const distance = xy.distance({ x: 3, y: 4 })
140
+ const distance = xy.getDistance({ x: 3, y: 4 })
141
141
  expect(distance).toBe(5)
142
142
  })
143
143
  })
package/dist/index.d.ts CHANGED
@@ -51,6 +51,7 @@ declare class OBB {
51
51
  collide: (another: OBB) => boolean;
52
52
  static identityOBB(): OBB;
53
53
  static fromRect(rect: IRect, rotation?: number): OBB;
54
+ static fromCenter(center: IXY, width: number, height: number, rotation?: number): OBB;
54
55
  static fromAABB(aabb: AABB): OBB;
55
56
  }
56
57
 
@@ -107,6 +108,7 @@ declare function pow3(number: number): number;
107
108
  declare function multiply(...numbers: number[]): number;
108
109
  declare function divide(a: number, b: number): number;
109
110
  declare function numberHalfFix(number: number): number;
111
+ declare function twoDecimal(number: number): number;
110
112
 
111
113
  type IMatrix = [number, number, number, number, number, number];
112
114
  declare class Matrix {
@@ -210,36 +212,30 @@ declare class XY {
210
212
  x: number;
211
213
  y: number;
212
214
  constructor(x: number, y: number);
213
- from(xy: IXY): this;
214
- client(e: {
215
- clientX: number;
216
- clientY: number;
217
- }): this;
218
- center(xy: {
215
+ plus(...others: IXY[]): XY;
216
+ minus(...others: IXY[]): XY;
217
+ multiply(...numbers: number[]): XY;
218
+ divide(...numbers: number[]): XY;
219
+ rotate(origin: IXY, rotation: number): XY;
220
+ symmetric(another: IXY, origin: IXY): XY;
221
+ getDot(another: IXY): number;
222
+ getDistance(another: IXY): number;
223
+ getAngle(another: IXY, origin: IXY): number;
224
+ static of(x: number, y: number): XY;
225
+ static from(xy: IXY): XY;
226
+ static center(xy: {
219
227
  centerX: number;
220
228
  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;
229
+ }): XY;
230
+ static leftTop(e: {
231
+ left: number;
232
+ top: number;
233
+ }): XY;
234
+ static client(e: {
235
+ clientX: number;
236
+ clientY: number;
237
+ }): XY;
238
+ static tuple(arr: [number, number]): XY;
243
239
  }
244
240
 
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 };
241
+ 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 CHANGED
@@ -18,6 +18,9 @@ function numberHalfFix(number) {
18
18
  const halfFixed = floatPart >= 0.75 ? 1 : floatPart >= 0.25 ? 0.5 : 0;
19
19
  return integerPart + halfFixed;
20
20
  }
21
+ function twoDecimal(number) {
22
+ return Number(number.toFixed(Number.isInteger(number) ? 0 : 2));
23
+ }
21
24
 
22
25
  // src/angle.ts
23
26
  var { PI, cos, sin, tan, acos, asin, atan, atan2 } = Math;
@@ -146,69 +149,59 @@ var XY = class _XY {
146
149
  this.x = x;
147
150
  this.y = y;
148
151
  }
149
- from(xy) {
150
- this.x = xy.x;
151
- this.y = xy.y;
152
- return this;
153
- }
154
- client(e) {
155
- this.x = e.clientX;
156
- this.y = e.clientY;
157
- return this;
158
- }
159
- center(xy) {
160
- this.x = xy.centerX;
161
- this.y = xy.centerY;
162
- return this;
163
- }
164
- plus(another) {
165
- this.x = this.x + another.x;
166
- this.y = this.y + another.y;
167
- return this;
152
+ plus(...others) {
153
+ const x = others.reduce((sum, cur) => sum + cur.x, this.x);
154
+ const y = others.reduce((sum, cur) => sum + cur.y, this.y);
155
+ return _XY.of(x, y);
168
156
  }
169
- minus(another) {
170
- this.x = this.x - another.x;
171
- this.y = this.y - another.y;
172
- return this;
157
+ minus(...others) {
158
+ const x = others.reduce((sum, cur) => sum - cur.x, this.x);
159
+ const y = others.reduce((sum, cur) => sum - cur.y, this.y);
160
+ return _XY.of(x, y);
173
161
  }
174
162
  multiply(...numbers) {
175
163
  const n = numbers.reduce((a, b) => a * b, 1);
176
- this.x = this.x * n;
177
- this.y = this.y * n;
178
- return this;
164
+ return _XY.of(this.x * n, this.y * n);
179
165
  }
180
166
  divide(...numbers) {
181
167
  const n = numbers.reduce((a, b) => a * b, 1);
182
- this.x = this.x / n;
183
- this.y = this.y / n;
184
- return this;
185
- }
186
- distance(another) {
187
- return sqrt((this.x - another.x) ** 2 + (this.y - another.y) ** 2);
168
+ return _XY.of(this.x / n, this.y / n);
188
169
  }
189
170
  rotate(origin, rotation) {
190
- if (rotation === 0) return this;
191
- return Angle.rotatePoint(this.x, this.y, origin.x, origin.y, rotation);
171
+ if (rotation === 0) return _XY.from(this);
172
+ return _XY.from(Angle.rotatePoint(this.x, this.y, origin.x, origin.y, rotation));
192
173
  }
193
- dot(another) {
194
- return this.x * another.x + this.y * another.y;
174
+ symmetric(another, origin) {
175
+ return _XY.of(2 * origin.x - another.x, 2 * origin.y - another.y);
195
176
  }
196
- opposite() {
197
- return { x: -this.x, y: -this.y };
177
+ getDot(another) {
178
+ return this.x * another.x + this.y * another.y;
198
179
  }
199
- symmetric(another, origin) {
200
- return { x: 2 * origin.x - another.x, y: 2 * origin.y - another.y };
180
+ getDistance(another) {
181
+ return sqrt((this.x - another.x) ** 2 + (this.y - another.y) ** 2);
201
182
  }
202
- angle(another, origin) {
183
+ getAngle(another, origin) {
203
184
  return Angle.angleFy(
204
185
  Math.atan2(this.y - origin.y, this.x - origin.x) - Math.atan2(another.y - origin.y, another.x - origin.x)
205
186
  );
206
187
  }
188
+ static of(x, y) {
189
+ return new _XY(x, y);
190
+ }
207
191
  static from(xy) {
208
- return new _XY(xy.x, xy.y);
192
+ return _XY.of(xy.x, xy.y);
209
193
  }
210
- static fromArray(arr) {
211
- return new _XY(arr[0], arr[1]);
194
+ static center(xy) {
195
+ return _XY.of(xy.centerX, xy.centerY);
196
+ }
197
+ static leftTop(e) {
198
+ return _XY.of(e.left, e.top);
199
+ }
200
+ static client(e) {
201
+ return _XY.of(e.clientX, e.clientY);
202
+ }
203
+ static tuple(arr) {
204
+ return _XY.of(arr[0], arr[1]);
212
205
  }
213
206
  };
214
207
 
@@ -388,6 +381,12 @@ var OBB = class _OBB {
388
381
  const { x, y, width, height } = rect;
389
382
  return new _OBB(x, y, width, height, rotation);
390
383
  }
384
+ static fromCenter(center, width, height, rotation = 0) {
385
+ const dx = center.x - width / 2;
386
+ const dy = center.y - height / 2;
387
+ const xy = XY.of(dx, dy).rotate(center, rotation);
388
+ return new _OBB(xy.x, xy.y, width, height, rotation);
389
+ }
391
390
  static fromAABB(aabb) {
392
391
  const { minX, minY, maxX, maxY } = aabb;
393
392
  return new _OBB(minX, minY, maxX - minX, maxY - minY, 0);
@@ -533,6 +532,7 @@ export {
533
532
  sin,
534
533
  sqrt,
535
534
  tan,
535
+ twoDecimal,
536
536
  xy_,
537
537
  xy_center,
538
538
  xy_client,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gitborlando/geo",
3
- "version": "2.0.0",
3
+ "version": "3.0.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {
package/src/math.ts CHANGED
@@ -20,3 +20,7 @@ export function numberHalfFix(number: number) {
20
20
  const halfFixed = floatPart >= 0.75 ? 1 : floatPart >= 0.25 ? 0.5 : 0
21
21
  return integerPart + halfFixed
22
22
  }
23
+
24
+ export function twoDecimal(number: number) {
25
+ return Number(number.toFixed(Number.isInteger(number) ? 0 : 2))
26
+ }
package/src/obb.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { AABB } from './aabb'
2
2
  import { Angle } from './angle'
3
3
  import { IRect, IXY } from './types'
4
- import { xy_, xy_dot, xy_minus, xy_rotate } from './xy'
4
+ import { XY, xy_, xy_dot, xy_minus, xy_rotate } from './xy'
5
5
 
6
6
  type IAxis = { widthAxis: IXY; heightAxis: IXY }
7
7
 
@@ -101,6 +101,13 @@ export class OBB {
101
101
  return new OBB(x, y, width, height, rotation)
102
102
  }
103
103
 
104
+ static fromCenter(center: IXY, width: number, height: number, rotation = 0) {
105
+ const dx = center.x - width / 2
106
+ const dy = center.y - height / 2
107
+ const xy = XY.of(dx, dy).rotate(center, rotation)
108
+ return new OBB(xy.x, xy.y, width, height, rotation)
109
+ }
110
+
104
111
  static fromAABB(aabb: AABB): OBB {
105
112
  const { minX, minY, maxX, maxY } = aabb
106
113
  return new OBB(minX, minY, maxX - minX, maxY - minY, 0)
package/src/xy.ts CHANGED
@@ -102,84 +102,73 @@ export class XY {
102
102
  public x: number,
103
103
  public y: number,
104
104
  ) {}
105
-
106
- from(xy: IXY) {
107
- this.x = xy.x
108
- this.y = xy.y
109
- return this
110
- }
111
-
112
- client(e: { clientX: number; clientY: number }) {
113
- this.x = e.clientX
114
- this.y = e.clientY
115
- return this
116
- }
117
-
118
- center(xy: { centerX: number; centerY: number }) {
119
- this.x = xy.centerX
120
- this.y = xy.centerY
121
- return this
122
- }
123
-
124
- plus(another: IXY) {
125
- this.x = this.x + another.x
126
- this.y = this.y + another.y
127
- return this
105
+ plus(...others: IXY[]) {
106
+ const x = others.reduce((sum, cur) => sum + cur.x, this.x)
107
+ const y = others.reduce((sum, cur) => sum + cur.y, this.y)
108
+ return XY.of(x, y)
128
109
  }
129
110
 
130
- minus(another: IXY) {
131
- this.x = this.x - another.x
132
- this.y = this.y - another.y
133
- return this
111
+ minus(...others: IXY[]) {
112
+ const x = others.reduce((sum, cur) => sum - cur.x, this.x)
113
+ const y = others.reduce((sum, cur) => sum - cur.y, this.y)
114
+ return XY.of(x, y)
134
115
  }
135
116
 
136
117
  multiply(...numbers: number[]) {
137
118
  const n = numbers.reduce((a, b) => a * b, 1)
138
- this.x = this.x * n
139
- this.y = this.y * n
140
- return this
119
+ return XY.of(this.x * n, this.y * n)
141
120
  }
142
121
 
143
122
  divide(...numbers: number[]) {
144
123
  const n = numbers.reduce((a, b) => a * b, 1)
145
- this.x = this.x / n
146
- this.y = this.y / n
147
- return this
148
- }
149
-
150
- distance(another: IXY) {
151
- return sqrt((this.x - another.x) ** 2 + (this.y - another.y) ** 2)
124
+ return XY.of(this.x / n, this.y / n)
152
125
  }
153
126
 
154
127
  rotate(origin: IXY, rotation: number) {
155
- if (rotation === 0) return this
156
- return Angle.rotatePoint(this.x, this.y, origin.x, origin.y, rotation)
128
+ if (rotation === 0) return XY.from(this)
129
+ return XY.from(Angle.rotatePoint(this.x, this.y, origin.x, origin.y, rotation))
157
130
  }
158
131
 
159
- dot(another: IXY) {
160
- return this.x * another.x + this.y * another.y
132
+ symmetric(another: IXY, origin: IXY) {
133
+ return XY.of(2 * origin.x - another.x, 2 * origin.y - another.y)
161
134
  }
162
135
 
163
- opposite() {
164
- return { x: -this.x, y: -this.y }
136
+ getDot(another: IXY) {
137
+ return this.x * another.x + this.y * another.y
165
138
  }
166
139
 
167
- symmetric(another: IXY, origin: IXY) {
168
- return { x: 2 * origin.x - another.x, y: 2 * origin.y - another.y }
140
+ getDistance(another: IXY) {
141
+ return sqrt((this.x - another.x) ** 2 + (this.y - another.y) ** 2)
169
142
  }
170
143
 
171
- angle(another: IXY, origin: IXY) {
144
+ getAngle(another: IXY, origin: IXY) {
172
145
  return Angle.angleFy(
173
146
  Math.atan2(this.y - origin.y, this.x - origin.x) -
174
147
  Math.atan2(another.y - origin.y, another.x - origin.x),
175
148
  )
176
149
  }
177
150
 
151
+ static of(x: number, y: number) {
152
+ return new XY(x, y)
153
+ }
154
+
178
155
  static from(xy: IXY) {
179
- return new XY(xy.x, xy.y)
156
+ return XY.of(xy.x, xy.y)
157
+ }
158
+
159
+ static center(xy: { centerX: number; centerY: number }) {
160
+ return XY.of(xy.centerX, xy.centerY)
161
+ }
162
+
163
+ static leftTop(e: { left: number; top: number }) {
164
+ return XY.of(e.left, e.top)
165
+ }
166
+
167
+ static client(e: { clientX: number; clientY: number }) {
168
+ return XY.of(e.clientX, e.clientY)
180
169
  }
181
170
 
182
- static fromArray(arr: [number, number]) {
183
- return new XY(arr[0], arr[1])
171
+ static tuple(arr: [number, number]) {
172
+ return XY.of(arr[0], arr[1])
184
173
  }
185
174
  }