Skip to content

Commit ec1034e

Browse files
committed
fixes
1 parent 62ff014 commit ec1034e

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/math/math.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ function math(p5, fn) {
2323
* This allows for flexibility in representing vectors in higher-dimensional
2424
* spaces.
2525
*
26+
* - Calling `createVector()` **with no arguments** is **deprecated** and will emit
27+
* a friendly warning. Use `createVector(0)`, `createVector(0, 0)`, or
28+
* `createVector(0, 0, 0)` instead.
29+
* - To prevent `NaN` appearing in subsequent operations, missing components are
30+
* padded with zeros: `createVector(x)` -> `[x, 0, 0]`, `createVector(x, y)` -> `[x, y, 0]`.
31+
* This is for backwards compatibility only; explicitly pass all intended components.
32+
*
2633
* <a href="#/p5.Vector">p5.Vector</a> objects are often used to program
2734
* motion because they simplify the math. For example, a moving ball has a
2835
* position and a velocity. Position describes where the ball is in space. The
@@ -96,14 +103,26 @@ function math(p5, fn) {
96103
* </div>
97104
*/
98105
fn.createVector = function (x, y, z) {
106+
107+
if (arguments.length === 0 && typeof p5 !== 'undefined' && p5._friendlyError) {
108+
p5._friendlyError(
109+
'Calling createVector() with no arguments is deprecated and will be removed in a future release. Pass zeros for the desired dimensionality.'
110+
);
111+
}
112+
113+
const safeArgs =
114+
arguments.length === 1 ? [x, 0, 0]
115+
: arguments.length === 2 ? [x, y, 0]
116+
: [...arguments];
117+
99118
if (this instanceof p5) {
100119
return new p5.Vector(
101120
this._fromRadians.bind(this),
102121
this._toRadians.bind(this),
103-
...arguments
122+
...safeArgs
104123
);
105124
} else {
106-
return new p5.Vector(x, y, z);
125+
return new p5.Vector(...safeArgs);
107126
}
108127
};
109128

test/unit/math/p5.Matrix.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,30 @@ suite('p5.Matrix', function () {
296296
});
297297
});
298298

299+
suite('createVector NaN (BC patch)', function () {
300+
test('div with 3D divisor: no NaN', function () {
301+
const v = myp5.createVector(8, 9);
302+
const w = myp5.createVector(1, 2, 3);
303+
v.div(w);
304+
const arr = v.array();
305+
expect(arr.some(Number.isNaN)).toBe(false);
306+
expect(arr).toEqual([8, 4.5, 0]);
307+
});
308+
309+
test('div by vector missing z: no NaN (unchanged/div-by-zero safe)', function () {
310+
const v1 = myp5.createVector(8, 9);
311+
const w1 = myp5.createVector(1, 2, 3);
312+
w1.div(v1);
313+
expect(w1.array().some(Number.isNaN)).toBe(false);
314+
});
315+
316+
test('0-arg vector ops: no NaN', function () {
317+
const z = myp5.createVector();
318+
z.add(1);
319+
expect(z.array().some(Number.isNaN)).toBe(false);
320+
});
321+
});
322+
299323
suite('p5.Matrix3x3', function () {
300324
test('apply copy() to 3x3Matrix', function () {
301325
const m = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]);

0 commit comments

Comments
 (0)