2013-07-22 39 views
5

最近,我一直試圖擊敗我的一個主要弱點在編程一般,隨機生成。我認爲這將是一件容易的事情,但缺乏簡單的信息會讓我失望。我不想聽起來很愚蠢,但我覺得像大多數this這些地方的信息都是寫給那些去大學讀理論數學的數學家的。我只是不明白我想用這些信息來做什麼,以便用python這樣的語言來編程。Python隨機地圖生成與佩林噪音

我一直在努力工作幾天,盯着方程式,試圖嘗試後嘗試嘗試,但仍然在所有那些日子之後,將我的代碼一遍又一遍地分離後,所有這一直在正常工作的是這個噪聲發生器,產生基本噪聲:

import random 
import math 

random.seed(0) 

def generateWhiteNoise(width,height): 
    noise = [[r for r in range(width)] for i in range(height)] 

    for i in range(0,height): 
     for j in range(0,width): 
      noise[i][j] = random.randint(0,1) 

    return noise 

noise = generateWhiteNoise(50,12) 

for i in noise: 
    print() 
    for o in i: 
     if(o == 0): 
      print('-',end='') 
     else: 
      print('#',end='') 

這段代碼產生這樣的結果:

##-######--#--#-#--##-###-###---#-##-#-----#--##-# 
#-#-##-##-#----##------##--#####-#-##---#--#-##--- 
-------#-#------#---#-#---###--#--#-###-----##-#-- 
######--#-#-#--####-###---#---###-##--#-#-##--#### 
-#----###--------##--##--##-#-#--#----###-####--## 
---####-#--#--###-#-#--#--#####--####-#-##-##--#-- 
----#--####-#-#-#-#-#---#--###------###--#-######- 
--###--#-###-------#-##--###---#-####----###-##### 
#----##--##-#--##-###--#----#-#-##--##-#-##---###- 
##---##----##--##--#--#--###-###-#--#-##---#------ 
-##----#-###---######---#-#---#---###---#---###-## 
#--##-##-###-###---#--##-##--##-##-#-#-##--#-#-##- 

我想它最終產生這樣的:

-------------------------------------------------- 
------------------####---------------------------- 
-----------------#####---------------------------- 
----------------#####----------------------------- 
---------------#####--------------###------------- 
---------------#####--------------###------------- 
---------------------------------####------------- 
---######------------------------####------------- 
---######------------###-------------------------- 
----########---------###-------------------------- 
-----#######---------###-------------------------- 
------###----------------------------------------- 

我該如何設法消除我產生的白噪聲,並將其變成島嶼?任何人都可以用一種非常簡單的方式來解釋它嗎?

我可能在想這一切都很錯。

+1

我會採取user1483482的建議。如果你想知道更多關於內部工作的信息,請嘗試在這裏尋找:http://devmag.org.za/2009/04/25/perlin-noise/ 我在python中複製了他的代碼,它可以工作,但它是**非常緩慢,即使是顛簸。 – seth

回答

5

只需使用Noise。好的編碼器代碼,很好的重用。

這裏是very basic example(其他可以在/ examples目錄中找到)。

+0

下面是這個問題,我正在尋找實際上產生噪音,因爲如果我要使用另一種沒有說圖書館的語言。 –

0

這篇文章(和其他人在同一個項目中)是一個很好的介紹編碼問題。 C++代碼。 https://code.google.com/p/fractalterraingeneration/wiki/Perlin_Noise

這是一篇關於使用單純形噪聲算法(在某些方面對原始Perlin噪聲算法進行改進)的論文。它包含示例Java代碼。 http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf

而且同一作者近日 http://staffwww.itn.liu.se/~stegu/simplexnoise/SimplexNoise.java

應該不會太難翻譯的概念引入Python使這個代碼公共領域,雖然Python的成語爲數據結構稍有不同。

1

而是使用細胞自動機。你找到here的算法創建類似的模式,你要看到:

. . . . . . . . . . . . . . . 
. . . . . # # . . . . . # . . 
. . . . # # # # . . . # # # . 
. . . . . # # # # . . # # # . 
. . . . . . # # # # # # # . . 
. . . . . . # # # # # # # . . 
. . . . # # # # # # # # # . . 
. . . # # # # # # # # # # . . 
. . # # # # # # . # . # # . . 
. . # # # # # . . # . . . . . 
. . . # # # # . . . # # # . . 
. . . # # # # . . . # # # # . 
. . # # # # . . . . . # # # . 
. . # # # # . . . . . # # . . 
. . . . . . . . . . . . . . . 
3

直接回答你的問題是:「不,你不能做你的要求」,第二個答案是「是的,你在想這一切都是錯誤的「。

原因是你正在產生完全隨機的噪音。你所要求的是相干噪音。它們是兩種完全不同的動物,你不能從隨機噪音中獲得連貫的噪音。因此我的答案。

要解釋爲什麼,你必須瞭解它,我從excellent libnoise documentation重複這個簡單的聲明:


相干噪聲

A型光滑僞隨機噪聲。

  • 在相同的輸入值傳遞將始終返回相同的輸出值:

    相干噪聲由一個相干噪聲功能,其中有三個重要的性質產生的。

  • 輸入值的微小變化會使輸出值發生小的變化。
  • 輸入值的大變化會在輸出值中產生隨機變化。

隨機噪聲不具有這些特性,因此是完全不適合你想要達到的目的。

我會建議學習Ken Perlin's latest (improved) reference implementation和他的SIGGRAPH 2002筆記。

如果你無法理解或實現這一點,那麼只需使用一個庫,如libnoise,這是一個非常好用的LGPL庫,最初使用的是C++,它也被移植到許多其他語言中。

0

這是一個有趣的小問題,你可以用這個排序算法的解決這個問題:

  1. 產生一個小的均勻噪聲
  2. 重新取樣,以更高的分辨率(給你一個平穩噪聲圖像)
  3. 應用門檻,獲得假/真陣列
  4. 地圖假/真爲「 - 」 /「#」

並且以位首席的ting格式化效果很好。演示:

import numpy as np 
np.set_printoptions(threshold=np.nan) 
from scipy.ndimage.interpolation import zoom 
arr = np.random.uniform(size=(4,4)) 
arr = zoom(arr, 8) 
arr = arr > 0.5 
arr = np.where(arr, '-', '#') 
arr = np.array_str(arr, max_line_width=500) 
print(arr) 

輸出:

[['-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '-' '-'] 
['-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '-' '-'] 
['-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '-' '-'] 
['-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '-'] 
['-' '-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-'] 
['-' '-' '-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#'] 
['-' '-' '-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#'] 
['-' '-' '-' '-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#'] 
['-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#'] 
['-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#'] 
['-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#'] 
['-' '-' '-' '-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#'] 
['-' '-' '-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#'] 
['-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#' '#'] 
['-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#' '#'] 
['#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#' '#'] 
['#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#' '#'] 
['#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#' '#'] 
['#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '-' '-' '#' '#' '#' '#' '#'] 
['#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '#' '#' '#'] 
['#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-'] 
['#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-'] 
['#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-'] 
['#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-'] 
['#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-'] 
['#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-'] 
['#' '#' '#' '#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-'] 
['#' '#' '#' '#' '#' '#' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-'] 
['#' '#' '#' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-'] 
['-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-'] 
['-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-'] 
['-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-']] 

當然,一個柏林或單純樣雜音表示將給予看上去稍微好一些其他回答者。如果你想嘗試,用Perlin/Simplex或其他任何可以抓取的噪音替換步驟1和2,然後重試。