gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
使用python创建一个2D柏林噪声
- User
使用python创建一个2D柏林噪声
- Assistant
以下是使用Python创建2D柏林噪声的代码示例。
import numpy as np from numpy.random import default_rng # Generate a random gradient for each corner of the grid corners = np.array([[1, 1], [-1, 1], [1, -1], [-1, -1]]) rng = default_rng() gradients = rng.standard_normal((4, 2)) def perlin_noise_2d(x, y, repeatx=1024, repeaty=1024): """ Generate 2D Perlin noise for the given (x,y) coordinate. repeatx and repeaty define the size of the grid. Increasing these values will make the noise smoother and slower to compute. """ # Wrap around the coordinates to repeat the noise x = x % repeatx y = y % repeaty # Find the coordinates of the grid cell that contains the point ix = int(x) iy = int(y) # Find the position of the point within the cell fx = x - ix fy = y - iy # Compute the dot products between the gradients and the vectors # from each corner to the point p00 = np.dot(gradients[0], [fx, fy]) p10 = np.dot(gradients[1], [fx-1, fy]) p01 = np.dot(gradients[2], [fx, fy-1]) p11 = np.dot(gradients[3], [fx-1, fy-1]) # Interpolate along the x-axis wx = _smootherstep(fx) p0 = (1-wx)*p00 + wx*p10 p1 = (1-wx)*p01 + wx*p11 # Interpolate along the y-axis wy = _smootherstep(fy) p = (1-wy)*p0 + wy*p1 return p def _smootherstep(t): """ Apply the smootherstep function to the given value t, which should be between 0 and 1. Returns a value between 0 and 1 that is smoother than t. """ return t * t * t * (t * (t * 6 - 15) + 10) # Create an image of Perlin noise sx, sy = 512, 512 image = np.zeros((sy, sx)) for y in range(sy): for x in range(sx): # Generate Perlin noise for each pixel noise = perlin_noise_2d(x/4, y/4, repeatx=sx/4, repeaty=sy/4) # Normalize the noise to [0, 1] noise = (noise + 1) / 2 # Set the pixel color based on the noise value image[y, x] = noise # Display the image using matplotlib import matplotlib.pyplot as plt plt.imshow(image) 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.