生成Perlin噪声地形
world = np.zeros(shape)
for i in range(shape[0]):
for j in range(shape[1]):
world[i][j] = noise.pnoise2(i / scale,
j / scale,
octaves=octaves,
persistence=persistence,
lacunarity=lacunarity,
repeatx=1024,
repeaty=1024,
base=42)
迭代更新地图
for _ in range(iterations):
new_cave = np.copy(cave)
for y in range(height):
for x in range(width):
计算周围8个邻居的状态
neighbors = sum(cave[max(0, y1):min(height, y+2), max(0, x1):min(width, x+2)].flatten()) cave[y][x]
if cave[y][x] == 1:
new_cave[y][x] = 1 if neighbors = 4 else 0 生存规则
else:
new_cave[y][x] = 1 if neighbors = 5 else 0 繁殖规则
cave = new_cave
python
import numpy as np
from scipy.spatial import Voronoi
import matplotlib.pyplot as plt
生成随机点
points = np.random.rand(10, 2)
构建Voronoi图
vor = Voronoi(points)
可视化Voronoi图
plt.figure(figsize=(8, 8))
plt.plot(points[:, 0], points[:, 1], 'ko') 显示点
for region in vor.regions:
if not 1 in region and len(region) 0:
polygon = [vor.vertices[i] for i in region]
plt.fill(*zip(*polygon), alpha=0.4)
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.show()