@gitborlando/geo 4.1.1 → 5.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/dist/index.js CHANGED
@@ -1,50 +1,20 @@
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
1
  // src/angle.ts
26
- var { PI, cos, sin, tan, acos, asin, atan, atan2 } = Math;
27
2
  var Angle = class _Angle {
28
3
  static cos(angle) {
29
- return cos(_Angle.radianFy(angle));
4
+ return Math.cos(_Angle.radianFy(angle));
30
5
  }
31
6
  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)));
7
+ return Math.sin(_Angle.radianFy(angle));
39
8
  }
40
- static asin(angle) {
41
- return _Angle.angleFy(asin(_Angle.radianFy(angle)));
9
+ static cosSin(angle) {
10
+ const radians = _Angle.radianFy(angle);
11
+ return { cos: Math.cos(radians), sin: Math.sin(radians) };
42
12
  }
43
- static atan(angle) {
44
- return _Angle.angleFy(atan(_Angle.radianFy(angle)));
13
+ static tan(angle) {
14
+ return Math.tan(_Angle.radianFy(angle));
45
15
  }
46
16
  static atan2(y, x) {
47
- return _Angle.angleFy(atan2(y, x));
17
+ return _Angle.angleFy(Math.atan2(y, x));
48
18
  }
49
19
  static angleFy(radians) {
50
20
  return radians * (180 / Math.PI);
@@ -53,176 +23,117 @@ var Angle = class _Angle {
53
23
  return angle * (Math.PI / 180);
54
24
  }
55
25
  static normal(angle) {
56
- return (angle + 360) % 360;
26
+ return (angle % 360 + 360) % 360;
27
+ }
28
+ static minor(angle) {
29
+ return Math.min(angle, 360 - angle);
57
30
  }
58
31
  static snap(angle, step = 90) {
59
32
  return _Angle.normal(Math.round(angle / step) * step);
60
33
  }
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
- };
34
+ static sweep(v1, v2 = XY.xAxis(), clockwise = false) {
35
+ const dot = v1.x * v2.x + v1.y * v2.y;
36
+ const det = v1.x * v2.y - v1.y * v2.x;
37
+ return _Angle.normal(_Angle.atan2(det, dot) * (clockwise ? 1 : -1));
67
38
  }
68
39
  };
69
40
 
70
41
  // 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
42
  var XY = class _XY {
148
43
  constructor(x, y) {
149
44
  this.x = x;
150
45
  this.y = y;
151
46
  }
152
- plain() {
47
+ $() {
153
48
  return { x: this.x, y: this.y };
154
49
  }
155
50
  tuple() {
156
51
  return [this.x, this.y];
157
52
  }
158
53
  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);
54
+ this.x = others.reduce((sum, cur) => sum + cur.x, this.x);
55
+ this.y = others.reduce((sum, cur) => sum + cur.y, this.y);
56
+ return this;
57
+ }
58
+ plusNum(num) {
59
+ this.x += num;
60
+ this.y += num;
61
+ return this;
162
62
  }
163
63
  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);
64
+ this.x = others.reduce((sum, cur) => sum - cur.x, this.x);
65
+ this.y = others.reduce((sum, cur) => sum - cur.y, this.y);
66
+ return this;
167
67
  }
168
68
  multiply(...numbers) {
169
69
  const n = numbers.reduce((a, b) => a * b, 1);
170
- return _XY.of(this.x * n, this.y * n);
70
+ this.x *= n;
71
+ this.y *= n;
72
+ return this;
73
+ }
74
+ multiplyNum(num) {
75
+ this.x *= num;
76
+ this.y *= num;
77
+ return this;
171
78
  }
172
79
  divide(...numbers) {
173
80
  const n = numbers.reduce((a, b) => a * b, 1);
174
- return _XY.of(this.x / n, this.y / n);
81
+ this.x /= n;
82
+ this.y /= n;
83
+ return this;
175
84
  }
176
85
  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);
86
+ const { cos, sin } = Angle.cosSin(rotation);
87
+ const dx = this.x - origin.x;
88
+ const dy = this.y - origin.y;
89
+ this.x = dx * cos - dy * sin + origin.x;
90
+ this.y = dx * sin + dy * cos + origin.y;
91
+ return this;
92
+ }
93
+ static $(x = 0, y = 0) {
94
+ return { x, y };
182
95
  }
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);
96
+ static of(xy) {
97
+ return new _XY(xy.x, xy.y);
187
98
  }
