生成随机高斯粗糙表面python
时间: 2024-04-02 14:30:29 浏览: 493
生成随机高斯粗糙表面是一种常见的任务,可以使用Python中的NumPy库来实现。下面是一个简单的示例代码:
```python
import numpy as np
def generate_rough_surface(size, roughness):
# 生成一个空的二维数组
surface = np.zeros((size, size))
# 设置初始边界值
surface[0, 0] = np.random.normal(0, roughness)
surface[0, size-1] = np.random.normal(0, roughness)
surface[size-1, 0] = np.random.normal(0, roughness)
surface[size-1, size-1] = np.random.normal(0, roughness)
# 递归生成表面
divide(size-1, roughness, surface)
return surface
def divide(size, roughness, surface):
half = size // 2
if half < 1:
return
# 平滑四个角点
surface[half, half] = (surface[0, 0] + surface[0, size] + surface[size, 0] + surface[size, size]) / 4 + np.random.normal(0, roughness)
# 平滑四条边中点
surface[half, 0] = (surface[0, 0] + surface[0, size]) / 2 + np.random.normal(0, roughness)
surface[half, size] = (surface[size, 0] + surface[size, size]) / 2 + np.random.normal(0, roughness)
surface[0, half] = (surface[0, 0] + surface[size, 0]) / 2 + np.random.normal(0, roughness)
surface[size, half] = (surface[0, size] + surface[size, size]) / 2 + np.random.normal(0, roughness)
# 递归调用
divide(half, roughness / 2, surface[:half+1, :half+1])
divide(half, roughness / 2, surface[:half+1, half:])
divide(half, roughness / 2, surface[half:, :half+1])
divide(half, roughness / 2, surface[half:, half:])
# 示例使用
size = 8
roughness = 1.0
surface = generate_rough_surface(size, roughness)
print(surface)
```
这段代码使用了递归的方法生成了一个指定大小的高斯粗糙表面。你可以根据需要调整`size`和`roughness`参数来控制表面的大小和粗糙程度。生成的表面将以二维数组的形式返回。
阅读全文
相关推荐


















