gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
如何用python实现2d柏林噪声,效果是中间一堆是大于0,四周的尽量小于0
- User
如何用python实现2d柏林噪声,效果是中间一堆是大于0,四周的尽量小于0
- Assistant
以下是使用Python实现2D柏林噪声的代码,代码中使用了Perlin噪声算法:
import numpy as np import math def perlin_noise_2d(shape, res): def f(t): return 6 * t ** 5 - 15 * t ** 4 + 10 * t ** 3 delta = (res[0] / shape[0], res[1] / shape[1]) d = (shape[0] // res[0], shape[1] // res[1]) grid = np.mgrid[0:res[0]:delta[0], 0:res[1]:delta[1]].transpose(1, 2, 0) % 1 # Gradients angles = 2 * math.pi * np.random.rand(res[0] + 1, res[1] + 1) gradients = np.dstack((np.cos(angles), np.sin(angles))) g00 = gradients[0:-1, 0:-1].repeat(d[0], 0).repeat(d[1], 1) g10 = gradients[1:, 0:-1].repeat(d[0], 0).repeat(d[1], 1) g01 = gradients[0:-1, 1:].repeat(d[0], 0).repeat(d[1], 1) g11 = gradients[1:, 1:].repeat(d[0], 0).repeat(d[1], 1) # Ramps n00 = np.sum(np.dstack((grid[:, :, 0], grid[:, :, 1])) * g00, 2) n10 = np.sum(np.dstack((grid[:, :, 0] - 1, grid[:, :, 1])) * g10, 2) n01 = np.sum(np.dstack((grid[:, :, 0], grid[:, :, 1] - 1)) * g01, 2) n11 = np.sum(np.dstack((grid[:, :, 0] - 1, grid[:, :, 1] - 1)) * g11, 2) # Interpolation t = f(grid) n0 = n00 * (1 - t[:, :, 0]) + t[:, :, 0] * n10 n1 = n01 * (1 - t[:, :, 0]) + t[:, :, 0] * n11 return np.sqrt(2) * ((1 - t[:, :, 1]) * n0 + t[:, :, 1] * n1) noise = perlin_noise_2d((600, 600), (100, 100)) noise = (noise - np.min(noise)) / (np.max(noise) - np.min(noise)) noise = (noise - 0.5) * 2 mask = np.zeros((600, 600)) mask[200:400, 200:400] = 1 kernel = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]]) for i in range(5): mask = (np.convolve(np.convolve(mask, kernel, mode='same'), kernel.transpose(), mode='same') > 2).astype(int) result = noise * mask result = (result - np.min(result)) / (np.max(result) - np.min(result)) result = (result - 0.5) * 2 - User
python实现2d柏林噪声
- Assistant
以下是使用Python实现2D柏林噪声的代码,代码中使用了Perlin噪声算法:
import numpy as np import math def perlin_noise_2d(shape, res): def f(t): return 6 * t ** 5 - 15 * t ** 4 + 10 * t ** 3 delta = (res[0] / shape[0], res[1] / shape[1]) d = (shape[0] // res[0], shape[1] // res[1]) grid = np.mgrid[0:res[0]:delta[0], 0:res[1]:delta[1]].transpose(1, 2, 0) % 1 # Gradients angles = 2 * math.pi * np.random.rand(res[0] + 1, res[1] + 1) gradients = np.dstack((np.cos(angles), np.sin(angles))) g00 = gradients[0:-1, 0:-1].repeat(d[0], 0).repeat(d[1], 1) g10 = gradients[1:, 0:-1].repeat(d[0], 0).repeat(d[1], 1) g01 = gradients[0:-1, 1:].repeat(d[0], 0).repeat(d[1], 1) g11 = gradients[1:, 1:].repeat(d[0], 0).repeat(d[1], 1) # Ramps n00 = np.sum(np.dstack((grid[:, :, 0], grid[:, :, 1])) * g00, 2) n10 = np.sum(np.dstack((grid[:, :, 0] - 1, grid[:, :, 1])) * g10, 2) n01 = np.sum(np.dstack((grid[:, :, 0], grid[:, :, 1] - 1)) * g01, 2) n11 = np.sum(np.dstack((grid[:, :, 0] - 1, grid[:, :, 1] - 1)) * g11, 2) # Interpolation t = f(grid) n0 = n00 * (1 - t[:, :, 0]) + t[:, :, 0] * n10 n1 = n01 * (1 - t[:, :, 0]) + t[:, :, 0] * n11 return np.sqrt(2) * ((1 - t[:, :, 1]) * n0 + t[:, :, 1] * n1) noise = perlin_noise_2d((600, 600), (100, 100)) noise = (noise - np.min(noise)) / (np.max(noise) - np.min(noise)) noise = (noise - 0.5) * 2
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.