processing/p5.js

[2.x] Fix dimensional quirk in the `setHeading()` method of `p5.Vector`

Open

#8215 opened on Oct 28, 2025

View on GitHub
 (9 comments) (0 reactions) (1 assignee)JavaScript (20,784 stars) (3,178 forks)batch import
Area:MathBugCommunity Input NeededGood First IssueHelp Wantedp5.js 2.0+

Description

Problem

  • In 1.x, any heading can be applied to a vector made with createVector(1), as this is just a shorthand for creating the vector [1, 0, 0].
  • In 2.x, rotating a 1D vector by a value like HALF_PI would require its dimension to be mutated, which would likely be an unintended side effect.

Solution

The simplest solution is to have setHeading() support only 2D vectors in 2.x, adding error messages for vectors of other dimensions. This conforms to the documented definition of “heading” as an angle in the $xy$-plane, and fixes a quirk of the 1.x feature. It's also consistent with the proposed fix for heading() in #8214.

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 .z and toString()).
  • #8155: Consensus on a user-facing API for checking dimension size would enable a more stable implementation of setHeading(). 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.

Contributor guide