Skip to content

[p5.js 2.0+ Bug Report]: Vector.lerp() is hard coded to use three dimensions #8927

Closed

Description

@sidwellr

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • WebGPU
  • p5.strands
  • Build process
  • Unit testing
  • Internationalization
  • Friendly errors
  • Other (specify if possible)

p5.js version

2.3.0

Web browser and version

Brave 1.90.122

Operating system

Linux Mint 22.1

Steps to reproduce this

Steps:

  1. Create two 4D vectors
  2. Use lerp() to interpolate between the two
  3. Note the result only works for the first three dimensions

Snippet:

function setup() {
  let v1 = createVector(0, 1, 0, 1);
  let v2 = createVector(1, 0, 1, 0);
  v1.lerp(v2, 0.5);
  
  // v1 should be vector[0.5, 0.5, 0.5, 0.5], but is actually vector[0.5, 0.5, 0.5, 1]
  print(v1.toString());
}

Discussion

lerp() currently computes x, y, and z individually. To extend to vectors of any dimension, other vector methods can be used, like p5.Vector.add(v1, p5.Vector.sub(v2, v1).mult(c)). Of course, this needs to be adapted to both the instance and static methods.

Activity

  1. added this to the 2.4 milestone on Jun 17, 2026
  2. ksen0 commented on Jun 17, 2026

    @ksen0
    Member

    Hi everyone, this is reserved for the CodeDay microinternship. If it is not completed by them, then it can be opened to volunteers. But for now, please do not make PRs or volunteer - thanks for understanding!

    The solution should generalize lerp() to N dimensions:

    • add unit tests checking behavior for beyond 3d dimensionalities, as well as for operations between vectors of mismatched dimensions
    • when performing binary operation on vectors of different dimensions, use smallest vector priority: check the implementation of add, sub, div, mult, rem and their behavior on mismatched sized vectors
  3. sidwellr commented on Jun 24, 2026

    @sidwellr
    Author

    The solution should also update the documentation to match the new behavior.

    Also, lerp needs to allow the second vector to be specified using a list of numbers. For example, the following code should print "vector[2,2]":

    function setup() {
      let v = createVector(1, 1);
      v.lerp(3, 3, 0.5);
      print(v.toString());
    }

    The dot method may be a better pattern to follow for this than add, sub, etc. But it is a bit tricky since the last parameter is the interpolation amount, not part of the second vector.

    The add, sub, div, mult, and rem methods also allow an array parameter. To understand how this works requires knowing that a decorator _validatedVectorOperation is applied to them in patch-vector.js. This is not obvious from the code in p5.Vector.js.

  4. perminder-17 commented on Jul 20, 2026

    @perminder-17
    Collaborator

    Hi @mawerb, thanks for raising this PR! However, since this issue is currently reserved for the CodeDay microinternship. If it isn't completed by them, we can definitely revisit and review your code then. Thanks so much for your understanding and for your contribution!

  5. mawerb commented on Jul 20, 2026

    @mawerb
    Contributor

    Hi, @perminder-17. I am a student under the CodeDay SOSE microinternship. I am part of the team that was assigned to the issue!

  6. perminder-17 commented on Jul 20, 2026

    @perminder-17
    Collaborator

    Hi, @perminder-17. I am a student under the CodeDay SOSE microinternship. I am part of the team that was assigned to the issue!

    Thanks for letting me know, I'll review the PR then. :")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions