Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vecX: add setLength function #25

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
});

});

Loading