python - OpenSimplex 问题

标签 python algorithm

我正在关注 Amit 的精彩教程: http://www.redblobgames.com/maps/terrain-from-noise/#demo

这段代码:

from opensimplex import OpenSimplex

def simplex_noise(size):
    def noise(nx, ny):
        gen = OpenSimplex()
        # Rescale from -1.0:+1.0 to 0.0:1.0
        return gen.noise2d(nx, ny) / 2.0 + 0.5 

    value = np.zeros((size,size),dtype=np.float16)

    for y in range(size):
        for x in range(size):
            nx = x/size - 0.5
            ny = y/size - 0.5
            value[y,x] = noise(nx, ny) #(dont ask why flips x-y)


    return value.reshape(size * size).tolist();

向我呈现这个(以 3d 形式制作):

enter image description here

而不是像这样的更随机噪音的东西(在 2D 中,来自 Amit 的网站):

enter image description here

我已经用 Diamond Square 算法做了这个,但想试试这个库,但很奇怪。输出介于 0 和 1 之间。

我迷路了!谢谢!

最佳答案

你的代码对我来说有点吵。是值(value)矩阵的 reshape 数组!? 为什么 ?试试这个例子得到相同的结果,也许这对你有帮助。

from opensimplex import OpenSimplex
from PIL import Image

height = int(input("Enter in the map height: "))
width = int(input("Enter in the map width: "))

def main():
    simplex = OpenSimplex()
    im = Image.new('L', (width, height))
    for y in range(0, height):
        for x in range(0, width):
            value = simplex.noise2d(x , y )
            color = int((value + 1) * 128)
            im.putpixel((x, y), color)
    im.save('noise2d_output.png')
    im.show()
if __name__ == '__main__':
    main()

关于python - OpenSimplex 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40894473/

相关文章:

python - 有没有一种简单的方法可以从某个点开始拆分字符串?

python - 从字符串中剪切多类型符号

python - 寻找最小差异

c++ - std::transform 中未解析的重载函数类型

string - 使用动态编程查找字符串中最长的单词?

algorithm - 如何随机生成一条窄路径?

java - 如何检查曲线是否相似

javascript - 如何根据 dom Node 列表创建包含 json 对象数组的文件

python - Windows 错误(3, 'The system cannot find the path specified' )

php - 高效排名算法(PHP)