[p5.js 2.0+ Bug Report]: Vector angleBetween() doesn't work for vectors with more than three dimensions
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 74/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Quiet
- Tech stack
- javascript
- Domain
- frontend
Research direction
Locate the Vector angleBetween() instance and static methods in the p5.js math implementation, then inspect how they use cross() for angle calculation. Verify the behavior with the four-dimensional example and confirm that both methods return PI/2 for perpendicular vectors while continuing to work for 2D and 3D vectors.
Written by the indexing model from the issue text.
Description
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:
- Create two four dimensional vectors
- Use angleBetween to compute the angle between them
- Note that it is incorrect (although no errors were reported)
Snippet:
function setup() {
let v1 = createVector(1, 0, 0, 0);
let v2 = createVector(0, 0, 0, 1);
let theta = v1.angleBetween(v2);
// The correct angle between v1 and v2 is PI/2, but theta is 0
print(theta);
}
Discussion
The angleBetween() method currently uses cross() to compute the angle. This was fine in p5.js version 1 since vectors were always 3D. But version 2 vectors can have any number of dimensions, so angleBetween() needs to use a more general method. I recommend using the dot product divided by the product of the vector lengths:
angleBetween(v1, v2) = acos(v1.dot(v2) / (v1.mag() * v2.mag()))
This will work for vectors of any dimension (including 2D and 3D vectors).
This issue applies to both the instance and static methods.
- Dominant language
- JavaScript
- Stars
- 24k
- Forks
- 3.8k
- Avg merge
- 3d 9h
- Merged PRs (30d)
- 26
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from processing/p5.js
-
Area:Math p5.js 2.0+
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
processing/p5.js#9197 · 3 comments · 1 assignee ·
-
Area:Core Area:DOM
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
processing/p5.js#9189 ·
-
[Bug]: p5.Image.prototype.copy and blend do not scale destination coordinates for high pixel density Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
processing/p5.js#9169 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
processing/p5.js#9129 ·
-
[p5.js 2.0+ Bug Report]: splitTokens() returns [] for a caret delimiter and throws for a backslash Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
processing/p5.js#9126 · 1 comment ·
All issues in processing/p5.js
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
HarperFast/skills#96 ·
-
[Block] Latest Posts [Type] Bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Automattic/studio#4908 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
sugarlabs/musicblocks#8847 ·