Skip to content

Commit

Permalink
Merge pull request #25 from mreinstein/main
Browse files Browse the repository at this point in the history
vecX: add setLength function
  • Loading branch information
greggman authored Mar 26, 2024
2 parents 218a9db + 61d91a3 commit 2451bb4
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/vec2-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,3 +667,16 @@ export function rotate(a: Vec2, b: Vec2, rad: number, dst?: Vec2) {

return dst;
}

/**
* Treat a 2D vector as a direction and set it's length
*
* @param a The vec2 to lengthen
* @param len The length of the resulting vector
* @returns The lengthened vector
*/
export function setLength(a: Vec2, len: number, dst?: Vec2) {
dst = dst || new VecType(2);
normalize(a, dst);
return mulScalar(dst, len, dst);
}
13 changes: 13 additions & 0 deletions src/vec3-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -883,3 +883,16 @@ export function rotateZ(a: Vec3, b: Vec3, rad: number, dst?: Vec3) {

return dst;
}

/**
* Treat a 3D vector as a direction and set it's length
*
* @param a The vec3 to lengthen
* @param len The length of the resulting vector
* @returns The lengthened vector
*/
export function setLength(a: Vec3, len: number, dst?: Vec3) {
dst = dst || new VecType(3);
normalize(a, dst);
return mulScalar(dst, len, dst);
}
14 changes: 14 additions & 0 deletions src/vec4-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,17 @@ export function transformMat4(v: Vec4, m: Mat4, dst?: Vec4): Vec4 {

return dst;
}


/**
* Treat a 4D vector as a direction and set it's length
*
* @param a The vec4 to lengthen
* @param len The length of the resulting vector
* @returns The lengthened vector
*/
export function setLength(a: Vec4, len: number, dst?: Vec4) {
dst = dst || new VecType(4);
normalize(a, dst);
return mulScalar(dst, len, dst);
}
14 changes: 14 additions & 0 deletions test/tests/vec2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,5 +496,19 @@ describe('vec2', () => {
});
});

describe('setLength', function() {
describe('set the length of a provided direction vector', function() {
let vecA, result;
beforeEach(function () {
vecA = [1, 1];
result = vec2.setLength(vecA, 14.6);
});
it("should return the lengthend vector", function () {
assertEqualApproximately(result, [10.323759005323593, 10.323759005323593]);
assertEqualApproximately(vec2.length(result), 14.6);
});
});
});

});

15 changes: 15 additions & 0 deletions test/tests/vec3-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,5 +551,20 @@ describe('vec3', () => {
});
});
});

describe('setLength', function() {
describe('set the length of a provided direction vector', function() {
let vecA, result;
beforeEach(function () {
vecA = [1, 1, 1];
result = vec3.setLength(vecA, 14.6);
});
it("should return the lengthened vector", function () {
assertEqualApproximately(result, [8.429313930168536, 8.429313930168536, 8.429313930168536]);
assertEqualApproximately(vec3.length(result), 14.6);
});
});
});

});

15 changes: 15 additions & 0 deletions test/tests/vec4-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
assertStrictEqual,
assertStrictNotEqual,
assertIsArray,
assertEqualApproximately,
assertTruthy,
assertFalsy,
} from '../assert.js';
Expand Down Expand Up @@ -404,5 +405,19 @@ describe('vec4', () => {
check(Float32Array);
check(Float64Array);

describe('setLength', function() {
describe('set the length of a provided direction vector', function() {
let vecA, result;
beforeEach(function () {
vecA = [1, 1, 1, 1];
result = vec4.setLength(vecA, 14.6);
});
it("should return the lengthened vector", function () {
assertEqualApproximately(result, [7.3, 7.3, 7.3, 7.3]);
assertEqualApproximately(vec4.length(result), 14.6);
});
});
});

});

0 comments on commit 2451bb4

Please sign in to comment.