@openglobus/og 0.12.4 → 0.13.2
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/README.md +0 -2
- package/dist/@openglobus/og.esm.js +2 -2
- package/dist/@openglobus/og.esm.js.map +1 -1
- package/dist/@openglobus/og.umd.js +2 -2
- package/dist/@openglobus/og.umd.js.map +1 -1
- package/package.json +7 -7
- package/src/og/Extent.js +3 -3
- package/src/og/Globe.js +277 -275
- package/src/og/ImageCanvas.js +3 -3
- package/src/og/LonLat.js +9 -9
- package/src/og/QueueArray.js +2 -2
- package/src/og/Rectangle.js +5 -5
- package/src/og/bv/Box.js +1 -1
- package/src/og/bv/Sphere.js +2 -2
- package/src/og/camera/Camera.js +1 -1
- package/src/og/camera/PlanetCamera.js +649 -646
- package/src/og/control/LayerSwitcher.js +1 -1
- package/src/og/control/MouseNavigation.js +22 -13
- package/src/og/control/MouseWheelZoomControl.js +2 -2
- package/src/og/control/ScaleControl.js +1 -1
- package/src/og/control/TouchNavigation.js +319 -319
- package/src/og/control/ZoomControl.js +132 -132
- package/src/og/control/visibleExtent/Segment.js +12 -12
- package/src/og/ellipsoid/Ellipsoid.js +577 -567
- package/src/og/entity/BillboardHandler.js +878 -1051
- package/src/og/entity/Entity.js +1 -1
- package/src/og/entity/EntityCollection.js +847 -857
- package/src/og/entity/GeoObjectHandler.js +2 -2
- package/src/og/entity/LabelHandler.js +887 -1040
- package/src/og/entity/LabelWorker.js +0 -6
- package/src/og/entity/PointCloud.js +495 -495
- package/src/og/entity/Polyline.js +2218 -2222
- package/src/og/entity/RayHandler.js +1 -1
- package/src/og/layer/CanvasTiles.js +6 -3
- package/src/og/layer/GeoImage.js +1 -1
- package/src/og/layer/KML.js +5 -5
- package/src/og/layer/Layer.js +800 -752
- package/src/og/layer/Vector.js +997 -1010
- package/src/og/layer/XYZ.js +359 -357
- package/src/og/math/Line2.js +4 -4
- package/src/og/math/Quat.js +2 -2
- package/src/og/math/Ray.js +2 -2
- package/src/og/math/Vec2.js +524 -524
- package/src/og/math/Vec3.js +900 -900
- package/src/og/math/Vec4.js +261 -261
- package/src/og/math.js +397 -398
- package/src/og/quadTree/EntityCollectionNode.js +389 -387
- package/src/og/renderer/Renderer.js +14 -20
- package/src/og/renderer/RendererEvents.js +1034 -1045
- package/src/og/scene/Planet.js +12 -9
- package/src/og/scene/RenderNode.js +347 -354
- package/src/og/segment/Segment.js +3 -1
- package/src/og/segment/SegmentLonLat.js +1 -4
- package/src/og/shaders/billboard.js +220 -204
- package/src/og/shaders/drawnode.js +607 -603
- package/src/og/shaders/label.js +576 -456
- package/src/og/shaders/polyline.js +365 -361
- package/src/og/terrain/MapboxTerrain.js +1 -1
- package/src/og/utils/FontAtlas.js +209 -200
- package/src/og/utils/GeoImageCreator.js +4 -5
- package/src/og/utils/TextureAtlas.js +4 -4
- package/src/og/utils/VectorTileCreator.js +414 -414
- package/src/og/utils/shared.js +1 -1
- package/src/og/webgl/Handler.js +28 -60
- package/src/og/webgl/Multisample.js +260 -260
- package/src/og/webgl/Program.js +411 -396
- package/types/bv/Box.d.ts +1 -1
- package/types/bv/Sphere.d.ts +1 -1
- package/types/camera/PlanetCamera.d.ts +3 -3
- package/types/control/LayerSwitcher.d.ts +1 -1
- package/types/ellipsoid/Ellipsoid.d.ts +9 -2
- package/types/entity/BillboardHandler.d.ts +0 -4
- package/types/entity/EntityCollection.d.ts +0 -8
- package/types/entity/LabelHandler.d.ts +0 -2
- package/types/entity/Polyline.d.ts +1 -1
- package/types/layer/KML.d.ts +11 -11
- package/types/layer/Layer.d.ts +4 -0
- package/types/layer/Vector.d.ts +1 -8
- package/types/math/Line2.d.ts +4 -4
- package/types/math/Ray.d.ts +2 -2
- package/types/math/Vec2.d.ts +1 -1
- package/types/math/Vec3.d.ts +1 -1
- package/types/math/Vec4.d.ts +2 -2
- package/types/math.d.ts +1 -1
- package/types/renderer/Renderer.d.ts +1 -1
- package/types/renderer/RendererEvents.d.ts +1 -1
- package/types/shaders/label.d.ts +1 -1
- package/types/utils/FontAtlas.d.ts +3 -2
- package/types/utils/TextureAtlas.d.ts +3 -3
- package/types/utils/VectorTileCreator.d.ts +4 -4
- package/types/utils/shared.d.ts +1 -1
- package/types/webgl/Handler.d.ts +6 -31
- package/types/webgl/Program.d.ts +2 -0
package/src/og/math/Vec2.js
CHANGED
|
@@ -1,524 +1,524 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module og/math/Vec2
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
"use strict";
|
|
6
|
-
|
|
7
|
-
import { Vec3 } from "./Vec3.js";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Class represents a 3d vector.
|
|
11
|
-
* @class
|
|
12
|
-
* @param {number} [x] - First value.
|
|
13
|
-
* @param {number} [y] - Second value.
|
|
14
|
-
*/
|
|
15
|
-
export class Vec2 {
|
|
16
|
-
constructor(x, y) {
|
|
17
|
-
/**
|
|
18
|
-
* @public
|
|
19
|
-
* @type {number}
|
|
20
|
-
*/
|
|
21
|
-
this.x = x || 0.0;
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* @public
|
|
25
|
-
* @type {number}
|
|
26
|
-
*/
|
|
27
|
-
this.y = y || 0.0;
|
|
28
|
-
}
|
|
29
|
-
/** @const */
|
|
30
|
-
static get UP() {
|
|
31
|
-
return new Vec2(0, 1);
|
|
32
|
-
}
|
|
33
|
-
/** @const */
|
|
34
|
-
static get DOWN() {
|
|
35
|
-
return new Vec2(0, -1);
|
|
36
|
-
}
|
|
37
|
-
/** @const */
|
|
38
|
-
static get RIGHT() {
|
|
39
|
-
return new Vec2(1, 0);
|
|
40
|
-
}
|
|
41
|
-
/** @const */
|
|
42
|
-
static get LEFT() {
|
|
43
|
-
return new Vec2(-1, 0);
|
|
44
|
-
}
|
|
45
|
-
/** @const */
|
|
46
|
-
static get ZERO() {
|
|
47
|
-
return new Vec2();
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Returns summary vector.
|
|
52
|
-
* @static
|
|
53
|
-
* @param {math.Vec2} a - First vector.
|
|
54
|
-
* @param {math.Vec2} b - Second vector.
|
|
55
|
-
* @returns {math.Vec2} - Summary vector.
|
|
56
|
-
*/
|
|
57
|
-
static add(a, b) {
|
|
58
|
-
var res = new Vec2(a.x, a.y);
|
|
59
|
-
res.addA(b);
|
|
60
|
-
return res;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Returns two vectors subtraction.
|
|
65
|
-
* @static
|
|
66
|
-
* @param {math.Vec2} a - First vector.
|
|
67
|
-
* @param {math.Vec2} b - Second vector.
|
|
68
|
-
* @returns {math.Vec2} - Vectors subtraction.
|
|
69
|
-
*/
|
|
70
|
-
static sub(a, b) {
|
|
71
|
-
var res = new Vec2(a.x, a.y);
|
|
72
|
-
res.subA(b);
|
|
73
|
-
return res;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Returns scaled vector.
|
|
78
|
-
* @static
|
|
79
|
-
* @param {math.Vec2} a - Input vector.
|
|
80
|
-
* @param {number} scale - Scale value.
|
|
81
|
-
* @returns {math.Vec2}
|
|
82
|
-
*/
|
|
83
|
-
static scale(a, scale) {
|
|
84
|
-
var res = new Vec2(a.x, a.y);
|
|
85
|
-
res.scale(scale);
|
|
86
|
-
return res;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Returns two vectors production.
|
|
91
|
-
* @static
|
|
92
|
-
* @param {math.Vec2} a - First vector.
|
|
93
|
-
* @param {math.Vec2} b - Second vector.
|
|
94
|
-
* @returns {math.Vec2}
|
|
95
|
-
*/
|
|
96
|
-
static mul(a, b) {
|
|
97
|
-
var res = new Vec2(a.x, a.y);
|
|
98
|
-
res.mulA(b);
|
|
99
|
-
return res;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Returns vector components division product one to another.
|
|
104
|
-
* @static
|
|
105
|
-
* @param {math.Vec2} a - First vector.
|
|
106
|
-
* @param {math.Vec2} b - Second vector.
|
|
107
|
-
* @returns {math.Vec2}
|
|
108
|
-
*/
|
|
109
|
-
static div(a, b) {
|
|
110
|
-
var res = new Vec2(a.x, a.y);
|
|
111
|
-
res.divA(b);
|
|
112
|
-
return res;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Get projection of the first vector to the second.
|
|
117
|
-
* @static
|
|
118
|
-
* @param {math.Vec2} b - First vector.
|
|
119
|
-
* @param {math.Vec2} a - Second vector.
|
|
120
|
-
* @returns {math.Vec2}
|
|
121
|
-
*/
|
|
122
|
-
static proj_b_to_a(b, a) {
|
|
123
|
-
return a.scaleTo(a.dot(b) / a.dot(a));
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Gets angle between two vectors.
|
|
128
|
-
* @static
|
|
129
|
-
* @param {math.Vec2} a - First vector.
|
|
130
|
-
* @param {math.Vec2} b - Second vector.
|
|
131
|
-
* @returns {number}
|
|
132
|
-
*/
|
|
133
|
-
static angle(a, b) {
|
|
134
|
-
return Math.acos(a.dot(b) / Math.sqrt(a.length2() * b.length2()));
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Makes vectors normalized and orthogonal to each other.
|
|
139
|
-
* @static
|
|
140
|
-
* @param {math.Vec2} normal - Normal vector.
|
|
141
|
-
* @param {math.Vec2} tangent - Tangent vector.
|
|
142
|
-
* @returns {math.Vec2}
|
|
143
|
-
*/
|
|
144
|
-
static orthoNormalize(normal, tangent) {
|
|
145
|
-
normal = normal.norm();
|
|
146
|
-
normal.scale(tangent.dot(normal));
|
|
147
|
-
return tangent.sub(normal).normalize();
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Converts to 3d vector, third value is 0.0.
|
|
152
|
-
* @public
|
|
153
|
-
* @returns {Vec3}
|
|
154
|
-
*/
|
|
155
|
-
toVector3() {
|
|
156
|
-
return new Vec3(this.x, this.y, 0);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Returns clone vector.
|
|
161
|
-
* @public
|
|
162
|
-
* @returns {math.Vec2}
|
|
163
|
-
*/
|
|
164
|
-
clone() {
|
|
165
|
-
return new Vec2(this.x, this.y);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Compares with vector. Returns true if it equals another.
|
|
170
|
-
* @public
|
|
171
|
-
* @param {math.Vec2} p - Vector to compare.
|
|
172
|
-
* @returns {boolean}
|
|
173
|
-
*/
|
|
174
|
-
equal(p) {
|
|
175
|
-
return this.x === p.x && this.y === p.y;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Copy input vector's values.
|
|
180
|
-
* @param {math.Vec2} point2 - Vector to copy.
|
|
181
|
-
* @returns {math.Vec2}
|
|
182
|
-
*/
|
|
183
|
-
copy(point2) {
|
|
184
|
-
this.x = point2.x;
|
|
185
|
-
this.y = point2.y;
|
|
186
|
-
return this;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Gets vector's length.
|
|
191
|
-
* @public
|
|
192
|
-
* @returns {number}
|
|
193
|
-
*/
|
|
194
|
-
length() {
|
|
195
|
-
return Math.sqrt(this.x * this.x + this.y * this.y);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Returns squared vector's length.
|
|
200
|
-
* @public
|
|
201
|
-
* @returns {number}
|
|
202
|
-
*/
|
|
203
|
-
length2() {
|
|
204
|
-
return this.x * this.x + this.y * this.y;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Adds vector to the current.
|
|
209
|
-
* @public
|
|
210
|
-
* @param {math.Vec2}
|
|
211
|
-
* @returns {math.Vec2}
|
|
212
|
-
*/
|
|
213
|
-
addA(v) {
|
|
214
|
-
this.x += v.x;
|
|
215
|
-
this.y += v.y;
|
|
216
|
-
return this;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Summarize two vectors.
|
|
221
|
-
* @public
|
|
222
|
-
* @param {math.Vec2}
|
|
223
|
-
* @returns {math.Vec2}
|
|
224
|
-
*/
|
|
225
|
-
add(v) {
|
|
226
|
-
return new Vec2(this.x + v.x, this.y + v.y);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* Subtract vector from the current where results saved on the current instance.
|
|
231
|
-
* @public
|
|
232
|
-
* @param {math.Vec2} v - Subtract vector.
|
|
233
|
-
* @returns {math.Vec2}
|
|
234
|
-
*/
|
|
235
|
-
subA(v) {
|
|
236
|
-
this.x -= v.x;
|
|
237
|
-
this.y -= v.y;
|
|
238
|
-
return this;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Subtract vector from the current.
|
|
243
|
-
* @public
|
|
244
|
-
* @param {math.Vec2} v - Subtract vector.
|
|
245
|
-
* @returns {math.Vec2}
|
|
246
|
-
*/
|
|
247
|
-
sub(v) {
|
|
248
|
-
return new Vec2(this.x - v.x, this.y - v.y);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Scale current vector.
|
|
253
|
-
* @public
|
|
254
|
-
* @param {number} scale - Scale value.
|
|
255
|
-
* @returns {math.Vec2}
|
|
256
|
-
*/
|
|
257
|
-
scale(scale) {
|
|
258
|
-
this.x *= scale;
|
|
259
|
-
this.y *= scale;
|
|
260
|
-
return this;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
/**
|
|
264
|
-
* Scale current vector to another instance.
|
|
265
|
-
* @public
|
|
266
|
-
* @param {number} scale - Scale value.
|
|
267
|
-
* @returns {math.Vec2}
|
|
268
|
-
*/
|
|
269
|
-
scaleTo(scale) {
|
|
270
|
-
return new Vec2(this.x * scale, this.y * scale);
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* Multiply current vector object to another and store result in the current instance.
|
|
275
|
-
* @public
|
|
276
|
-
* @param {math.Vec2} vec - Multiply vector.
|
|
277
|
-
* @returns {math.Vec2}
|
|
278
|
-
*/
|
|
279
|
-
mulA(vec) {
|
|
280
|
-
this.x *= vec.x;
|
|
281
|
-
this.y *= vec.y;
|
|
282
|
-
return this;
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
/**
|
|
286
|
-
* Multiply current vector object to another and returns new vector instance.
|
|
287
|
-
* @public
|
|
288
|
-
* @param {math.Vec2} vec - Multiply vector.
|
|
289
|
-
* @returns {math.Vec2}
|
|
290
|
-
*/
|
|
291
|
-
mul(vec) {
|
|
292
|
-
return new Vec2(this.x * vec.x, this.y * vec.y);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
* Divide current vector's components to another. Results stores in the current vector object.
|
|
297
|
-
* @public
|
|
298
|
-
* @param {math.Vec2}
|
|
299
|
-
* @returns {math.Vec2}
|
|
300
|
-
*/
|
|
301
|
-
divA(vec) {
|
|
302
|
-
this.x /= vec.x;
|
|
303
|
-
this.y /= vec.y;
|
|
304
|
-
return this;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* Gets vectors dot production.
|
|
309
|
-
* @public
|
|
310
|
-
* @param {math.Vec2} v - Another vector.
|
|
311
|
-
* @returns {number}
|
|
312
|
-
*/
|
|
313
|
-
dot(v) {
|
|
314
|
-
return v.x * this.x + v.y * this.y;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* Gets vectors dot production.
|
|
319
|
-
* @public
|
|
320
|
-
* @param {Array.<number>} arr - Array vector. (exactly 2 entries)
|
|
321
|
-
* @returns {number}
|
|
322
|
-
*/
|
|
323
|
-
dotArr(arr) {
|
|
324
|
-
return arr[0] * this.x + arr[1] * this.y;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
* Gets vectors cross production.
|
|
329
|
-
* @public
|
|
330
|
-
* @param {math.Vec2} v - Another vector.
|
|
331
|
-
* @returns {math.Vec2}
|
|
332
|
-
*/
|
|
333
|
-
cross(v) {
|
|
334
|
-
return this.x * v.y - this.y * v.x;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
/**
|
|
338
|
-
* Sets vector to zero.
|
|
339
|
-
* @public
|
|
340
|
-
* @returns {math.Vec2}
|
|
341
|
-
*/
|
|
342
|
-
clear() {
|
|
343
|
-
this.x = this.y = 0;
|
|
344
|
-
return this;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
/**
|
|
348
|
-
* Returns normalized vector.
|
|
349
|
-
* @public
|
|
350
|
-
* @returns {math.Vec2}
|
|
351
|
-
*/
|
|
352
|
-
normal() {
|
|
353
|
-
var res = new Vec2();
|
|
354
|
-
res.copy(this);
|
|
355
|
-
|
|
356
|
-
var length = 1.0 / res.length();
|
|
357
|
-
|
|
358
|
-
res.x *= length;
|
|
359
|
-
res.y *= length;
|
|
360
|
-
|
|
361
|
-
return res;
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
/**
|
|
365
|
-
* Normalize current vector.
|
|
366
|
-
* @public
|
|
367
|
-
* @returns {math.Vec2}
|
|
368
|
-
*/
|
|
369
|
-
normalize() {
|
|
370
|
-
var length = 1.0 / this.length();
|
|
371
|
-
|
|
372
|
-
this.x *= length;
|
|
373
|
-
this.y *= length;
|
|
374
|
-
|
|
375
|
-
return this;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
/**
|
|
379
|
-
* Converts vector to a number array.
|
|
380
|
-
* @public
|
|
381
|
-
* @returns {Array.<number>} - (exactly 2 entries)
|
|
382
|
-
*/
|
|
383
|
-
toVec() {
|
|
384
|
-
return [this.x, this.y];
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
/**
|
|
388
|
-
* Gets distance to point.
|
|
389
|
-
* @public
|
|
390
|
-
* @param {math.Vec2} p - Distant point.
|
|
391
|
-
* @returns {number}
|
|
392
|
-
*/
|
|
393
|
-
distance(p) {
|
|
394
|
-
var vec = Vec2.sub(this, p);
|
|
395
|
-
return vec.length();
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
/**
|
|
399
|
-
* Sets vector's values.
|
|
400
|
-
* @public
|
|
401
|
-
* @param {number} x - Value X.
|
|
402
|
-
* @param {number} y - Value Y.
|
|
403
|
-
* @returns {math.Vec2}
|
|
404
|
-
*/
|
|
405
|
-
set(x, y) {
|
|
406
|
-
this.x = x;
|
|
407
|
-
this.y = y;
|
|
408
|
-
return this;
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
/**
|
|
412
|
-
* Negate current vector.
|
|
413
|
-
* @public
|
|
414
|
-
* @returns {math.Vec2}
|
|
415
|
-
*/
|
|
416
|
-
negate() {
|
|
417
|
-
this.x = -this.x;
|
|
418
|
-
this.y = -this.y;
|
|
419
|
-
return this;
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
/**
|
|
423
|
-
* Negate current vector to another instance.
|
|
424
|
-
* @public
|
|
425
|
-
* @returns {math.Vec2}
|
|
426
|
-
*/
|
|
427
|
-
negateTo() {
|
|
428
|
-
return new Vec2(-this.x, -this.y);
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
/**
|
|
432
|
-
* Gets projected point coordinates of the current vector on the ray.
|
|
433
|
-
* @public
|
|
434
|
-
* @param {math.Vec2} pos - Ray position.
|
|
435
|
-
* @param {math.Vec2} direction - Ray direction.
|
|
436
|
-
* @returns {math.Vec2}
|
|
437
|
-
*/
|
|
438
|
-
projToRay(pos, direction) {
|
|
439
|
-
var v = Vec2.proj_b_to_a(Vec2.sub(this, pos), direction);
|
|
440
|
-
v.add(pos);
|
|
441
|
-
return v;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
/**
|
|
445
|
-
* Gets angle between two vectors.
|
|
446
|
-
* @public
|
|
447
|
-
* @param {math.Vec2} a - Another vector.
|
|
448
|
-
* @returns {number}
|
|
449
|
-
*/
|
|
450
|
-
angle(a) {
|
|
451
|
-
return Vec2.angle(this, a);
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
/**
|
|
455
|
-
* Returns two vectors linear interpolation.
|
|
456
|
-
* @public
|
|
457
|
-
* @param {math.Vec2} v2 - End vector.
|
|
458
|
-
* @param {number} l - Interpolate value.
|
|
459
|
-
* @returns {math.Vec2}
|
|
460
|
-
*/
|
|
461
|
-
lerp(v1, v2, l) {
|
|
462
|
-
var res = Vec2.clone(this);
|
|
463
|
-
if (l <= 0.0) {
|
|
464
|
-
res.copy(v1);
|
|
465
|
-
} else if (l >= 1.0) {
|
|
466
|
-
res.copy(v2);
|
|
467
|
-
} else {
|
|
468
|
-
res = Vec2.add(v1, Vec2.sub(v2, v1).scale(l));
|
|
469
|
-
}
|
|
470
|
-
return res;
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
static get LERP_DELTA() {
|
|
474
|
-
return 1e-6;
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
/**
|
|
478
|
-
* Spherically interpolates between two vectors.
|
|
479
|
-
* Interpolates between current and v2 vector by amount t. The difference between this and linear interpolation (aka, "lerp") is that
|
|
480
|
-
* the vectors are treated as directions rather than points in space. The direction of the returned vector is interpolated
|
|
481
|
-
* by the angle and its magnitude is interpolated between the magnitudes of from and to.
|
|
482
|
-
* @public
|
|
483
|
-
* @param {math.Vec2} v2 -
|
|
484
|
-
* @param {number} t - The parameter t is clamped to the range [0, 1].
|
|
485
|
-
* @returns {math.Vec2}
|
|
486
|
-
*/
|
|
487
|
-
slerp(v2, t) {
|
|
488
|
-
var res = new Vec2();
|
|
489
|
-
|
|
490
|
-
if (t <= 0.0) {
|
|
491
|
-
res.copy(this);
|
|
492
|
-
return;
|
|
493
|
-
} else if (t >= 1.0) {
|
|
494
|
-
res.copy(v2);
|
|
495
|
-
return;
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
var omega, sinom, scale0, scale1;
|
|
499
|
-
var cosom = this.dot(v2);
|
|
500
|
-
|
|
501
|
-
if (1.0 - cosom > Vec2.LERP_DELTA) {
|
|
502
|
-
omega = Math.acos(cosom);
|
|
503
|
-
sinom = Math.sin(omega);
|
|
504
|
-
scale0 = Math.sin((1.0 - t) * omega) / sinom;
|
|
505
|
-
scale1 = Math.sin(t * omega) / sinom;
|
|
506
|
-
} else {
|
|
507
|
-
scale0 = 1.0 - t;
|
|
508
|
-
scale1 = t;
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
return Vec2.add(this.scale(scale0), v2.scale(scale1));
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
/**
|
|
516
|
-
* Vector 2d object creator.
|
|
517
|
-
* @function
|
|
518
|
-
* @param {number} [x] - First cvalue.
|
|
519
|
-
* @param {number} [y] - Second value.
|
|
520
|
-
* @returns {math.Vec2}
|
|
521
|
-
*/
|
|
522
|
-
export function vec2(x, y) {
|
|
523
|
-
return new Vec2(x, y);
|
|
524
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @module og/math/Vec2
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
7
|
+
import { Vec3 } from "./Vec3.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Class represents a 3d vector.
|
|
11
|
+
* @class
|
|
12
|
+
* @param {number} [x] - First value.
|
|
13
|
+
* @param {number} [y] - Second value.
|
|
14
|
+
*/
|
|
15
|
+
export class Vec2 {
|
|
16
|
+
constructor(x = 0.0, y = 0.0) {
|
|
17
|
+
/**
|
|
18
|
+
* @public
|
|
19
|
+
* @type {number}
|
|
20
|
+
*/
|
|
21
|
+
this.x = x || 0.0;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @public
|
|
25
|
+
* @type {number}
|
|
26
|
+
*/
|
|
27
|
+
this.y = y || 0.0;
|
|
28
|
+
}
|
|
29
|
+
/** @const */
|
|
30
|
+
static get UP() {
|
|
31
|
+
return new Vec2(0, 1);
|
|
32
|
+
}
|
|
33
|
+
/** @const */
|
|
34
|
+
static get DOWN() {
|
|
35
|
+
return new Vec2(0, -1);
|
|
36
|
+
}
|
|
37
|
+
/** @const */
|
|
38
|
+
static get RIGHT() {
|
|
39
|
+
return new Vec2(1, 0);
|
|
40
|
+
}
|
|
41
|
+
/** @const */
|
|
42
|
+
static get LEFT() {
|
|
43
|
+
return new Vec2(-1, 0);
|
|
44
|
+
}
|
|
45
|
+
/** @const */
|
|
46
|
+
static get ZERO() {
|
|
47
|
+
return new Vec2();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Returns summary vector.
|
|
52
|
+
* @static
|
|
53
|
+
* @param {math.Vec2} a - First vector.
|
|
54
|
+
* @param {math.Vec2} b - Second vector.
|
|
55
|
+
* @returns {math.Vec2} - Summary vector.
|
|
56
|
+
*/
|
|
57
|
+
static add(a, b) {
|
|
58
|
+
var res = new Vec2(a.x, a.y);
|
|
59
|
+
res.addA(b);
|
|
60
|
+
return res;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Returns two vectors subtraction.
|
|
65
|
+
* @static
|
|
66
|
+
* @param {math.Vec2} a - First vector.
|
|
67
|
+
* @param {math.Vec2} b - Second vector.
|
|
68
|
+
* @returns {math.Vec2} - Vectors subtraction.
|
|
69
|
+
*/
|
|
70
|
+
static sub(a, b) {
|
|
71
|
+
var res = new Vec2(a.x, a.y);
|
|
72
|
+
res.subA(b);
|
|
73
|
+
return res;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Returns scaled vector.
|
|
78
|
+
* @static
|
|
79
|
+
* @param {math.Vec2} a - Input vector.
|
|
80
|
+
* @param {number} scale - Scale value.
|
|
81
|
+
* @returns {math.Vec2}
|
|
82
|
+
*/
|
|
83
|
+
static scale(a, scale) {
|
|
84
|
+
var res = new Vec2(a.x, a.y);
|
|
85
|
+
res.scale(scale);
|
|
86
|
+
return res;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Returns two vectors production.
|
|
91
|
+
* @static
|
|
92
|
+
* @param {math.Vec2} a - First vector.
|
|
93
|
+
* @param {math.Vec2} b - Second vector.
|
|
94
|
+
* @returns {math.Vec2}
|
|
95
|
+
*/
|
|
96
|
+
static mul(a, b) {
|
|
97
|
+
var res = new Vec2(a.x, a.y);
|
|
98
|
+
res.mulA(b);
|
|
99
|
+
return res;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Returns vector components division product one to another.
|
|
104
|
+
* @static
|
|
105
|
+
* @param {math.Vec2} a - First vector.
|
|
106
|
+
* @param {math.Vec2} b - Second vector.
|
|
107
|
+
* @returns {math.Vec2}
|
|
108
|
+
*/
|
|
109
|
+
static div(a, b) {
|
|
110
|
+
var res = new Vec2(a.x, a.y);
|
|
111
|
+
res.divA(b);
|
|
112
|
+
return res;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Get projection of the first vector to the second.
|
|
117
|
+
* @static
|
|
118
|
+
* @param {math.Vec2} b - First vector.
|
|
119
|
+
* @param {math.Vec2} a - Second vector.
|
|
120
|
+
* @returns {math.Vec2}
|
|
121
|
+
*/
|
|
122
|
+
static proj_b_to_a(b, a) {
|
|
123
|
+
return a.scaleTo(a.dot(b) / a.dot(a));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Gets angle between two vectors.
|
|
128
|
+
* @static
|
|
129
|
+
* @param {math.Vec2} a - First vector.
|
|
130
|
+
* @param {math.Vec2} b - Second vector.
|
|
131
|
+
* @returns {number}
|
|
132
|
+
*/
|
|
133
|
+
static angle(a, b) {
|
|
134
|
+
return Math.acos(a.dot(b) / Math.sqrt(a.length2() * b.length2()));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Makes vectors normalized and orthogonal to each other.
|
|
139
|
+
* @static
|
|
140
|
+
* @param {math.Vec2} normal - Normal vector.
|
|
141
|
+
* @param {math.Vec2} tangent - Tangent vector.
|
|
142
|
+
* @returns {math.Vec2}
|
|
143
|
+
*/
|
|
144
|
+
static orthoNormalize(normal, tangent) {
|
|
145
|
+
normal = normal.norm();
|
|
146
|
+
normal.scale(tangent.dot(normal));
|
|
147
|
+
return tangent.sub(normal).normalize();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Converts to 3d vector, third value is 0.0.
|
|
152
|
+
* @public
|
|
153
|
+
* @returns {Vec3}
|
|
154
|
+
*/
|
|
155
|
+
toVector3() {
|
|
156
|
+
return new Vec3(this.x, this.y, 0);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Returns clone vector.
|
|
161
|
+
* @public
|
|
162
|
+
* @returns {math.Vec2}
|
|
163
|
+
*/
|
|
164
|
+
clone() {
|
|
165
|
+
return new Vec2(this.x, this.y);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Compares with vector. Returns true if it equals another.
|
|
170
|
+
* @public
|
|
171
|
+
* @param {math.Vec2} p - Vector to compare.
|
|
172
|
+
* @returns {boolean}
|
|
173
|
+
*/
|
|
174
|
+
equal(p) {
|
|
175
|
+
return this.x === p.x && this.y === p.y;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Copy input vector's values.
|
|
180
|
+
* @param {math.Vec2} point2 - Vector to copy.
|
|
181
|
+
* @returns {math.Vec2}
|
|
182
|
+
*/
|
|
183
|
+
copy(point2) {
|
|
184
|
+
this.x = point2.x;
|
|
185
|
+
this.y = point2.y;
|
|
186
|
+
return this;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Gets vector's length.
|
|
191
|
+
* @public
|
|
192
|
+
* @returns {number}
|
|
193
|
+
*/
|
|
194
|
+
length() {
|
|
195
|
+
return Math.sqrt(this.x * this.x + this.y * this.y);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Returns squared vector's length.
|
|
200
|
+
* @public
|
|
201
|
+
* @returns {number}
|
|
202
|
+
*/
|
|
203
|
+
length2() {
|
|
204
|
+
return this.x * this.x + this.y * this.y;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Adds vector to the current.
|
|
209
|
+
* @public
|
|
210
|
+
* @param {math.Vec2}
|
|
211
|
+
* @returns {math.Vec2}
|
|
212
|
+
*/
|
|
213
|
+
addA(v) {
|
|
214
|
+
this.x += v.x;
|
|
215
|
+
this.y += v.y;
|
|
216
|
+
return this;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Summarize two vectors.
|
|
221
|
+
* @public
|
|
222
|
+
* @param {math.Vec2}
|
|
223
|
+
* @returns {math.Vec2}
|
|
224
|
+
*/
|
|
225
|
+
add(v) {
|
|
226
|
+
return new Vec2(this.x + v.x, this.y + v.y);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Subtract vector from the current where results saved on the current instance.
|
|
231
|
+
* @public
|
|
232
|
+
* @param {math.Vec2} v - Subtract vector.
|
|
233
|
+
* @returns {math.Vec2}
|
|
234
|
+
*/
|
|
235
|
+
subA(v) {
|
|
236
|
+
this.x -= v.x;
|
|
237
|
+
this.y -= v.y;
|
|
238
|
+
return this;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Subtract vector from the current.
|
|
243
|
+
* @public
|
|
244
|
+
* @param {math.Vec2} v - Subtract vector.
|
|
245
|
+
* @returns {math.Vec2}
|
|
246
|
+
*/
|
|
247
|
+
sub(v) {
|
|
248
|
+
return new Vec2(this.x - v.x, this.y - v.y);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Scale current vector.
|
|
253
|
+
* @public
|
|
254
|
+
* @param {number} scale - Scale value.
|
|
255
|
+
* @returns {math.Vec2}
|
|
256
|
+
*/
|
|
257
|
+
scale(scale) {
|
|
258
|
+
this.x *= scale;
|
|
259
|
+
this.y *= scale;
|
|
260
|
+
return this;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Scale current vector to another instance.
|
|
265
|
+
* @public
|
|
266
|
+
* @param {number} scale - Scale value.
|
|
267
|
+
* @returns {math.Vec2}
|
|
268
|
+
*/
|
|
269
|
+
scaleTo(scale) {
|
|
270
|
+
return new Vec2(this.x * scale, this.y * scale);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Multiply current vector object to another and store result in the current instance.
|
|
275
|
+
* @public
|
|
276
|
+
* @param {math.Vec2} vec - Multiply vector.
|
|
277
|
+
* @returns {math.Vec2}
|
|
278
|
+
*/
|
|
279
|
+
mulA(vec) {
|
|
280
|
+
this.x *= vec.x;
|
|
281
|
+
this.y *= vec.y;
|
|
282
|
+
return this;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Multiply current vector object to another and returns new vector instance.
|
|
287
|
+
* @public
|
|
288
|
+
* @param {math.Vec2} vec - Multiply vector.
|
|
289
|
+
* @returns {math.Vec2}
|
|
290
|
+
*/
|
|
291
|
+
mul(vec) {
|
|
292
|
+
return new Vec2(this.x * vec.x, this.y * vec.y);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Divide current vector's components to another. Results stores in the current vector object.
|
|
297
|
+
* @public
|
|
298
|
+
* @param {math.Vec2}
|
|
299
|
+
* @returns {math.Vec2}
|
|
300
|
+
*/
|
|
301
|
+
divA(vec) {
|
|
302
|
+
this.x /= vec.x;
|
|
303
|
+
this.y /= vec.y;
|
|
304
|
+
return this;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Gets vectors dot production.
|
|
309
|
+
* @public
|
|
310
|
+
* @param {math.Vec2} v - Another vector.
|
|
311
|
+
* @returns {number}
|
|
312
|
+
*/
|
|
313
|
+
dot(v) {
|
|
314
|
+
return v.x * this.x + v.y * this.y;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Gets vectors dot production.
|
|
319
|
+
* @public
|
|
320
|
+
* @param {Array.<number>} arr - Array vector. (exactly 2 entries)
|
|
321
|
+
* @returns {number}
|
|
322
|
+
*/
|
|
323
|
+
dotArr(arr) {
|
|
324
|
+
return arr[0] * this.x + arr[1] * this.y;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Gets vectors cross production.
|
|
329
|
+
* @public
|
|
330
|
+
* @param {math.Vec2} v - Another vector.
|
|
331
|
+
* @returns {math.Vec2}
|
|
332
|
+
*/
|
|
333
|
+
cross(v) {
|
|
334
|
+
return this.x * v.y - this.y * v.x;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Sets vector to zero.
|
|
339
|
+
* @public
|
|
340
|
+
* @returns {math.Vec2}
|
|
341
|
+
*/
|
|
342
|
+
clear() {
|
|
343
|
+
this.x = this.y = 0;
|
|
344
|
+
return this;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Returns normalized vector.
|
|
349
|
+
* @public
|
|
350
|
+
* @returns {math.Vec2}
|
|
351
|
+
*/
|
|
352
|
+
normal() {
|
|
353
|
+
var res = new Vec2();
|
|
354
|
+
res.copy(this);
|
|
355
|
+
|
|
356
|
+
var length = 1.0 / res.length();
|
|
357
|
+
|
|
358
|
+
res.x *= length;
|
|
359
|
+
res.y *= length;
|
|
360
|
+
|
|
361
|
+
return res;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Normalize current vector.
|
|
366
|
+
* @public
|
|
367
|
+
* @returns {math.Vec2}
|
|
368
|
+
*/
|
|
369
|
+
normalize() {
|
|
370
|
+
var length = 1.0 / this.length();
|
|
371
|
+
|
|
372
|
+
this.x *= length;
|
|
373
|
+
this.y *= length;
|
|
374
|
+
|
|
375
|
+
return this;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Converts vector to a number array.
|
|
380
|
+
* @public
|
|
381
|
+
* @returns {Array.<number>} - (exactly 2 entries)
|
|
382
|
+
*/
|
|
383
|
+
toVec() {
|
|
384
|
+
return [this.x, this.y];
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Gets distance to point.
|
|
389
|
+
* @public
|
|
390
|
+
* @param {math.Vec2} p - Distant point.
|
|
391
|
+
* @returns {number}
|
|
392
|
+
*/
|
|
393
|
+
distance(p) {
|
|
394
|
+
var vec = Vec2.sub(this, p);
|
|
395
|
+
return vec.length();
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Sets vector's values.
|
|
400
|
+
* @public
|
|
401
|
+
* @param {number} x - Value X.
|
|
402
|
+
* @param {number} y - Value Y.
|
|
403
|
+
* @returns {math.Vec2}
|
|
404
|
+
*/
|
|
405
|
+
set(x, y) {
|
|
406
|
+
this.x = x;
|
|
407
|
+
this.y = y;
|
|
408
|
+
return this;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Negate current vector.
|
|
413
|
+
* @public
|
|
414
|
+
* @returns {math.Vec2}
|
|
415
|
+
*/
|
|
416
|
+
negate() {
|
|
417
|
+
this.x = -this.x;
|
|
418
|
+
this.y = -this.y;
|
|
419
|
+
return this;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Negate current vector to another instance.
|
|
424
|
+
* @public
|
|
425
|
+
* @returns {math.Vec2}
|
|
426
|
+
*/
|
|
427
|
+
negateTo() {
|
|
428
|
+
return new Vec2(-this.x, -this.y);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Gets projected point coordinates of the current vector on the ray.
|
|
433
|
+
* @public
|
|
434
|
+
* @param {math.Vec2} pos - Ray position.
|
|
435
|
+
* @param {math.Vec2} direction - Ray direction.
|
|
436
|
+
* @returns {math.Vec2}
|
|
437
|
+
*/
|
|
438
|
+
projToRay(pos, direction) {
|
|
439
|
+
var v = Vec2.proj_b_to_a(Vec2.sub(this, pos), direction);
|
|
440
|
+
v.add(pos);
|
|
441
|
+
return v;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Gets angle between two vectors.
|
|
446
|
+
* @public
|
|
447
|
+
* @param {math.Vec2} a - Another vector.
|
|
448
|
+
* @returns {number}
|
|
449
|
+
*/
|
|
450
|
+
angle(a) {
|
|
451
|
+
return Vec2.angle(this, a);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Returns two vectors linear interpolation.
|
|
456
|
+
* @public
|
|
457
|
+
* @param {math.Vec2} v2 - End vector.
|
|
458
|
+
* @param {number} l - Interpolate value.
|
|
459
|
+
* @returns {math.Vec2}
|
|
460
|
+
*/
|
|
461
|
+
lerp(v1, v2, l) {
|
|
462
|
+
var res = Vec2.clone(this);
|
|
463
|
+
if (l <= 0.0) {
|
|
464
|
+
res.copy(v1);
|
|
465
|
+
} else if (l >= 1.0) {
|
|
466
|
+
res.copy(v2);
|
|
467
|
+
} else {
|
|
468
|
+
res = Vec2.add(v1, Vec2.sub(v2, v1).scale(l));
|
|
469
|
+
}
|
|
470
|
+
return res;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
static get LERP_DELTA() {
|
|
474
|
+
return 1e-6;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Spherically interpolates between two vectors.
|
|
479
|
+
* Interpolates between current and v2 vector by amount t. The difference between this and linear interpolation (aka, "lerp") is that
|
|
480
|
+
* the vectors are treated as directions rather than points in space. The direction of the returned vector is interpolated
|
|
481
|
+
* by the angle and its magnitude is interpolated between the magnitudes of from and to.
|
|
482
|
+
* @public
|
|
483
|
+
* @param {math.Vec2} v2 -
|
|
484
|
+
* @param {number} t - The parameter t is clamped to the range [0, 1].
|
|
485
|
+
* @returns {math.Vec2}
|
|
486
|
+
*/
|
|
487
|
+
slerp(v2, t) {
|
|
488
|
+
var res = new Vec2();
|
|
489
|
+
|
|
490
|
+
if (t <= 0.0) {
|
|
491
|
+
res.copy(this);
|
|
492
|
+
return;
|
|
493
|
+
} else if (t >= 1.0) {
|
|
494
|
+
res.copy(v2);
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
var omega, sinom, scale0, scale1;
|
|
499
|
+
var cosom = this.dot(v2);
|
|
500
|
+
|
|
501
|
+
if (1.0 - cosom > Vec2.LERP_DELTA) {
|
|
502
|
+
omega = Math.acos(cosom);
|
|
503
|
+
sinom = Math.sin(omega);
|
|
504
|
+
scale0 = Math.sin((1.0 - t) * omega) / sinom;
|
|
505
|
+
scale1 = Math.sin(t * omega) / sinom;
|
|
506
|
+
} else {
|
|
507
|
+
scale0 = 1.0 - t;
|
|
508
|
+
scale1 = t;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
return Vec2.add(this.scale(scale0), v2.scale(scale1));
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* Vector 2d object creator.
|
|
517
|
+
* @function
|
|
518
|
+
* @param {number} [x] - First cvalue.
|
|
519
|
+
* @param {number} [y] - Second value.
|
|
520
|
+
* @returns {math.Vec2}
|
|
521
|
+
*/
|
|
522
|
+
export function vec2(x, y) {
|
|
523
|
+
return new Vec2(x, y);
|
|
524
|
+
}
|