processing / processing/p5.js

[p5.js 2.0+ Bug Report]: Vector.cross() assumes the vectors have three dimensions

Open
#8,925 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area:Math p5.js 2.0+
Dominant language
JavaScript
Stars
24k
Forks
3.8k
Avg merge
3d 16h
Merged PRs (30d)
25

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:
  1. Create two vectors with more than three dimensions
  2. Use Vector.cross() to compute their cross product
  3. Observe no errors and a (meaningless) 3D result
Snippet:
function setup() {
  let v1 = createVector(1, 0, 0, 2);
  let v2 = createVector(0, 0, 1, -1);
  let cp = v1.cross(v2);
  
  print(cp.toString());
}
Discussion

The vector cross product is only defined for 3D vectors. I think Vector.cross() should give an error if either vector is not 3D.

Perhaps an special case should be made for 2D vectors, setting their "z" value to zero for the cross product computation. This is in fact what is currently done since the code uses Vector.z, which returns 0 for 2D vectors. Also, the cross() documentation, after stating "This method should only be used with 3D vectors", uses 2D vectors in the examples! Those examples would break if a strict check for three dimensions is implemented. So would the current implementations of angleBetween() and slerp().

This issue applies to both the instance and static methods.

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.

Research direction

Start by reading the Vector.cross() documentation and inspecting both its instance and static methods. Compare the handling of 2D and higher-dimensional vectors with the related angleBetween() and slerp() behavior, then define consistent validation and documentation updates for the affected methods and verify both API forms.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.