Most appropriate sub-area of p5.js?
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.
Most appropriate sub-area of p5.js?
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:
Snippet:
Discussion
The
angleBetween()method currently usescross()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, soangleBetween()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.