如何减慢处理中的随机颜色生成器?

西里尔

大家好-我想从阵列中随机填充颜色的矩形网格图案。我可以按照自己的方式完成它-但是随机选择的速度太快了。我试图用frameRate()放慢一切;–但这会减慢整个动画的速度。(例如,如果我想添加其他内容)。然后,我尝试用来减慢它的速度,if(frameCount%20 == 0) {…}但这并不能保持绘制的网格-只让它每隔XXX帧出现一帧-有人知道我如何放慢它称为“色彩噪声”的速度吗?–谢谢您的任何帮助!

float size = 20;
color cbw = color(0, 0, 0);      //defines BLACK
color cg = color(0, 255, 0);     //defines GREEN
color cb = color(0, 0, 255);     //defines BLUE
color cw = color(255, 255, 255); //defines WHITE
color[] colors = {             //random selects one of above colors
cbw, cg, cb, cw
}; 

void setup() {
  size(1080, 1080);

}

void draw() {
  background(255);

  for (int x = 0; x < width/size; x++) {
    for (int y = 0; y < height/size; y++) {
      color c1 = (colors[int(random(0, 4))]);  //assigns a random color from above to c1-4
      fill(c1);
      noStroke();
      rect(size*x, size*y, size, size);
    }
  }
  }
乔治·普罗芬扎

您在的正确轨道上frameCount % 20(或者,您可以使用millis()

主要问题是颜色选择与矩形图紧密相关用简单的英语来说,当前您只能选择随机颜色并同时进行渲染,而不能单独选择颜色并进行渲染(例如,在不同时间)

一种选择是使用数组存储每个矩形的颜色,您可以使用两次:

  1. 将值写入:选择随机颜色
  2. 读取以下值:渲染矩形时

这是草图的修改后的版本,它说明了上面的想法:

float size = 20;
color cbw = color(0, 0, 0);      //defines BLACK
color cg = color(0, 255, 0);     //defines GREEN
color cb = color(0, 0, 255);     //defines BLUE
color cw = color(255, 255, 255); //defines WHITE
color[] colors = {             //random selects one of above colors
cbw, cg, cb, cw
}; 
// all colors for each rect
color[][] rectColors;

void setup() {
  size(1080, 1080);
  // allocate invidual rect colours
  rectColors = new color[width/(int)size][height/(int)size];
}

void draw() {
  background(255);
  
  if(frameCount%20 == 0){
    // randomize colours
    int numColors = colors.length;
    for (int x = 0; x < width/size; x++) {
      for (int y = 0; y < height/size; y++) {
        rectColors[x][y] = colors[int(random(0, numColors))];
      }
    }
  }

  for (int x = 0; x < width/size; x++) {
    for (int y = 0; y < height/size; y++) {
      color c1 = rectColors[x][y];  //assigns a random color from above to c1-4
      fill(c1);
      noStroke();
      rect(size*x, size*y, size, size);
    }
  }
 }

就我个人而言,我将做一些额外的事情,以使其更易于阅读并有可能在其他草图中重复使用:

  1. 更改float size = 20;int size = 20;假设您希望网格单元降落在整个像素上。这样就无需进行铸造(例如,宽度/(int)尺寸)
  2. 缓存/存储经常重复使用的数据(例如网格行和列)
  3. 封装将颜色随机化和将矩形渲染为独立函数的循环。甚至像不返回任何值且不接受任何参数的函数之类的简单操作(例如void setup(),例如)

这是可能的样子:

int size = 20;
color cbw = color(0, 0, 0);      //defines BLACK
color cg = color(0, 255, 0);     //defines GREEN
color cb = color(0, 0, 255);     //defines BLUE
color cw = color(255, 255, 255); //defines WHITE
color[] colors = {             //random selects one of above colors
cbw, cg, cb, cw
}; 
// all colours for each rect
color[][] rectColors;

// grid dimensions
int cols;
int rows;

void setup() {
  size(1080, 1080);
  // compute grid dimensions
  cols = width / size;
  rows = height / size;
  // allocate invidual rect colours
  rectColors = new color[cols][rows];
  // call randomize colours function
  randomizeColors();
}
// declare randomize colours function
void randomizeColors(){
  // read array length, avoding the previosuly hardcoded value (4)
  int numColors = colors.length;
  for (int x = 0; x < cols; x++) {
    for (int y = 0; y < rows; y++) {
      rectColors[x][y] = colors[int(random(0, numColors))];
    }
  }
}

void drawRectangles(){
  for (int x = 0; x < cols; x++) {
    for (int y = 0; y < rows; y++) {
      color c1 = rectColors[x][y];  //read a random color
      fill(c1);
      noStroke();
      rect(size * x, size * y, size, size);
    }
  }
}

void draw() {
  background(255);
  
  if(frameCount % 20 == 0){
    randomizeColors();
  }
  
  drawRectangles();
  
 }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

随机颜色生成器

Flutter:如何制作随机颜色生成器背景

如何用玩笑测试随机颜色生成器

如何使图像随机生成器颤动?

Java随机生成器如何工作?

如何修复随机选择生成器?

如何在随机十六进制颜色生成器上设置不变的透明度

在 ionic 3 和 Angular 4 中使用的文本中的随机颜色生成器

我需要一个批处理文件中的随机婴儿名称生成器

如何防止在随机数生成器中多次生成整数

随机图像生成器

如何在javascript中的数独生成器中重置函数中的随机性

生成器总是在随机句子生成器中返回相同的故障事件

如何在C ++中编写随机的大写char生成器?

如何停止我的应用中的随机数生成器而不冻结应用

如何从R中的随机数生成器指定奇数和偶数的数量?

如何在官方的“围棋之旅”中播种随机数生成器?

如何使用用户指定的种子在elm 0.17中使用随机生成器?

如何在Swift中为随机数生成器播种?

如何在结构中存储随机数生成器?

如何从随机名称生成器中删除重复项?

如何在Rust中为不同的线程克隆随机数生成器?

如何在as3中创建随机名称生成器

如何在随机字母/数字生成器中插入空格并对齐输出?

如何初始化数组中的随机数生成器以用于冒泡排序

我如何能够在 VB.net 中循环我的随机数生成器

如何在 Flutter 中创建随机数生成器?

为什么我的随机颜色生成器的JS代码在浏览器中不起作用

如何使用新的 NumPy 随机数生成器?