Skip to content

Commit

Permalink
Merge pull request #4996 from weslord/static-normalize
Browse files Browse the repository at this point in the history
Add static version of p5.Vector.normalize()
  • Loading branch information
limzykenneth authored Jan 30, 2021
2 parents f2f8bb1 + 4143134 commit 5db68fb
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 14 deletions.
45 changes: 44 additions & 1 deletion src/math/p5.Vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,20 @@ p5.Vector.prototype.dist = function dist(v) {
* // [0.4454354, 0.8908708, 0.089087084]
* </code>
* </div>
*
* <div class="norender">
* <code>
* // Static method
* let v_initial = createVector(10, 20, 2);
* // v_initial has components [10.0, 20.0, 2.0]
* let v_normalized = p5.Vector.normalize(v_initial);
* print(v_normalized);
* // returns a new vector with components set to
* // [0.4454354, 0.8908708, 0.089087084]
* // v_initial remains unchanged
* </code>
* </div>
*
* <div>
* <code>
* function draw() {
Expand Down Expand Up @@ -2303,11 +2317,15 @@ p5.Vector.lerp = function lerp(v1, v2, amt, target) {
return target;
};

/**
* Calculates the magnitude (length) of the vector and returns the result as
* a float (this is simply the equation sqrt(x\*x + y\*y + z\*z).)
*/
/**
* @method mag
* @static
* @param {p5.Vector} vecT the vector to return the magnitude of
* @return {Number} the magnitude of vecT
* @static
*/
p5.Vector.mag = function mag(vecT) {
const x = vecT.x,
Expand All @@ -2317,4 +2335,29 @@ p5.Vector.mag = function mag(vecT) {
return Math.sqrt(magSq);
};

/**
* Normalize the vector to length 1 (make it a unit vector).
*/
/**
* @method normalize
* @static
* @param {p5.Vector} v the vector to normalize
* @param {p5.Vector} [target] the vector to receive the result (Optional)
* @return {p5.Vector} v normalized to a length of 1
*/
p5.Vector.normalize = function normalize(v, target) {
if (arguments.length < 2) {
target = v.copy();
} else {
if (!(target instanceof p5.Vector)) {
p5._friendlyError(
'The target parameter should be of type p5.Vector',
'p5.Vector.normalize'
);
}
target.set(v);
}
return target.normalize();
};

export default p5.Vector;
54 changes: 41 additions & 13 deletions test/unit/math/p5.Vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -868,18 +868,16 @@ suite('p5.Vector', function() {
});

suite('normalize', function() {
setup(function() {
v.x = 1;
v.y = 1;
v.z = 1;
});
suite('v.normalize()', function() {
setup(function() {
v = myp5.createVector(1, 1, 1);
});

test('should return the same object', function() {
expect(v.normalize()).to.eql(v);
});
test('should return the same object', function() {
expect(v.normalize()).to.eql(v);
});

suite('with unit vector', function() {
test('should not change the vector', function() {
test('unit vector should not change values', function() {
v.x = 1;
v.y = 0;
v.z = 0;
Expand All @@ -888,10 +886,8 @@ suite('p5.Vector', function() {
expect(v.y).to.eql(0);
expect(v.z).to.eql(0);
});
});

suite('with 2,2,1', function() {
test('should normalize to 0.66,0.66,0.33', function() {
test('2,2,1 should normalize to ~0.66,0.66,0.33', function() {
v.x = 2;
v.y = 2;
v.z = 1;
Expand All @@ -901,6 +897,38 @@ suite('p5.Vector', function() {
expect(v.z).to.be.closeTo(0.3333, 0.01);
});
});

suite('p5.Vector.normalize(v)', function() {
var res;
setup(function() {
v = myp5.createVector(1, 0, 0);
res = p5.Vector.normalize(v);
});

test('should not be undefined', function() {
expect(res).to.not.eql(undefined);
});

test('should not return same object v', function() {
expect(res).to.not.equal(v);
});

test('unit vector 1,0,0 should normalize to 1,0,0', function() {
expect(res.x).to.eql(1);
expect(res.y).to.eql(0);
expect(res.z).to.eql(0);
});

test('2,2,1 should normalize to ~0.66,0.66,0.33', function() {
v.x = 2;
v.y = 2;
v.z = 1;
res = p5.Vector.normalize(v);
expect(res.x).to.be.closeTo(0.6666, 0.01);
expect(res.y).to.be.closeTo(0.6666, 0.01);
expect(res.z).to.be.closeTo(0.3333, 0.01);
});
});
});

suite('limit', function() {
Expand Down

0 comments on commit 5db68fb

Please sign in to comment.