@gitborlando/geo 2.1.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,11 @@
1
1
  # @gitborlando/geo
2
2
 
3
+ ## 3.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - f121a3e: XY类破坏性更新
8
+
3
9
  ## 2.1.0
4
10
 
5
11
  ### 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,30 @@ 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): {
230
- x: number;
231
- y: number;
232
- };
233
- dot(another: IXY): number;
234
- opposite(): {
235
- x: number;
236
- y: number;
237
- };
238
- symmetric(another: IXY, origin: IXY): {
239
- x: number;
240
- y: number;
241
- };
242
- angle(another: IXY, origin: IXY): 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;
243
224
  static of(x: number, y: number): XY;
244
225
  static from(xy: IXY): XY;
245
- static fromArray(arr: [number, number]): XY;
226
+ static center(xy: {
227
+ centerX: number;
228
+ centerY: number;
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;
246
239
  }
247
240
 
248
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
@@ -149,60 +149,38 @@ 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
  );
@@ -211,10 +189,19 @@ var XY = class _XY {
211
189
  return new _XY(x, y);
212
190
  }
213
191
  static from(xy) {
214
- return new _XY(xy.x, xy.y);
192
+ return _XY.of(xy.x, xy.y);
193
+ }
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);
215
202
  }
216
- static fromArray(arr) {
217
- return new _XY(arr[0], arr[1]);
203
+ static tuple(arr) {
204
+ return _XY.of(arr[0], arr[1]);
218
205
  }
219
206
  };
220
207
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gitborlando/geo",
3
- "version": "2.1.0",
3
+ "version": "3.0.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {
package/src/xy.ts CHANGED
@@ -102,73 +102,46 @@ 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
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)
116
109
  }
117
110
 
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
128
- }
129
-
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),
@@ -180,10 +153,22 @@ export class XY {
180
153
  }
181
154
 
182
155
  static from(xy: IXY) {
183
- 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)
184
169
  }
185
170
 
186
- static fromArray(arr: [number, number]) {
187
- return new XY(arr[0], arr[1])
171
+ static tuple(arr: [number, number]) {
172
+ return XY.of(arr[0], arr[1])
188
173
  }
189
174
  }