2013-06-05 19 views
0

所以我正在编写一个处理草图来测试我正在处理的焦土克隆的随机地形生成器。它似乎按预期工作,但有一个小问题。在代码中,我生成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; 
} 
+1

对于一个像素,你也可以使用点(X,Y),这些用笔画来设置颜色。 –

回答

3

我想可能有一些做每个矩形边框被黑?

我相信是这样。在您的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]); 
    } 
}