gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
python实现2d柏林噪声
- User
python实现2d柏林噪声
- Assistant
以下是Python实现2D柏林噪声的代码:
import random import math def dot(v1, v2): return v1[0]*v2[0]+v1[1]*v2[1] def perlin_noise(x, y, seed=0): def fade(t): return t * t * t * (t * (t * 6 - 15) + 10) def lerp(t, a, b): return a + t * (b - a) def grad(h, x, y): if h == 0: return (x, y) elif h == 1: return (-x, y) elif h == 2: return (x, -y) elif h == 3: return (-x, -y) # set up grid random.seed(seed) grid = [[(random.randint(0, 3), random.uniform(-1, 1), random.uniform(-1, 1)) for _ in range(2)] for _ in range(2)] # find the grid cells xi = math.floor(x) yi = math.floor(y) # find the distance between the point and the cell xf = x - xi yf = y - yi # find the dot product between the gradient and the distance vectors n00 = dot(grad(grid[xi%2][yi%2][0], xf, yf), (xf, yf)) n10 = dot(grad(grid[(xi+1)%2][yi%2][0], xf-1, yf), (xf-1, yf)) n01 = dot(grad(grid[xi%2][(yi+1)%2][0], xf, yf-1), (xf, yf-1)) n11 = dot(grad(grid[(xi+1)%2][(yi+1)%2][0], xf-1, yf-1), (xf-1, yf-1)) # interpolate values u = fade(xf) v = fade(yf) x0 = lerp(n00, n10, u) x1 = lerp(n01, n11, u) return lerp(v, x0, x1)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.