188
- getDot(another) {
189
- return this.x * another.x + this.y * another.y;
99
+ static from(x, y) {
100
+ return new _XY(x, y);
190
101
  }
191
- getDistance(another) {
192
- return sqrt((this.x - another.x) ** 2 + (this.y - another.y) ** 2);
102
+ static center(wh) {
103
+ return new _XY(wh.width / 2, wh.height / 2);
193
104
  }
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 };
105
+ static leftTop(e) {
106
+ return new _XY(e.left, e.top);
201
107
  }
202
- static of(x, y) {
203
- return new _XY(x, y);
108
+ static client(e) {
109
+ return new _XY(e.clientX, e.clientY);
204
110
  }
205
- static from(xy) {
206
- if (xy instanceof _XY) return xy;
207
- return _XY.of(xy.x, xy.y);
111
+ static xAxis(rotation = 0) {
112
+ const { cos, sin } = Angle.cosSin(rotation);
113
+ return new _XY(cos, sin);
208
114
  }
209
- static center(xy) {
210
- return _XY.of(xy.centerX, xy.centerY);
115
+ static yAxis(rotation = 0) {
116
+ const { cos, sin } = Angle.cosSin(rotation);
117
+ return new _XY(-sin, cos);
211
118
  }
212
- static leftTop(e) {
213
- return _XY.of(e.left, e.top);
119
+ static dot(self, another) {
120
+ return self.x * another.x + self.y * another.y;
214
121
  }
215
- static client(e) {
216
- return _XY.of(e.clientX, e.clientY);
122
+ static distance(self, another) {
123
+ return Math.hypot(self.x - another.x, self.y - another.y);
217
124
  }
218
- static tuple(arr) {
219
- return _XY.of(arr[0], arr[1]);
125
+ static vector(self, another) {
126
+ return new _XY(self.x - another.x, self.y - another.y);
220
127
  }
221
- static xAxis(rotation) {
222
- return _XY.of(Angle.cos(rotation), Angle.sin(rotation));
128
+ static symmetric(self, origin = _XY.$()) {
129
+ return new _XY(2 * origin.x - self.x, 2 * origin.y - self.y);
223
130
  }
224
- static yAxis(rotation) {
225
- return _XY.of(-Angle.sin(rotation), Angle.cos(rotation));
131
+ static lerp(self, origin, t) {
132
+ const distance = _XY.distance(self, origin);
133
+ return new _XY(
134
+ self.x + (self.x - origin.x) * (t / distance),
135
+ self.y + (self.y - origin.y) * (t / distance)
136
+ );
226
137
  }
227
138
  };
228
139
 
@@ -252,6 +163,29 @@ var AABB = class _AABB {
252
163
  aabb.maxY - aabb.minY
253
164
  ];
254
165
  }
166
+ static update(aabb, minX, minY, maxX, maxY) {
167
+ aabb.minX = minX;
168
+ aabb.minY = minY;
169
+ aabb.maxX = maxX;
170
+ aabb.maxY = maxY;
171
+ return aabb;
172
+ }
173
+ static updateFromRect(aabb, rect) {
174
+ return _AABB.update(
175
+ aabb,
176
+ rect.x,
177
+ rect.y,
178
+ rect.x + rect.width,
179
+ rect.y + rect.height
180
+ );
181
+ }
182
+ static shift(aabb, delta) {
183
+ aabb.minX += delta.x;
184
+ aabb.minY += delta.y;
185
+ aabb.maxX += delta.x;
186
+ aabb.maxY += delta.y;
187
+ return aabb;
188
+ }
255
189
  static collide(one, another) {
256
190
  return one.minX <= another.maxX && one.maxX >= another.minX && one.minY <= another.maxY && one.maxY >= another.minY;
257
191
  }
@@ -280,19 +214,24 @@ var AABB = class _AABB {
280
214
  );
281
215
  }
282
216
  }
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
- });
217
+ static merge(aabbList) {
218
+ if (Array.isArray(aabbList)) {
219
+ if (aabbList.length === 0) return new _AABB(0, 0, 0, 0);
220
+ } else {
221
+ if (aabbList.size === 0) return new _AABB(0, 0, 0, 0);
222
+ }
223
+ let xMin = Infinity, yMin = Infinity, xMax = -Infinity, yMax = -Infinity;
224
+ for (const aabb of aabbList) {
225
+ xMin = Math.min(xMin, aabb.minX);
226
+ yMin = Math.min(yMin, aabb.minY);
227
+ xMax = Math.max(xMax, aabb.maxX);
228
+ yMax = Math.max(yMax, aabb.maxY);
229
+ }
291
230
  return new _AABB(xMin, yMin, xMax, yMax);
292
231
  }
293
232
  static fromOBB(obb) {
294
- const width = obb.projectionLengthAt(XY._(1, 0));
295
- const height = obb.projectionLengthAt(XY._(0, 1));
233
+ const width = obb.projectAt(XY.$(1, 0));
234
+ const height = obb.projectAt(XY.$(0, 1));
296
235
  return new _AABB(
297
236
  obb.center.x - width / 2,
298
237
  obb.center.y - height / 2,
@@ -300,46 +239,25 @@ var AABB = class _AABB {
300
239
  obb.center.y + height / 2
301
240
  );
302
241
  }
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));
242
+ static updateFromOBB(aabb, obb) {
243
+ const width = obb.projectAt(XY.$(1, 0));
244
+ const height = obb.projectAt(XY.$(0, 1));
245
+ aabb.minX = obb.center.x - width / 2;
246
+ aabb.minY = obb.center.y - height / 2;
247
+ aabb.maxX = obb.center.x + width / 2;
248
+ aabb.maxY = obb.center.y + height / 2;
249
+ return aabb;
340
250
  }
341
251
  };
342
252
 
253
+ // src/misc.ts
254
+ function twoDecimal(number) {
255
+ return Number(number.toFixed(Number.isInteger(number) ? 0 : 2));
256
+ }
257
+ function minMax(val, min, max) {
258
+ return Math.min(Math.max(val, min), max);
259
+ }
260
+
343
261
  // src/obb.ts
344
262
  var OBB = class _OBB {
345
263
  constructor(x, y, width, height, rotation) {
@@ -358,48 +276,47 @@ var OBB = class _OBB {
358
276
  aabb;
359
277
  vertexes;
360
278
  get xy() {
361
- return xy_(this.x, this.y);
279
+ return XY.$(this.x, this.y);
362
280
  }
363
281
  #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);
282
+ const center = XY.center(this);
283
+ return center.rotate(this.xy, this.rotation);
366
284
  };
367
285
  #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);
286
+ const { cos, sin } = Angle.cosSin(this.rotation);
287
+ const widthAxis = XY.$(cos, -sin);
288
+ const heightAxis = XY.$(sin, cos);
372
289
  return this.axis = { widthAxis, heightAxis };
373
290
  };
374
291
  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);
292
+ const cos = Angle.cos(this.rotation);
293
+ const sin = Angle.sin(this.rotation);
294
+ const cosWidth = cos * this.width;
295
+ const sinWidth = sin * this.width;
296
+ const cosHeight = cos * this.height;
297
+ const sinHeight = sin * this.height;
298
+ const TL = XY.$(this.x, this.y);
299
+ const TR = XY.$(this.x + cosWidth, this.y + sinWidth);
300
+ const BR = XY.$(this.x + cosWidth - sinHeight, this.y + sinWidth + cosHeight);
301
+ const BL = XY.$(this.x - sinHeight, this.y + cosHeight);
385
302
  return this.vertexes = [TL, TR, BR, BL];
386
303
  };
