【问题标题】:Why are these shapes the wrong color?为什么这些形状的颜色不对?
【发布时间】:2013-06-05 19:39:34
【问题描述】:

所以我正在编写一个处理草图来测试我正在研究的焦土克隆的随机地形生成器。它似乎按预期工作,但有一个小问题。在代码中,我生成了 800 个 1 像素宽的矩形,并预先将填充设置为棕色。矩形的组合应该是一个实心块,带有类似棕色的泥土颜色 (77,0,0)。

但是,无论设置的 rgb 填充值如何,组合都会显示为黑色。我认为这可能与每个矩形的边框为黑色有关?有谁知道这里发生了什么?

final int w = 800;
final int h = 480;

void setup() {
  size(w, h);
  fill(0,128,255);
  rect(0,0,w,h);
  int t[] = terrain(w,h);
  fill(77,0,0);
  for(int i=0; i < w; i++){
    rect(i, h, 1, -1*t[i]);
  }
}

void draw() {

}

int[] terrain(int w, int h){

    width = w;
    height = h;

    //min and max bracket the freq's of the sin/cos series
    //The higher the max the hillier the environment
    int min = 1, max = 6;

    //allocating horizon for screen width
    int[] horizon = new int[width];
    double[] skyline =  new double[width];

    //ratio of amplitude of screen height to landscape variation
    double r = (int) 2.0/5.0;

    //number of terms to be used in sine/cosine series
    int n = 4;

    int[] f = new int[n*2];


    //calculating omegas for sine series
    for(int i = 0; i < n*2 ; i ++){
      f[i] = (int) random(max - min + 1) + min;
    }

    //amp is the amplitude of the series
    int amp =  (int) (r*height);

    for(int i = 0 ; i < width; i ++){
      skyline[i] = 0;

      for(int j = 0; j < n; j++){
        skyline[i] += ( sin( (f[j]*PI*i/height) ) +  cos(f[j+n]*PI*i/height) );
      }

      skyline[i] *= amp/(n*2);
      skyline[i] += (height/2);
      skyline[i] = (int)skyline[i];
      horizon[i] =  (int)skyline[i];
    }
    return horizon;
}

【问题讨论】:

  • 对于一个像素你也可以使用point(x,y),那些使用stroke来设置颜色。

标签: colors processing shapes


【解决方案1】:

我认为这可能与每个矩形的边框为黑色有关?

我相信是这样的。在您的 setup() 函数中,我在绘制矩形之前添加了 noStroke() 函数。这将删除矩形的黑色轮廓。由于每个矩形只有 1 个像素宽,因此无论您之前尝试选择哪种颜色,使用此黑色笔触(默认情况下启用)会使每个矩形的颜色变为黑色。

这是一个更新的setup() 函数 - 我现在看到了红褐色的地形:

void setup() {
  size(w, h);
  fill(0, 128, 255);
  rect(0, 0, w, h);
  int t[] = terrain(w, h);
  fill(77, 0, 0);
  noStroke(); // here
  for (int i=0; i < w; i++) {
    rect(i, h, 1, -1*t[i]);
  }
}

【讨论】:

    猜你喜欢
    • 2019-12-27
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多