不断增加旋转度数(超过 360 度)是不好的做法吗

Is it bad practice to keep increasing rotational degrees (over 360)

我正在试验一个有创意的 javascript 框架 P5.js,而且我经常使用度数来旋转球体。但是,我通过不断增加一个变量并基于该变量进行旋转来做到这一点。无限增加变量是一种不好的做法吗?当它达到 360 时,我应该将旋转重置为 0 吗?

示例:

this.deg = 0;

this.show = function(){
    rotateY(radians(this.deg));
    sphere(this.x, this.y, this.r);
    this.deg++; // Continuously increasing the deg :(
}

好吧,这取决于。

如果您谈论它是否会影响 p5.js 的任何性能,那么不会,因为它很可能已经在学位上做了类似 this.deg%=360 的事情。

但是,您应该小心 JavaScript 中的非常大的整数,因为对于非常大的整数,您可能会失去精度或可能溢出大小。

除此之外,你真的应该把东西保持在 360 以下,以避免在调试过程中出现任何混乱。

执行此操作的任何简单方法是在您的代码中使用模数运算符,就像这样

this.deg = 0;

this.show = function(){
rotateY(radians(this.deg));
sphere(this.x, this.y, this.r);
this.deg++; 
this.deg%=360; // keep it under 360 deg , always
}

阅读 JavaScript 中有关整数安全限制的更多信息:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER and this Whosebug question : What is JavaScript's highest integer value that a Number can go to without losing precision?