用python实现一个可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声
- User
用python实现一个可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声
- Assistant
import numpy as np import random
def noise_2d(width, height, frequency=16, lacunarity=2, persistence=0.5, octaves=8, seed=1): random.seed(seed) noise = np.zeros((width, height)) amplitude = 1 for i in range(octaves): x_off = random.randint(-100000, 100000) y_off = random.randint(-100000, 100000) freq = frequency * (lacunarity ** i) amplitude *= persistence for x in range(width): for y in range(height): noise[x][y] += amplitude * np.interp(noise_func((x + x_off) / freq, (y + y_off) / freq), [0, 1], [-1, 1]) return noise
def noise_func(x, y): n = int(x + y * 57) n = (n << 13) ^ n return (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0)
test
import matplotlib.pyplot as plt
noise = noise_2d(256, 256, frequency=16, lacunarity=2, persistence=0.5, octaves=8, seed=1) plt.imshow(noise, cmap="gray") plt.show()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.