gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
用python实现一个可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声
- User
用python实现一个可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声
- Assistant
以下是一个基于Python的实现,使用了numpy,matplotlib库。
import numpy as np import matplotlib.pyplot as plt def noise2d(x, y, frequency, lacunarity, persistence, octaves, seed): # 计算噪声 result = 0.0 amplitude = 1.0 for i in range(octaves): # 通过seed和坐标x、y计算一个随机数 rnd = np.random.RandomState(int(seed+i)).randint(0, 4096) wx, wy = x * frequency, y * frequency signal = np.sin(wx + np.cos(wy) + rnd) result += signal * amplitude frequency *= lacunarity amplitude *= persistence return result # 绘制柏林噪声的图像 plt.figure(figsize=(8, 8)) x = np.linspace(0, 5, 256) y = np.linspace(0, 5, 256) X, Y = np.meshgrid(x, y) Z = np.zeros((256, 256)) for i in range(256): for j in range(256): Z[i][j] = noise2d(x[i], y[j], 0.1, 2, 0.5, 6, 42) plt.imshow(Z, cmap=plt.get_cmap('gray_r')) plt.axis('off') plt.show()该实现中,noise2d函数接收五个参数:frequency、lacunarity、persistence、octaves和seed。frequency为噪声的基础频率,lacunarity为分形维度变化的程度,persistence为每个八度的幅度变化,octaves为基础频率的数量,seed为随机数种子。
在绘制图像时,使用了numpy库生成网格,然后通过noise2d函数计算每个点的值,最终使用matplotlib库将图像展示出来。可以看到,不同的参数会产生不同的柏林噪声。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.