简体   繁体   English

Processing.js形状不起作用

[英]Processing.js shape not working

I have a function that I wrote to draw a polygon with any number of sides. 我有一个函数,我写了绘制一个具有任意数量的边的多边形。 However, when I run it in Java-script only, it doesn't work. 但是,当我仅在Java脚本中运行它时,它不起作用。 Why? 为什么?

    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();
}}

It just draws a weird zigzag shape. 它只是绘制一个奇怪的锯齿形状。 (I implement this function later in my code and it works okay, just the shape is messed up.) (我稍后在我的代码中实现了这个功能,它运行正常,只是形状搞砸了。)

In the future, you might want to edit your question itself instead of just commenting with a JSFiddle. 将来,您可能希望编辑问题本身而不是仅使用JSFiddle进行注释。 You might also @reply each person who asked for it. 您也可能@reply每个要求它的人。 Otherwise we won't see it! 否则我们不会看到它! Anyway, thanks for putting it together. 无论如何,谢谢你把它放在一起。

Take a look at this line: 看看这一行:

processing.vertex(centerX + radius*Math.sin(rotationAngle), centerY + radius*Math.cos(rotationAngle));

Notice that you're feeding rotationAngle into the sin() and cos() functions. 请注意,您正在将rotationAnglesin()cos()函数。 Also notice that rotationAngle is specified in degrees. 另请注意, rotationAngle以度为单位指定。 (Values between 0 and 360.) (0到360之间的值。)

This works okay on Khan Academy because by default their functions take degrees. 这对可汗学院没问题,因为默认情况下他们的功能需要学位。

However, normal Processing and Processing.js take arguments in radians ! 但是,正常的Processing和Processing.js以弧度为参数! (Values between 0 and 2 PI.) So you have to convert your rotationAngle variable into a radian value! (值介于0到2之间。)因此,您必须将rotationAngle变量转换为弧度值! You can use the handy radians() function to do that. 你可以使用方便的radians()函数来做到这一点。

More info can be found in the reference . 更多信息可以在参考资料中找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM