p5.Vector feature request: unit + magnitude method

When creating physics models, I often find myself converting a distance vector (between two points) into a unit vector AND the magnitude. This bugs me simply because I know the computation of the unit vector required the magnitude as well.

One possible design would be give mag() an optional p5.Vector argument, which if provided, fills in the vector with the unit vector:

var v = createVector(20, 10);
var u = createVector();
var m = v.mag(u);

// m has value 22.360679774997898
// u has components [0.8944271909999159, 0.4472135954999579]

Would this be useful to the community at large?

Answers

Sign In or Register to comment.