Skip to content

Commit 2451bb4

Browse files
authored
Merge pull request #25 from mreinstein/main
vecX: add setLength function
2 parents 218a9db + 61d91a3 commit 2451bb4

File tree

6 files changed

+84
-0
lines changed

6 files changed

+84
-0
lines changed

src/vec2-impl.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,3 +667,16 @@ export function rotate(a: Vec2, b: Vec2, rad: number, dst?: Vec2) {
667667

668668
return dst;
669669
}
670+
671+
/**
672+
* Treat a 2D vector as a direction and set it's length
673+
*
674+
* @param a The vec2 to lengthen
675+
* @param len The length of the resulting vector
676+
* @returns The lengthened vector
677+
*/
678+
export function setLength(a: Vec2, len: number, dst?: Vec2) {
679+
dst = dst || new VecType(2);
680+
normalize(a, dst);
681+
return mulScalar(dst, len, dst);
682+
}

src/vec3-impl.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,3 +883,16 @@ export function rotateZ(a: Vec3, b: Vec3, rad: number, dst?: Vec3) {
883883

884884
return dst;
885885
}
886+
887+
/**
888+
* Treat a 3D vector as a direction and set it's length
889+
*
890+
* @param a The vec3 to lengthen
891+
* @param len The length of the resulting vector
892+
* @returns The lengthened vector
893+
*/
894+
export function setLength(a: Vec3, len: number, dst?: Vec3) {
895+
dst = dst || new VecType(3);
896+
normalize(a, dst);
897+
return mulScalar(dst, len, dst);
898+
}

src/vec4-impl.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,3 +632,17 @@ export function transformMat4(v: Vec4, m: Mat4, dst?: Vec4): Vec4 {
632632

633633
return dst;
634634
}
635+
636+
637+
/**
638+
* Treat a 4D vector as a direction and set it's length
639+
*
640+
* @param a The vec4 to lengthen
641+
* @param len The length of the resulting vector
642+
* @returns The lengthened vector
643+
*/
644+
export function setLength(a: Vec4, len: number, dst?: Vec4) {
645+
dst = dst || new VecType(4);
646+
normalize(a, dst);
647+
return mulScalar(dst, len, dst);
648+
}

test/tests/vec2-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,5 +496,19 @@ describe('vec2', () => {
496496
});
497497
});
498498

499+
describe('setLength', function() {
500+
describe('set the length of a provided direction vector', function() {
501+
let vecA, result;
502+
beforeEach(function () {
503+
vecA = [1, 1];
504+
result = vec2.setLength(vecA, 14.6);
505+
});
506+
it("should return the lengthend vector", function () {
507+
assertEqualApproximately(result, [10.323759005323593, 10.323759005323593]);
508+
assertEqualApproximately(vec2.length(result), 14.6);
509+
});
510+
});
511+
});
512+
499513
});
500514

test/tests/vec3-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,5 +551,20 @@ describe('vec3', () => {
551551
});
552552
});
553553
});
554+
555+
describe('setLength', function() {
556+
describe('set the length of a provided direction vector', function() {
557+
let vecA, result;
558+
beforeEach(function () {
559+
vecA = [1, 1, 1];
560+
result = vec3.setLength(vecA, 14.6);
561+
});
562+
it("should return the lengthened vector", function () {
563+
assertEqualApproximately(result, [8.429313930168536, 8.429313930168536, 8.429313930168536]);
564+
assertEqualApproximately(vec3.length(result), 14.6);
565+
});
566+
});
567+
});
568+
554569
});
555570

test/tests/vec4-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
assertStrictEqual,
88
assertStrictNotEqual,
99
assertIsArray,
10+
assertEqualApproximately,
1011
assertTruthy,
1112
assertFalsy,
1213
} from '../assert.js';
@@ -404,5 +405,19 @@ describe('vec4', () => {
404405
check(Float32Array);
405406
check(Float64Array);
406407

408+
describe('setLength', function() {
409+
describe('set the length of a provided direction vector', function() {
410+
let vecA, result;
411+
beforeEach(function () {
412+
vecA = [1, 1, 1, 1];
413+
result = vec4.setLength(vecA, 14.6);
414+
});
415+
it("should return the lengthened vector", function () {
416+
assertEqualApproximately(result, [7.3, 7.3, 7.3, 7.3]);
417+
assertEqualApproximately(vec4.length(result), 14.6);
418+
});
419+
});
420+
});
421+
407422
});
408423

0 commit comments

Comments
 (0)