@gitborlando/geo 2.1.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @gitborlando/geo
2
2
 
3
+ ## 3.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 2b4693c: 更新xy类
8
+ - 0ee9da2: XY类小幅更新
9
+
10
+ ## 3.0.0
11
+
12
+ ### Major Changes
13
+
14
+ - f121a3e: XY类破坏性更新
15
+
3
16
  ## 2.1.0
4
17
 
5
18
  ### Minor 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
@@ -212,37 +212,38 @@ declare class XY {
212
212
  x: number;
213
213
  y: number;
214
214
  constructor(x: number, y: number);
215
- from(xy: IXY): this;
216
- client(e: {
217
- clientX: number;
218
- clientY: number;
219
- }): this;
220
- center(xy: {
221
- centerX: number;
222
- centerY: number;
223
- }): this;
224
- plus(another: IXY): this;
225
- minus(another: IXY): this;
226
- multiply(...numbers: number[]): this;
227
- divide(...numbers: number[]): this;
228
- distance(another: IXY): number;
229
- rotate(origin: IXY, rotation: number): {
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
+ primitive(): {
230
225
  x: number;
231
226
  y: number;
232
227
  };
233
- dot(another: IXY): number;
234
- opposite(): {
228
+ static _(x?: number, y?: number): {
235
229
  x: number;
236
230
  y: number;
237
231
  };
238
- symmetric(another: IXY, origin: IXY): {
239
- x: number;
240
- y: number;
241
- };
242
- angle(another: IXY, origin: IXY): number;
243
232
  static of(x: number, y: number): XY;
244
233
  static from(xy: IXY): XY;
245
- static fromArray(arr: [number, number]): XY;
234
+ static center(xy: {
235
+ centerX: number;
236
+ centerY: number;
237
+ }): XY;
238
+ static leftTop(e: {
239
+ left: number;
240
+ top: number;
241
+ }): XY;
242
+ static client(e: {
243
+ clientX: number;
244
+ clientY: number;
245
+ }): XY;
246
+ static tuple(arr: [number, number]): XY;
246
247
  }
247
248
 
248
249
  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
@@ -149,72 +149,66 @@ var XY = class _XY {
149
149
  this.x = x;
150
150
  this.y = y;
151
151
  }
152
- from(xy) {
153
- this.x = xy.x;
154
- this.y = xy.y;
155
- return this;
156
- }
157
- client(e) {
158
- this.x = e.clientX;
159
- this.y = e.clientY;
160
- return this;
161
- }
162
- center(xy) {
163
- this.x = xy.centerX;
164
- this.y = xy.centerY;
165
- return this;
166
- }
167
- plus(another) {
168
- this.x = this.x + another.x;
169
- this.y = this.y + another.y;
170
- return this;
171
- }
172
- minus(another) {
173
- this.x = this.x - another.x;
174
- this.y = this.y - another.y;
175
- 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);
156
+ }
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);
176
161
  }
177
162
  multiply(...numbers) {
178
163
  const n = numbers.reduce((a, b) => a * b, 1);
179
- this.x = this.x * n;
180
- this.y = this.y * n;
181
- return this;
164
+ return _XY.of(this.x * n, this.y * n);
182
165
  }
183
166
  divide(...numbers) {
184
167
  const n = numbers.reduce((a, b) => a * b, 1);
185
- this.x = this.x / n;
186
- this.y = this.y / n;
187
- return this;
188
- }
189
- distance(another) {
190
- return sqrt((this.x - another.x) ** 2 + (this.y - another.y) ** 2);
168
+ return _XY.of(this.x / n, this.y / n);
191
169
  }
192
170
  rotate(origin, rotation) {
193
- if (rotation === 0) return this;
194
- 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));
195
173
  }
196
- dot(another) {
197
- 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);
198
176
  }
199
- opposite() {
200
- return { x: -this.x, y: -this.y };
177
+ getDot(another) {
178
+ return this.x * another.x + this.y * another.y;
201
179
  }
202
- symmetric(another, origin) {
203
- 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);
204
182
  }
205
- angle(another, origin) {
183
+ getAngle(another, origin) {
206
184
  return Angle.angleFy(
207
185
  Math.atan2(this.y - origin.y, this.x - origin.x) - Math.atan2(another.y - origin.y, another.x - origin.x)
208
186
  );
209
187
  }
188
+ primitive() {
189
+ return { x: this.x, y: this.y };
190
+ }
191
+ static _(x = 0, y = 0) {
192
+ return { x, y };
193
+ }
210
194
  static of(x, y) {
211
195
  return new _XY(x, y);
212
196
  }
213
197
  static from(xy) {
214
- return new _XY(xy.x, xy.y);
198
+ if (xy instanceof _XY) return xy;
199
+ return _XY.of(xy.x, xy.y);
200
+ }
201
+ static center(xy) {
202
+ return _XY.of(xy.centerX, xy.centerY);
203
+ }
204
+ static leftTop(e) {
205
+ return _XY.of(e.left, e.top);
206
+ }
207
+ static client(e) {
208
+ return _XY.of(e.clientX, e.clientY);
215
209
  }
216
- static fromArray(arr) {
217
- return new _XY(arr[0], arr[1]);
210
+ static tuple(arr) {
211
+ return _XY.of(arr[0], arr[1]);
218
212
  }
219
213
  };
220
214
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gitborlando/geo",
3
- "version": "2.1.0",
3
+ "version": "3.1.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {
package/src/xy.ts CHANGED
@@ -102,88 +102,82 @@ 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
+ primitive() {
152
+ return { x: this.x, y: this.y }
153
+ }
154
+
155
+ static _(x: number = 0, y: number = 0) {
156
+ return { x, y }
157
+ }
158
+
178
159
  static of(x: number, y: number) {
179
160
  return new XY(x, y)
180
161
  }
181
162
 
182
163
  static from(xy: IXY) {
183
- return new XY(xy.x, xy.y)
164
+ if (xy instanceof XY) return xy
165
+ return XY.of(xy.x, xy.y)
166
+ }
167
+
168
+ static center(xy: { centerX: number; centerY: number }) {
169
+ return XY.of(xy.centerX, xy.centerY)
170
+ }
171
+
172
+ static leftTop(e: { left: number; top: number }) {
173
+ return XY.of(e.left, e.top)
174
+ }
175
+
176
+ static client(e: { clientX: number; clientY: number }) {
177
+ return XY.of(e.clientX, e.clientY)
184
178
  }
185
179
 
186
- static fromArray(arr: [number, number]) {
187
- return new XY(arr[0], arr[1])
180
+ static tuple(arr: [number, number]) {
181
+ return XY.of(arr[0], arr[1])
188
182
  }
189
183
  }