映射噪声(漫步游走程序)

2022-03-22  本文已影响0人  大龙10

书名:代码本色:用编程模拟自然系统
作者:Daniel Shiffman
译者:周晗彬
ISBN:978-7-115-36947-5

0.6.1 映射噪声

1、如何处理得到的噪声值

  我们开始研究如何处理得到的噪声值。

图0-8

2、映射

  我们知道噪声函数的返回值在0~1的范围内,但我们想要在0到窗口宽度的范围内画这个圆。

float t = 0;
void draw() {
  float n = noise(t);
  float x = map(n,0,1,0,width); 用map()函数定制Perlin噪声的范围
  ellipse(x,180,16,16);
  t += 0.01;
}

3、Perlin噪声游走模型

class Walker {
  float x, y;
  float tx, ty;

  float prevX, prevY;

  Walker() {
    tx = 0;
    ty = 10000;
    x = map(noise(tx), 0, 1, 0, width);
    y = map(noise(ty), 0, 1, 0, height);
  }

  void render() {
    stroke(255);
    line(prevX, prevY, x, y);
  }

  // Randomly move according to floating point values
  void step() {

    prevX = x;
    prevY = y;

    x = map(noise(tx), 0, 1, 0, width);
    y = map(noise(ty), 0, 1, 0, height);

    tx += 0.01;
    ty += 0.01;

  }
}
图0-9
上一篇 下一篇

猜你喜欢

热点阅读