p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

p5.js Vector.fromAngle does not respect angleMode(DEGREES) and docs don't indicate units of angle needed

Open FriskyDev opened this issue 8 years ago • 7 comments

Nature of issue?

  • [x ] Found a bug
  • [ ] Existing feature enhancement
  • [ ] New feature request

Most appropriate sub-area of p5.js?

  • [ ] Color
  • [ ] Core
  • [ ] Data
  • [ ] Events
  • [ ] Image
  • [ ] IO
  • [x ] Math
  • [ ] Typography
  • [ ] Utilities
  • [ ] WebGL
  • [ ] Other (specify if possible)

Which platform were you using when you encountered this?

  • [ ] Mobile/Tablet (touch devices)
  • [x ] Desktop/Laptop
  • [ ] Others (specify if possible)

Details about the bug: If you set angleMode(DEGREES), the Vector.fromAngle() functions does not respect this. It appears that the angle supplied to the function must always be in radians. If you are computing everything else with degrees, this makes it a more complicated call, which should just be handled by the function. Also, the documentation does not state the units to be used in the call. If fixed above it would need to indicate that it uses the angle mode set by angleMode(), which defaults to radians.

  • p5.js version: p5.js v0.5.16 October 11, 2017
  • Web browser and version: Google Chrome | 63.0.3239.132
  • Operating System: Windows 10/Linux Ubuntu 17.10
  • Steps to reproduce this: function setup() { angleMode(RADIANS); var vecD = p5.Vector.fromAngle(90); var vecR = p5.Vector.fromAngle(PI/2); console.log(vecD); console.log(vecR); angleMode(DEGREES); vecD = p5.Vector.fromAngle(90); vecR = p5.Vector.fromAngle(PI/2); console.log(vecD); console.log(vecR); }

You would expect the two angleMode() settings to cause a change in the behavior.

Output: page.js:9 p5.Vector {x: -0.4480736161291702, y: 0.8939966636005579, z: 0, name: "p5.Vector"} page.js:10 p5.Vector {x: 6.123233995736766e-17, y: 1, z: 0, name: "p5.Vector"} page.js:14 p5.Vector {x: -0.4480736161291702, y: 0.8939966636005579, z: 0, name: "p5.Vector"} page.js:15 p5.Vector {x: 6.123233995736766e-17, y: 1, z: 0, name: "p5.Vector"}

FriskyDev avatar Jan 14 '18 19:01 FriskyDev

yeah, the issue here is that p5.Vector.fromAngle is a static method and so cannot refer to a p5 instance (that contains the angleMode state). the docs should definitely be updated to reflect this.

maybe additional p5 instance methods createPolarVector(theta, length?)/createSphericalVector(theta, phi, length?) could address this?

Spongman avatar Jan 14 '18 19:01 Spongman

I ended up doing something similar:

p5.Vector.fromAngle(((angle + 90) / 180 * PI) - PI);

The +90 degrees is the correction based on the orientation in my model. Seems to work fine.

I understand about the static, so if the docs could be updated, that would be sufficient.

FriskyDev avatar Jan 15 '18 05:01 FriskyDev

Another option might be to overload createVector() with a third/fourth argument that's a constant/String, i.e.

let v1 = createVector(100, 100);                // default "Cartesian" mode
let v2 = createVector(90, 1, POLAR);            // switch to "Polar" mode
let v3 = createVector(100, 100, 100) ;          // default "Cartesian" mode for 3D
let v4 = createVector(90, 180, 100, SPHERICAL); // switch to "Spherical" mode for 3D

I might prefer this over adding a lot of new methods? If we said POLAR for 3D is that horrible?

(Now I'm remembering why we didn't have angleMode() in Processing 😆)

shiffman avatar Jan 15 '18 16:01 shiffman

I would recommend against having parameters whose value changes the type/number of other required parameters. It significantly complicates the requirements of the parameter validation code. This is why we can't automatically validate parameters for (or generate typescript definitions for) functions like 'httpDo' & 'losdJSON'.

Spongman avatar Jan 15 '18 18:01 Spongman

@shiffman is the cause of all of this (forcing me to watch his vids and make stuff) 🤣

I actually would have been happy if the documentation had just indicated the units and that they don't obey the angleMode if you set it.

I don't feel strongly about fixing this if the doc is added. But, going down that road, I would likely say (not knowing the implications of saying this) to turn the static method an instance method that can access angleMode(). I like the API surface area that is there and it makes sense to me. This stuff is super powerful, and I really like it a lot. So, I would just make what feels right work the way you would expect.

FriskyDev avatar Jan 17 '18 00:01 FriskyDev

@Spongman's points make sense and the pull request #2563 looks great to me, at least having the documentation right is best for now and we can always revisit! Thank you for the contribution!

shiffman avatar Jan 21 '18 14:01 shiffman

:ship: :shipit:

chuckeng-isp avatar Jan 26 '18 13:01 chuckeng-isp