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

add vec2.setLength. fixes #393 #394

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions spec/gl-matrix/vec2-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,16 @@ describe("vec2", function() {
it("should return the squared length", function() { expect(result).toEqual(5); });
});

describe("setLength", function() {
const vecA = [0, 0];
const vecB = [0.5, 0];
const result = vec2.setLength(vecA, vecB, 10);

it("should return the updated vector", function() { expect(result).toBeEqualish([10, 0]); });
it("should modify vecA", function() { expect(vecA).toBeEqualish([10, 0]); });
it("should not modify vecB", function() { expect(vecB).toBeEqualish([0.5, 0]); });
});

describe("negate", function() {
describe("with a separate output vector", function() {
beforeEach(function() { result = vec2.negate(out, vecA); });
Expand Down
14 changes: 14 additions & 0 deletions src/vec2.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,20 @@ export function squaredLength(a) {
return x * x + y * y;
}

/**
* Set the length of a 2D vector
* @param {vec2} out The receiving vec2
* @param {vec2} a the vector to lengthen
* @param {Number} length of the resulting vector
* @returns {vec3} out
*/
export function setLength(out, a, length) {
const angle = Math.atan2(a[1], a[0]);
out[0] = Math.cos(angle) * length;
out[1] = Math.sin(angle) * length;
return out;
}

/**
* Negates the components of a vec2
*
Expand Down