Processing.js 形状无效
Processing.js shape not working
我编写了一个函数来绘制具有任意边数的多边形。但是,当我 运行 它仅在 Java-script 中时,它不起作用。为什么?
function sketchProc(processing) {
function polygon (sides, centerX, centerY, radius, fillColor, strokeColor) {
processing.fill(fillColor);
processing.stroke(strokeColor);
var innerAngle = 360/sides;
var rotationAngle = innerAngle;
processing.beginShape();
for (var i = 0; i < sides + 2; i++) {
processing.vertex(centerX + radius*Math.sin(rotationAngle), centerY + radius*Math.cos(rotationAngle));
console.log(centerX + radius*Math.sin(rotationAngle), centerY + radius*Math.cos(rotationAngle));
rotationAngle = innerAngle * i;
}
processing.endShape();
}}
它只是画了一个奇怪的之字形。 (我稍后在我的代码中实现了这个功能并且它工作正常,只是形状搞砸了。)
将来,您可能希望自己编辑问题,而不是仅仅使用 JSFiddle 进行评论。您也可以@reply 每个提出请求的人。不然我们看不到!不管怎样,谢谢你把它放在一起。
看看这一行:
processing.vertex(centerX + radius*Math.sin(rotationAngle), centerY + radius*Math.cos(rotationAngle));
请注意,您正在将 rotationAngle
送入 sin()
和 cos()
函数。另请注意 rotationAngle
以度为单位指定。 (值介于 0 和 360 之间。)
这在可汗学院上运行良好,因为默认情况下它们的函数是有度数的。
但是,正常处理和 Processing.js 接受 弧度 的参数! (值介于 0 和 2 PI 之间。)因此,您必须将 rotationAngle
变量转换为弧度值!您可以使用方便的 radians()
函数来做到这一点。
可以在 the reference 中找到更多信息。
我编写了一个函数来绘制具有任意边数的多边形。但是,当我 运行 它仅在 Java-script 中时,它不起作用。为什么?
function sketchProc(processing) {
function polygon (sides, centerX, centerY, radius, fillColor, strokeColor) {
processing.fill(fillColor);
processing.stroke(strokeColor);
var innerAngle = 360/sides;
var rotationAngle = innerAngle;
processing.beginShape();
for (var i = 0; i < sides + 2; i++) {
processing.vertex(centerX + radius*Math.sin(rotationAngle), centerY + radius*Math.cos(rotationAngle));
console.log(centerX + radius*Math.sin(rotationAngle), centerY + radius*Math.cos(rotationAngle));
rotationAngle = innerAngle * i;
}
processing.endShape();
}}
它只是画了一个奇怪的之字形。 (我稍后在我的代码中实现了这个功能并且它工作正常,只是形状搞砸了。)
将来,您可能希望自己编辑问题,而不是仅仅使用 JSFiddle 进行评论。您也可以@reply 每个提出请求的人。不然我们看不到!不管怎样,谢谢你把它放在一起。
看看这一行:
processing.vertex(centerX + radius*Math.sin(rotationAngle), centerY + radius*Math.cos(rotationAngle));
请注意,您正在将 rotationAngle
送入 sin()
和 cos()
函数。另请注意 rotationAngle
以度为单位指定。 (值介于 0 和 360 之间。)
这在可汗学院上运行良好,因为默认情况下它们的函数是有度数的。
但是,正常处理和 Processing.js 接受 弧度 的参数! (值介于 0 和 2 PI 之间。)因此,您必须将 rotationAngle
变量转换为弧度值!您可以使用方便的 radians()
函数来做到这一点。
可以在 the reference 中找到更多信息。