p5.Vector 不尊重角度模式

p5.Vector not respecting angleMode

我注意到,在旋转矢量时,即使在我用 DEGREES 调用 angleMode 之后,旋转也是以弧度为单位进行的。此行为仅出现在使用 new p5.Vector 语法声明的向量中,而使用 createVector 可避免这种情况。

最小可重现示例:(粘贴到 p5 网络编辑器或 OpenProcessing)

function setup() {
    createCanvas(400, 400)
    translate(100, 100) // To see the effect more clearly
    angleMode(DEGREES)
    let v = new p5.Vector(1, 0)
    line(0, 0, v.x * 50, v.y * 50)
    v.rotate(HALF_PI)
    line(0, 0, v.x * 50, v.y * 50)
}

请注意,旋转仍然以弧度表示。如果将 new p5.Vector 替换为 createVector,那么问题就会消失。这是为什么?

因为像 sin() are effected, there is no good reason why p5.Vector.rotate 这样的数学函数不应该受到影响。
但是,它并不打算像您那样构建向量。您应该使用 createVector() 创建一个向量。此函数不仅创建对象,而且设置所有内部属性,以便对象按预期运行。

此行为未直接针对 rotate(), however it is documented for heading() 进行解释:

Calculate the angle of rotation for this vector(only 2D vectors). p5.Vectors created using createVector() will take the current angleMode into consideration, and give the angle in radians or degree accordingly.