Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 | 69x 69x 69x 69x 4x 1x 1x 1x 1x 1x 4x 2x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 16x 16x 1x 1x 17x 17x 17x 17x 34x | import * as EASE from '../ease/ease';
import * as mathf from './mathf';
import type {MatrixIV} from './matrixIV';
/**
*
* A basic vector class that can be used for 2d or 3d. When using for 2d,
* the z defaults to 0.
*
* ```ts
*
* let v = new Vector(0,0,0);
* let v2 = new Vector(2,1,0);
*
* v.add(v2);
* let v3 = v.clone();
* v3.scale(5);
*
* v3.dot(v2);
*
* ```
*
*
* TODO (uxder): Add matrix transformations.
*
* TODO (uxder): Add more tests.
*
* Some good resource on Vectors if you are unfamiliar with them.
* @see https://www.mathsisfun.com/algebra/vectors.html
* @see https://bit.ly/2wGFKdv
*
* Inspirted by:
* @see https://github.com/mrdoob/three.js/blob/master/src/math/Vector3.js
* @see https://github.com/photonstorm/phaser/blob/v3.17.0/src/math/Vector3.js
* @see https://evanw.github.io/lightgl.js/docs/vector.html
*
*/
export class Vector {
/**
* The x component of this vector.
*/
public x: number;
/**
* The y component of this vector.
*/
public y: number;
/**
* The z component of this vector.
*/
public z: number;
/**
* The w component of this vector.
*/
public w: number;
constructor(x = 0, y = 0, z = 0, w = 0) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
this.w = w || 0;
}
/**
* Makes a clone of this vector.
*
* ```ts
* var v = new Vector(0,1,0);
* var clonedVector = v.clone();
* ```
* @tested
*/
clone(): Vector {
return new Vector(this.x, this.y, this.z);
}
/**
* Makes a clone of this vector.
*
* ```ts
* var v = new Vector(0,1,0);
* var clonedVector = Vecrtor3.clone(v);
* ```
* @tested
*/
static clone(v: Vector) {
return new Vector(v.x, v.y, v.z);
}
/**
* Sets x,y,z.
* @tested
*/
set(x: number, y: number, z = 0): Vector {
this.x = x;
this.y = y;
this.z = z;
return this;
}
/**
* Floors out the internal values to avoid subpixel rendering.
*/
floor() {
this.x = ~~this.x;
this.y = ~~this.y;
this.z = ~~this.z;
return this;
}
/**
* Converts internal values to int
*/
int() {
this.x = this.x >> 0;
this.y = this.y >> 0;
this.z = this.z >> 0;
return this;
}
/**
* Tests the equality of this vector against another.
*
* ```ts
* var v = new Vector(0,1,0);
* var v2 = new Vector(0,1,0);
* v.equals(v2); // true
* ```
* @tested
*/
equals(vector: Vector): boolean {
return this.x === vector.x && this.y === vector.y && this.z === vector.z;
}
/**
* A static method testing the equality of two vectors.
*
* ```ts
* var v = new Vectorr3(0,1,0);
* var v2 = new Vector(0,1,0);
* Vector.equals(v2); // true
* ```
*
* @param v1
* @param v2
* @tested
*/
static equals(v1: Vector, v2: Vector): boolean {
return v1.x === v2.x && v1.y === v2.y && v1.z === v2.z;
}
/**
* Converts the current vector to an array.
*
* ```ts
* var v = new Vector(0,1,0);
* v.toArray(); // [0,1,0]
* ```
*/
toArray(): Array<number> {
return [this.x, this.y, this.z];
}
/**
* Converts the current vector to an array.
*
* ```ts
* var v = new Vector(0,1,0);
* var a = Vector.toArray(v);// [0,1,0]
* ```
*/
static toArray(v: Vector): Array<number> {
return [v.x, v.y, v.z];
}
/**
* Creates a new Vector from a set of arrays.
*
* ```ts
* var v = Vector.fromArray([0,2,2]);
* ```
*/
static fromArray(values: Array<number>): Vector {
return new Vector(values[0], values[1], values[2]);
}
/**
* Creates a new Vector from static method.
*
* ```ts
* var v = Vector.create(0,2,2);
* ```
* @param x
* @param y
* @param z
* @tested
*/
static create(x: number, y: number, z = 0): Vector {
return new Vector(x, y, z);
}
/**
* Creates a new 2d Vector with a specific angle and length.
* This is useful to create a directional vector.
*
* Example:
* ```ts
*
* let origin = new Vector(10, 20);
* // Create a ray that goes 90 degrees down in 100 length.
* let ray = Vector.fromAngle(mathf.degreeToRadian(90), 100);
*
* // Combine the origin and ray to get the endpoint (the tip of the ray).
* let endPoint = Vector.add(origin, ray);
*
* ```
*
*
* @param angle Angle in radians
* @param length The length, magnitude of the vector. Defaults to a
* semi-infinite length.
*/
static fromAngle(angle: number, length = 100000) {
return new Vector(length * Math.cos(angle), length * Math.sin(angle), 0);
}
/**
* Gets the angle between two vectors.
*
*
* ```ts
*
* let a = new Vector(100,200);
* let b = new Vector(150,240);
*
* let angle = Vector.angle2d(a, b);
*
* ```
*
* @param a The first vector
* @param b The second vector
* @return The angle of the two vectors in radians.
*/
static angle2d(a: Vector, b: Vector): number {
const dx = a.x - b.x,
dy = a.y - b.y;
return Math.atan2(dy, dx);
}
/**
* Given a targetVector, this will find the rotation angles of each
* axis which you can then later use to on rotateX, rotateY, rotateZ
* values on a matrix or rotational vector.
*
*
* Basic Example:
* ```ts
*
* let origin = Vector.ZERO;
* let target = new Vector(10,15,20);
* let angles = Vector.getXyzRotationTo(origin, target);
*
* angles[0] // 1.95.. -- radian x rotation value
* angles[1] // 2.16.. -- radian y rotation value
* angles[2] // 2.40.. -- radian z rotation value
*
* ```
*
* See for a reference but the below is not using this:
* https://stackoverflow.com/questions/48532207/get-xyz-rotation-from-pvector
* @param targetVector
* @return Array<number> An array with three numbers,
* angleX, angleY and angleZ in radians.
*/
static getXyzRotationTo(originVector: Vector, targetVector: Vector) {
// Use the difference between current vector and target as the basis.
const delta = originVector.clone().subtract(targetVector);
const angleX = Math.atan(delta.x);
const angleY = Math.atan(delta.y);
const angleZ = Math.atan(delta.z);
return [angleX, angleY, angleZ];
}
/**
* Find the Eular rotation from one vector to another. This will you
* the angle delta between the two.
*
*
* ```ts
* let eularVector = Vector.getEularRotation(origin, target);
* eularVector.x // degrees in x rotation
* eularVector.y // degrees in y rotation
* eularVector.z // degrees in z rotation
* ```
*
* @param originVector
* @param targetVector
*/
static getEularRotationTo(
originVector: Vector,
targetVector: Vector
): Vector {
// Use the difference between current vector and target as the basis.
const delta = originVector
.clone()
.normalize()
.subtract(targetVector.clone().normalize());
const angleX = -Math.atan(delta.x);
const angleY = Math.atan(delta.y);
const angleZ = -Math.atan(delta.z);
return new Vector(
mathf.radianToDegree(angleX),
mathf.radianToDegree(angleY),
mathf.radianToDegree(angleZ)
);
}
/**
* Adds this vector to another.
*/
add(v: Vector): Vector {
this.x += v.x;
this.y += v.y;
this.z += v.z || 0;
return this;
}
static add(v2: Vector, v1: Vector) {
const x = v2.x + v1.x;
const y = v2.y + v1.y;
const z = v2.z + v1.z;
return new Vector(x, y, z);
}
/**
* Subtract this vector from another .
*/
subtract(v: Vector): Vector {
this.x -= v.x;
this.y -= v.y;
this.z -= v.z || 0;
return this;
}
/**
* Subtract this vector from another and create a new vector.
*/
static subtract(v2: Vector, v1: Vector) {
const x = v2.x - v1.x;
const y = v2.y - v1.y;
const z = v2.z - v1.z;
return new Vector(x, y, z);
}
/**
* Multiply this vector with another.
*/
multiply(v: Vector): Vector {
this.x *= v.x;
this.y *= v.y;
this.z *= v.z || 1;
return this;
}
/**
* Divide this vector with another.
*/
divide(v: Vector): Vector {
this.x /= v.x;
this.y /= v.y;
this.z /= v.z || 1;
return this;
}
/**
* Scales this vector by the given scalar value.
*/
scale(scalar: number): Vector {
if (Number.isFinite(scalar)) {
this.x *= scalar;
this.y *= scalar;
this.z *= scalar;
return this;
} else {
return Vector.ZERO;
}
}
/**
* Negate this vector as a 3d vector.
*/
negate(): Vector {
this.x = -this.x;
this.y = -this.y;
this.z = mathf.absZero(-this.z);
return this;
}
static negate(v: Vector): Vector {
const x = -v.x;
const y = -v.y;
const z = mathf.absZero(-v.z);
return new Vector(x, y, z);
}
/**
* Calculates the distance between this vector and another.
* @return The distance from this vector to another
*/
distance(v: Vector): number {
const dx = v.x - this.x;
const dy = v.y - this.y;
const dz = v.z - this.z || 0;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
/**
* Calculates the length / magnitude of this vector.
*/
length(): number {
const x = this.x;
const y = this.y;
const z = this.z;
return Math.sqrt(x * x + y * y + z * z);
}
/**
* Calculates the length squared
*/
lengthSquared(): number {
return this.x * this.x + this.y * this.y + this.z * this.z;
}
/**
* Calculates the magnitude of this vector.
* Alias of [[Vector.length]]
*/
magnitude(): number {
return this.length();
}
/**
* Normalizes this vector.
*/
normalize(): Vector {
const x = this.x;
const y = this.y;
const z = this.z;
let len = x * x + y * y + z * z;
if (len > 0) {
len = 1 / Math.sqrt(len);
this.x = x * len;
this.y = y * len;
this.z = z * len;
}
return this;
}
/**
* Calculates the dot product of this Vector and the given vector.
* @return The dot product of this vector and provided vector.
*/
dot(v: Vector): number {
return this.x * v.x + this.y * v.y + this.z * v.z;
}
cross(v: Vector): Vector {
const ax = this.x;
const ay = this.y;
const az = this.z;
const bx = v.x;
const by = v.y;
const bz = v.z;
this.x = ay * bz - az * by;
this.y = az * bx - ax * bz;
this.z = ax * by - ay * bx;
return this;
}
/**
* Transform this vector given the provided matrix4.
*/
transformWithMatrixIV(matrix: MatrixIV): Vector {
const x = this.x;
const y = this.y;
const z = this.z;
const mat = matrix.value;
const tx = x * mat[0] + y * mat[4] + z * mat[8] + mat[12];
const ty = x * mat[1] + y * mat[5] + z * mat[9] + mat[13];
const tz = x * mat[2] + y * mat[6] + z * mat[10] + mat[14];
let tw = x * mat[3] + y * mat[7] + z * mat[11] + mat[15];
tw = tw || 1.0;
this.x = tx / tw;
this.y = ty / tw;
this.z = tz / tw;
return new Vector(this.x, this.y, this.z);
}
/**
* Transform the current vector with the provided matrix4 to a
* 2d. This effectively takes this 3d vector and makes it into
* the cooresponding 2d coordinates using the transformMatrix.
*
* This operation is the same as transformWithMatrixIV except the
* z is dropped.
* @param matrix
*/
transformWithMatrixIVTo2d(matrix: MatrixIV): Vector {
return this.transformWithMatrixIV(matrix).set(this.x, this.y, 0);
}
// /**
// * Adds a 3d rotation. This is NOT a rotation based on the center of the
// * this vector but based from VECTOR.ZERO. (so adding a rotation will move
// * the position not the direction it's looking).
// * https://ikeryou.hatenablog.com/entry/2018/01/07/104729
// * @param v
// * @param angle Angle in radians.
// * TODO (uxder): Maybe move to vector-dom instead?
// */
// rotateX(angle: number) {
// const cos = Math.cos(angle);
// const sin = Math.sin(angle);
// this.y = this.y * cos - this.z * sin;
// this.z = this.z * cos + this.y * sin;
// return this;
// }
// /**
// * Adds a 3d rotation. This is NOT a rotation based on the center of the
// * this vector but based from VECTOR.ZERO. (so adding a rotation will move
// * the position not the direction it's looking).
// * https://ikeryou.hatenablog.com/entry/2018/01/07/104729
// * @param v
// * @param angle Angle in radians.
// * TODO (uxder): Maybe move to vector-dom instead?
// */
// rotateY(angle: number) {
// const cos = Math.cos(angle);
// const sin = Math.sin(angle);
// this.x = this.x * cos - this.z * sin;
// this.z = this.z * cos + this.x * sin;
// return this;
// }
// /**
// * Adds a 3d rotation. This is NOT a rotation based on the center of the
// * this vector but based from VECTOR.ZERO. (so adding a rotation will move
// * the position not the direction it's looking).
// * https://ikeryou.hatenablog.com/entry/2018/01/07/104729
// * @param v
// * @param angle Angle in radians.
// * TODO (uxder): Maybe move to vector-dom instead?
// */
// rotateZ(angle: number) {
// const cos = Math.cos(angle);
// const sin = Math.sin(angle);
// this.x = this.x * cos - this.y * sin;
// this.y = this.y * cos + this.x * sin;
// return this;
// }
/**
* Linear interpolates this vector TOWARDS the provided vector.
* @param v
* @param progress A number between 0-1.
*/
lerp(v: Vector, progress = 0): Vector {
this.x = mathf.lerp(this.x, v.x, progress);
this.y = mathf.lerp(this.y, v.y, progress);
this.z = mathf.lerp(this.z, v.z, progress);
return this;
}
/**
* Similar to lerp but uses exponential decay. Useful to
* smooth out animations with damping.
* @param v
* @param progress A number between 0-1.
*/
damp(v: Vector, progress = 0, damp: number): Vector {
this.x = mathf.damp(this.x, v.x, progress, damp);
this.y = mathf.damp(this.y, v.y, progress, damp);
this.z = mathf.damp(this.z, v.z, progress, damp);
return this;
}
/**
* Ease interpolates this vector TOWARDS the provided vector.
* @param v
* @param progress A number between 0-1.
* @param easeFunction An easing function. See [[mathf.ease]].
*/
ease(v: Vector, progress = 0, easeFunction = EASE.linear): Vector {
this.x = mathf.ease(this.x, v.x, progress, easeFunction);
this.y = mathf.ease(this.y, v.y, progress, easeFunction);
this.z = mathf.ease(this.z, v.z, progress, easeFunction);
return this;
}
/**
* Ease interpolates and eases 1 vector towards another.
* @param v1 startVector
* @param v2 endVector
* @param progress
* @param easeFunction
*/
static ease(
v1: Vector,
v2: Vector,
progress = 0,
easeFunction = EASE.linear
): Vector {
const x = mathf.ease(v1.x, v2.x, progress, easeFunction);
const y = mathf.ease(v1.y, v2.y, progress, easeFunction);
const z = mathf.ease(v1.z, v2.z, progress, easeFunction);
return new Vector(x, y, z);
}
/**
* Given vector a and vector b, takes the Math.min values of x,y,z and
* returns a new Vector.
* @param a Vector A
* @param b Vector B
*/
static min(a: Vector, b: Vector): Vector {
return new Vector(
Math.min(a.x, b.x),
Math.min(a.y, b.y),
Math.min(a.z, b.z)
);
}
/**
* Given vector a and vector b, takes the Math.max values of x,y,z and
* returns a new Vector.
* @param a Vector A
* @param b Vector B
*/
static max(a: Vector, b: Vector): Vector {
return new Vector(
Math.max(a.x, b.x),
Math.max(a.y, b.y),
Math.max(a.z, b.z)
);
}
/**
* Given a Vector storing Euler degrees, converts it to radians.
*
* ```ts
* let eulerVector = new Vector(90, 180, 0);
* let radianVector = Vector.eulerVectorToRadians(eulerVector);
*
* ```
*/
static degreeToRadians(v: Vector) {
return new Vector(
mathf.degreeToRadian(v.x),
mathf.degreeToRadian(v.y),
mathf.degreeToRadian(v.z)
);
}
degreeToRadians() {
return Vector.degreeToRadians(this);
}
/**
* Clamps a given vector between a min and max vector.
* @param min The min vector
* @param max The max vector
* @param vector The vector to clamp
*/
static clamp(min: Vector, max: Vector, vector: Vector): Vector {
return new Vector(
mathf.clamp(min.x, max.x, vector.x),
mathf.clamp(min.y, max.y, vector.y),
mathf.clamp(min.z, max.z, vector.z)
);
}
/**
* Creates a Eular vector from a rotationalMatrix. This conversion is better
* avoided for different reasons. The pitch (y) will be limited to +-90.
*
* There are also cases where accuracy suffers so use with caution.
*
* Yaw, Roll will be +-180.
* Pitch will be +- 90.
*
* ```ts
* // Example converts a quaternion back over to euler.
* let quat = new Quaternion(0.2, 0.2, 0.2, 0.1);
* let matrix = MatrixIV.fromQuaternion(quat.clone());
* let eularVector = Vector.fromRotationMatrixIV(matrix);
* ```
*
*/
static fromRotationMatrixIV(m: MatrixIV) {
const result = Vector.ZERO;
const te = m.value;
const m11 = te[0],
m12 = te[4],
m13 = te[8];
// eslint-disable-next-line
const m21 = te[1],
m22 = te[5],
m23 = te[9];
// eslint-disable-next-line
const m31 = te[2],
m32 = te[6],
m33 = te[10];
// XYZ ordering
// https://github.com/mrdoob/three.js/blob/master/src/math/Euler.js#L146
result.y = Math.asin(mathf.clamp(-1, 1, m13));
if (Math.abs(m13) < 0.9999999) {
result.x = Math.atan2(-m23, m33);
result.z = Math.atan2(-m12, m11);
} else {
result.x = Math.atan2(m32, m22);
result.z = 0;
}
result.x = mathf.radianToDegree(result.x);
// result.y = mathf.wrap(mathf.radianToDegree(result.y), -89.9999, 89.9999);
result.y = mathf.radianToDegree(result.y);
result.z = mathf.radianToDegree(result.z);
return result;
}
/**
* A static zero vector.
*
* ```ts
* let positon = Vector.ZERO;
* ```
*/
static get ZERO(): Vector {
return new Vector(0, 0, 0);
}
/**
* A static one vector.
*
* ```ts
* let positon = Vector.ONE;
* ```
*/
static get ONE(): Vector {
return new Vector(1, 1, 1);
}
/**
* A static right vector for reference .
*
* ```ts
* let positon = Vector.ONE;
* position.add(Vector.RIGHT.scale(4))
* ```
*/
static get RIGHT(): Vector {
return new Vector(1, 0, 0);
}
/**
* A static left vector for reference .
*/
static get LEFT(): Vector {
return new Vector(-1, 0, 0);
}
/**
* A static up vector for reference .
*/
static get UP(): Vector {
return new Vector(0, -1, 0);
}
/**
* A static down vector for reference .
*/
static get DOWN(): Vector {
return new Vector(0, 1, 0);
}
/**
* A static foward vector for reference .
*/
static get FORWARD(): Vector {
return new Vector(0, 0, 1);
}
/**
* A static foward vector for reference .
*/
static get BACK(): Vector {
return new Vector(0, 0, -1);
}
}
|