gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
使用python实现一个2D柏林噪声
- User
使用python实现一个2D柏林噪声
- Assistant
柏林噪声是一种基于随机数生成的噪声,其特点是具有频谱平坦,且信号不断变化的特点。2D柏林噪声是将柏林噪声应用在二维平面上,可用于生成地形,纹理等效果。
下面是一个使用python实现2D柏林噪声的示例代码:
import random import math #定义柏林噪声函数 def noise(x,y): # 去掉小数部分 x_int = int(x) y_int = int(y) # 随机值 n = x_int + y_int * 57 n = (n<<13) ^ n return (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0) #定义插值函数 def smooth_interpolate(x, y, frac_x, frac_y): v1 = noise(x, y) v2 = noise(x + 1, y) v3 = noise(x, y + 1) v4 = noise(x + 1, y + 1) i1 = v1 * (1 - frac_x) + v2 * frac_x i2 = v3 * (1 - frac_x) + v4 * frac_x return i1 * (1 - frac_y) + i2 * frac_y #定义2D柏林噪声函数 def perlin_noise_2d(x, y): total = 0 frequency = 1 amplitude = 1 octaves = 4 #循环每个八度 for i in range(octaves): total += smooth_interpolate(x * frequency, y * frequency, x * frequency - math.floor(x * frequency), y * frequency - math.floor(y * frequency)) * amplitude frequency *= 2 amplitude *= 0.5 return total # 生成柏林噪声二维平面图像 width = 200 height = 200 image = [] for y in range(height): row = [] for x in range(width): value = perlin_noise_2d(x/width, y/height) row.append(int(value*255)) image.append(row)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.