# p5.Vector.div() doesnt work for 2D vectors

The div method doesn’t work with 2D vectors because the z component of the divisor is set automatically to 0. This should be fixed so the z component is ignored.

1 Like

Hello,

p5.js vector division is defined here:
https://p5js.org/reference/#/p5.Vector/div

And states:
`When dividing a vector by a vector, the x, y, z components of the source vector are treated as the dividend, and the x, y, z components of the argument is treated as the divisor (for example with two vectors a and b: a.x / b.x, a.y / b.y, a.z / b.z).`

You will get a “divide by zero error” for any vector component that is zero in the divisor.

p5.js editor shows:

If you want to do complex number division or a vector division that you define you can write a custom function.

`:)`

1 Like

Don’t use the signature:

``````a.div(b);
``````

``````a.div(b.x, b.y);
``````

This isn’t a bug that can be fixed, because p5.Vector is always a 3D vector – after it has been created, there is no way to know if z is 1 or 0 to indicate it is 2D, or if instead z holds 3D information. For 2D operations, the “default” z should sometimes be 1 (multiplication, division) and sometimes 0 (addition, subtraction), so explicitly adding it isn’t operator-safe. By default, when you create 2D vectors, they are add-subtract safe and multiply-safe, but not division safe. If the default z was 1, they would be multiply-divide safe, but not add-subtract safe.

The better practice is to call vector operations using their explicit 2D signatures, and they will provide the 1 or 0 for you:

``````a.sub(b.x, b.y);
a.div(b.x, b.y);
``````

…OR just put them in yourself, which is not necessary but makes your code explicit:

``````a.sub(b.x, b.y, 0);
a.div(b.x, b.y, 1);
``````
1 Like