387
304
  clone = () => {
388
305
  return new _OBB(this.x, this.y, this.width, this.height, this.rotation);
389
306
  };
390
- projectionLengthAt = (anotherAxis) => {
307
+ projectAt = (anotherAxis) => {
391
308
  const { widthAxis, heightAxis } = this.axis;
392
- return Math.abs(xy_dot(widthAxis, anotherAxis)) * this.width + Math.abs(xy_dot(heightAxis, anotherAxis)) * this.height;
309
+ return Math.abs(XY.dot(widthAxis, anotherAxis)) * this.width + Math.abs(XY.dot(heightAxis, anotherAxis)) * this.height;
393
310
  };
394
311
  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)))
312
+ const centerVector = XY.vector(this.center, another.center);
313
+ if (this.projectAt(another.axis.widthAxis) + another.width <= 2 * Math.abs(XY.dot(centerVector, another.axis.widthAxis)))
397
314
  return false;
398
- if (this.projectionLengthAt(another.axis.heightAxis) + another.height <= 2 * Math.abs(xy_dot(centerVector, another.axis.heightAxis)))
315
+ if (this.projectAt(another.axis.heightAxis) + another.height <= 2 * Math.abs(XY.dot(centerVector, another.axis.heightAxis)))
399
316
  return false;
400
- if (another.projectionLengthAt(this.axis.widthAxis) + this.width <= 2 * Math.abs(xy_dot(centerVector, this.axis.widthAxis)))
317
+ if (another.projectAt(this.axis.widthAxis) + this.width <= 2 * Math.abs(XY.dot(centerVector, this.axis.widthAxis)))
401
318
  return false;
402
- if (another.projectionLengthAt(this.axis.heightAxis) + this.height <= 2 * Math.abs(xy_dot(centerVector, this.axis.heightAxis)))
319
+ if (another.projectAt(this.axis.heightAxis) + this.height <= 2 * Math.abs(XY.dot(centerVector, this.axis.heightAxis)))
403
320
  return false;
404
321
  return true;
405
322
  };
@@ -413,7 +330,7 @@ var OBB = class _OBB {
413
330
  static fromCenter(center, width, height, rotation = 0) {
414
331
  const dx = center.x - width / 2;
415
332
  const dy = center.y - height / 2;
416
- const xy = XY.of(dx, dy).rotate(center, rotation);
333
+ const xy = XY.from(dx, dy).rotate(center, rotation);
417
334
  return new _OBB(xy.x, xy.y, width, height, rotation);
418
335
  }
419
336
  static fromAABB(aabb) {
@@ -421,167 +338,11 @@ var OBB = class _OBB {
421
338
  return new _OBB(minX, minY, maxX - minX, maxY - minY, 0);
422
339
  }
423
340
  };
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
341
  export {
535
342
  AABB,
536
343
  Angle,
537
- Matrix,
538
344
  OBB,
539
- PI,
540
345
  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
346
+ minMax,
347
+ twoDecimal
587
348
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gitborlando/geo",
3
- "version": "4.1.1",
3
+ "version": "5.1.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {