Repository navigation
Description
Problem
The documentation defines the concept of heading for 2D vectors specifically.
- In 1.x, there are no true 2D vectors, in that
zis always defined. As a result,heading()produces output for fully 3D vectors, even whenzis nonzero. It also produces output for “1D” vectors, sincecreateVector(1)could be used as a shortcut forcreateVector(1, 0). - In 2.x, we have an opportunity to resolve these unintended quirks.
Solution
Supporting only 2D input and adding errors for other dimensions would allow the simplest and clearest feature specification, and it’s how users expect this feature to be used. This could be considered a bug fix: users who accidentally provide a 3D vector can now be notified of this mistake, rather than silently allowing it. In practice, it appears that this change would cause zero (or near zero) disruption.
Disruption estimate: 0%
I used the Google search phrase site:https://editor.p5js.org/ "createVector" "heading" to find 50 sketches using the heading() method. Zero of them would break if we were to implement the proposed solution.
(I did exclude a few sketches, meaning I didn’t count them toward the numerator or denominator. One case used 4D vectors in 1.x, which is not supported, and the sketch appeared to be broken already. I also excluded three sketches that used createVector() with zero arguments somewhere in their heading calculations, since it’s not 100% clear what effect this will have in 2.x. But the goal is to prevent errors from arising in this case.)
Blocking issues (foundational issues for all of p5.Vector)
If you're interested in helping to move the current issue along, you can add a comment on the current issue to indicate whether you support the proposed solution. After that, you could add a comment on the following issues to indicate whether you support the proposals they contain, along with a brief rationale.
-
#8153: Consensus would clarify the meaning of 2D. In 1.x, this meant
$z=0$ , but according to the proposal, in 2.x it'd mean the vector has exactly two defined elements (this would resolve an inconsistency with the current implementations of.zandtoString()). -
#8155: Consensus on a user-facing API for checking dimension size would enable a more stable implementation of
heading(). Adding a comment on the current leading proposal (.shape), either in support of the proposal or against it, would be helpful. The exact format of this API depends on the outcome of an ongoing review of getter/setter patterns in p5, so any comments can focus on the "shape" concept.
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsNo status
Activity
p5.Vectorto n dimensions #8158reshma045 commentedon Oct 28, 2025
Hi @GregStanton, I would like to work on this. Can you please assign this issue to me?
justAnotherAnotherUser commentedon Oct 28, 2025
Hi @GregStanton I would like to start work on this as mentioned in the parent issue
GregStanton commentedon Oct 28, 2025
Hi @reshma045 and @justAnotherAnotherUser! Thank you both for volunteering. Please see the comment I just added to the
setHeading()issue about how we can organize the work on this.adity1raut commentedon Nov 12, 2025
/assign
8 remaining items
ksen0 commentedon Jun 17, 2026
Thans for bumping this @sidwellr . @limzykenneth and I have checked this and this one is open to a PR that:
@avinxshKD yours if you want it!
EDIT: sorry I misread the queue. But this is a very small one, apologies, I hope it's not too frustrating.
adityachoudhury29 commentedon Oct 23, 2026
@ksen0 Is this still open? Following your June comment, the scope I have in mind is a nonblocking FES warning when the vector is not 2D, with heading() still returning atan2(y, x) as it does today.
I checked on main at 656b415.
createVector(1).heading()returns 0 andcreateVector(1, 2, 3).heading()returns 1.107, neither of them emitting a warning, so nothing has landed for this yet andheading()in src/math/p5.Vector.js still has no dimension check.My plan is to add the check there and cover 1D, 3D and 4D vectors plus the static
p5.Vector.heading(v)form in tests. #8944 does the same thing forcross()on non-3D vectors, so I would follow whatever shape that one settles on, for consistency. Happy to take it if nobody else is on it.limzykenneth commentedon Nov 14, 2026
@adityachoudhury29 Sorry for the late reply. Just to clarify the proposed implementation, why is the proposal to check for 1D, 3D and 4D vectors specifically?
PRIYANKA-NEGI-28 commentedon Nov 25, 2026
To follow up on @limzykenneth's question: looking at
src/math/p5.Vector.js,p5.Vectoralready exposesget dimensions() { return this.values.length; }(line 119), whileheading()(lines 1929–1933) doesn't check dimensions at all.Checking
this.dimensions !== 2directly avoids hardcoding 1D/3D/4D and covers any arbitrary N-dimensional vector (0D, 1D, 3D, 4D, 5D, etc.) uniformly while keepingMath.atan2(this.y, this.x)unchanged.One edge case worth confirming before implementing: what is
this.dimensionsfor a zero-argumentcreateVector()? IfcreateVector()defaults to[0, 0, 0](3D) or[0, 0](2D) or[](0D), we should make sure that matches the intended Friendly Error System warning behavior — and verify thatp5.disableFriendlyErrors = trueand standalonenew p5.Vector(...)calls behave cleanly. Happy to help with a PR and unit tests if @adityachoudhury29 isn't actively working on it!adityachoudhury29 commentedon Dec 3, 2026
Hey @limzykenneth , sorry for the late reply!
The check itself would be one general condition on the existing dimensions getter (this.values.length), which covers every non-2D size:
As discussed above, the result doesn't change. It's still atan2(y, x), so this only adds a non-blocking warning. The static p5.Vector.heading(v) just calls v.heading(), so it gets the warning too.
I picked those test cases because each one fails in a different way today:
1D: y is missing and the getter falls back to 0, so createVector(5).heading() quietly returns 0.
3D: probably the most common misuse. z is silently ignored (createVector(1, 2, 3).heading() returns about 1.107).
4D: shows that the warning comes from the general !== 2 condition and isn't a special case for 3D.
Also, I'd follow the existing setHeading() pattern instead, a friendly error tagged with the method name, using this._friendlyError so it's a no-op when p5.Vector is used on its own. Does that approach sound good?
limzykenneth commentedon Dec 3, 2026
@adityachoudhury29 You can go ahead with the proposed implementation with one change, the
this._friendlyError()is now superceded byp5.FES.log, if possible please try to use that instead, you can find usage pattern for it elsewhere in the codebase, if it is not clear do ask me.adityachoudhury29 commentedon Dec 3, 2026
Sure @limzykenneth , will start on this now.
Kindly assign the issue to me. Thanks!
adityachoudhury29 commentedon Dec 3, 2026
@limzykenneth I do have 2 doubts:
Also, a heads up -> FES.log returns undefined when p5.disableFriendlyErrors is true, so calling the result directly throws. I am planning on guarding the call with Vector.friendlyErrorsDisabled(). Same as the constructor.
limzykenneth commentedon Dec 3, 2026
For rotate it should print the message as well since it also don't have an implementation beyond 2D. You can make the message more generic.
It is fine the message print on every call for now, the reason is the same as why I would not suggest dong the Vector.friendlyErrorsDisabled() method, if we handle it this way it means that every single part of the code base that uses FES need to have their implementation to handle these. It is much better for FES or core to take care of limiting repeated messages and cases where FES is disabled. We don't have to take care of the repeated print for now. The FES log return undefined I can make a fix and push to your PR.
adityachoudhury29 commentedon Dec 3, 2026
Sounds good.
I have pushed my changes. PTAL-> #9442