【问题标题】:Understanding non-random output when trying to create random pixels on canvas尝试在画布上创建随机像素时了解非随机输出
【发布时间】:2014-04-12 03:00:56
【问题描述】:

我正在使用 processing.js。对于 draw() 中循环的每次迭代,我在画布上的任意位置定义一个随机中心点 (x, y),然后在 80 到 100 之间定义一个随机宽度和高度。这定义了一个矩形。然后我遍历矩形内的每个点并确定是否要绘制一个点。绘制点的概率取决于它与中心的距离。我现在试图理解为什么我经常看到从矩形的左上角到右下角的填充条纹。

类似this:

这是我的代码。

void setup() {
    size(500, 500); // size of canvas
    background(0); // black background
    noLoop();
}

void draw() {

    // clusters

    int x;
    int y; // center coordinates of clusters
    int halfWidth; 
    int halfHeight; // halves of width and height of clusters

    for (int i = 0; i < 3; i++) {
        x = int(random(500));
        y = int(random(500));
        halfWidth = int(random(40, 50));
        halfHeight = int(random(40, 50));
        for (int j = x - halfWidth; j < x + halfWidth; j++) {
            for (int k = y - halfHeight; k < y + halfHeight; k++) {
                if (int(random(1, dist(x, j, y, k))) == 1) {
                    stroke(color(int(random(0, 255)), int(random(0, 255)), int(random(0, 255))));
                    point(j, k);
                }
            }
        }   
    }   
}

【问题讨论】:

    标签: javascript canvas random processing.js


    【解决方案1】:

    这很愚蠢。 dist() 需要 dist(x1, y1, x2, y2) 而我有 dist(x1, x2, y1, y2)。

    【讨论】:

    • 你应该得到一个 JS IDE ;)。
    猜你喜欢
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多