processing / processing/p5.js

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

Open
#8,215 10 comments 0 reactions 1 assignee View on GitHub

@reshma045 is already working on this.

Since Oct 30, 2025.

Area:Math Bug Community Input Needed Good First Issue Help Wanted p5.js 2.0+
Dominant language
JavaScript
Stars
24k
Forks
3.8k
Avg merge
3d 16h
Merged PRs (30d)
25

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.