Skip to content

Vector lerp() function now takes in generalized dimensions - #8994

Open
mawerb wants to merge 15 commits into
processing:mainfrom
mawerb:vector-lerp-generalize-dimensions
Open

Vector lerp() function now takes in generalized dimensions#8994
mawerb wants to merge 15 commits into
processing:mainfrom
mawerb:vector-lerp-generalize-dimensions

Conversation

@mawerb

@mawerb mawerb commented Jul 20, 2026

Copy link
Copy Markdown

Resolves #8927

Changes:

  • Refactored lerp() to take in generalized dimensions allowing for function to be called on vectors with more than 3 dimensions and mismatched.
  • Refactored _validatedVectorOperation decorator to be able to take in trailingArgs in order to fit the case of lerp() where it follows the pattern of validating parameters its taking with ...args like the mult(), add(), sub() functions but requiring the extra argument amt
  • Added test for prioritizing smaller dimensions for lerp()
  • Refactored tests dependent on passing of 4 arguments to fit the new structure of taking in a vector, array, or number list
  • Added tests mentioned in issue [p5.js 2.0+ Bug Report]: Vector.lerp() is hard coded to use three dimensions #8927 and extra to ensure lerp() works as intended on mismatched dimensions and vectors with dimensions > 3

Screenshots of the change:
Screenshot 2026-07-20 at 3 57 17 PM

the 1 failed test was present before changes were made (it's the typography test)

PR Checklist

@p5-bot

p5-bot Bot commented Jul 20, 2026

Copy link
Copy Markdown

@perminder-17 perminder-17 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Comment thread src/math/patch-vector.js Outdated

@perminder-17 perminder-17 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything else looks good so far!

Comment thread src/math/p5.Vector.js Outdated
Comment on lines +2373 to +2382
* // META:norender
* function setup() {
* // Create a p5.Vector object.
* let v = createVector(1, 1, 1);
*
* // Interpolate with an array.
* v.lerp([3, 3, 3], 0.5);
*
* // Prints "p5.Vector Object : [2, 2, 2]" to the console.
* print(v.toString());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thought: if someone reads these examples, they have to open the console to see the output. Since multiple examples use the console, the messages can get mixed up. What do you think about using the text() function instead, so the output is shown directly on the canvas?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, agreed, switched the lerp examples to show the result on the canvas with text() so readers don’t need the console.

@mawerb
mawerb force-pushed the vector-lerp-generalize-dimensions branch from 627e97a to ec21120 Compare July 22, 2026 15:47
@ksen0

ksen0 commented Jul 22, 2026

Copy link
Copy Markdown
Member

This is looking really good, thanks both!

@mawerb

mawerb commented Jul 25, 2026

Copy link
Copy Markdown
Author

Hey! I was just curious regarding the next steps for getting my pr approved. This is my first time contributing to p5.js so I’m unsure how it works

@mawerb
mawerb requested a review from perminder-17 July 30, 2026 12:01
@mawerb

mawerb commented Aug 10, 2026

Copy link
Copy Markdown
Author

@perminder-17 @ksen0 bump. How do i move forward with getting this PR approved?

@davepagurek

Copy link
Copy Markdown
Contributor

Hi @mawerb! Before merging we make sure that the branch is up-to-date with main, but when I did so, I think maybe a test was added elsewhere (or possibly if it was removed before git was confused about the update and it's back?) and is now failing:

 FAIL   unit-tests (chromium)  test/unit/math/p5.Vector.js > p5.Vector > lerp > with p5.Vector > should call lerp with 4 arguments
AssertionError: expected "Mock" to be called with arguments: [ 1, 2, 3, 1 ]

Received:

  1st Mock call:

  [
+   Vector {
+     "isVector": true,
+     "values": [
        1,
        2,
        3,
+     ],
+   },
    1,
  ]


Number of calls: 1

 ❯ test/unit/math/p5.Vector.js:1525:24
    1523|         vi.spyOn(v, 'lerp');
    1524|         v.lerp(new Vector(1, 2, 3), 1);
    1525|         expect(v.lerp).toHaveBeenCalledWith(1, 2, 3, 1);
       |                        ^
    1526|       });
    1527|     });

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

Do you think you could take a look at that test and help resolve the merge?

Other than that, @perminder-17 anything else need updating in the code before merging?

@perminder-17 perminder-17 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really sorry for the delay, everything looks good to me! Approved!

Just the same concern on the test failure,

@mawerb

mawerb commented Aug 13, 2026

Copy link
Copy Markdown
Author

Thanks for catching that @davepagurek ! That test came back when merging with main. It was checking the old 3D behavior where v.lerp(vector, amt) got rewritten to four numeric arguments (x, y, z, amt). With the N-D update that rewrite is gone, since lerp now takes (values, amt) instead, so the assertion was outdated.

I removed that test. The vector form is already covered by the behavior test that checks the resulting components. The suite is passing again on my end.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants