gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
用python实现一个可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声
- User
用python实现一个可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声
- Assistant
以下是一个可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声的Python实现:
import math import random def noise(x, y, frequency, lacunarity, persistence, octaves, seed): random.seed(seed) total = 0 amplitude = 1 max_value = 0 for i in range(octaves): x_val = x * frequency y_val = y * frequency x_int = int(math.floor(x_val)) y_int = int(math.floor(y_val)) x_frac = x_val - x_int y_frac = y_val - y_int random.seed(seed + i) angle = 2 * math.pi * random.random() dx = math.cos(angle) dy = math.sin(angle) g00 = grad(x_int, y_int, x_val, y_val, dx, dy) g01 = grad(x_int, y_int + 1, x_val, y_val - 1, dx, dy) g10 = grad(x_int + 1, y_int, x_val - 1, y_val, dx, dy) g11 = grad(x_int + 1, y_int + 1, x_val - 1, y_val - 1, dx, dy) sx = smoothstep(x_frac) sy = smoothstep(y_frac) p0 = lerp(g00, g10, sx) p1 = lerp(g01, g11, sx) p = lerp(p0, p1, sy) total += p * amplitude max_value += amplitude amplitude *= persistence frequency *= lacunarity return total/max_value def grad(x_int, y_int, x_val, y_val, dx, dy): random.seed(101 * x_int + y_int) angle = 2 * math.pi * random.random() weight = dx * (x_val - x_int) + dy * (y_val - y_int) return weight * math.cos(angle), weight * math.sin(angle) def lerp(a, b, t): return a + t * (b - a) def smoothstep(t): return t * t * (3 - 2 * t)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.