2013-01-02 43 views
5

任何人都可以建議任何鏈接,想法或算法來隨機生成花朵作爲我的個人資料圖片嗎?個人資料圖片花只有一個10×10的網格,算法不是真正的隨機。我還希望新算法使用約500 x 500或更好的網格,允許用戶選擇網格的大小。算法生成隨機花的想法

[廠[] []聲明爲INT廠[10] [10]]

public void generateSimpleSky(){ 

    for(int w2=0;w2<10;w2++) 
     for(int w3=0;w3<10;w3++) 
      plant[w2][w3]=5; 

} 

public void generateSimpleSoil(){ 

    for(int q=0;q<10;q++) 
     plant[q][9]=1; 

} 

public void generateSimpleStem(){ 

    int ry=rand.nextInt(4); 
    plant[3+ry][8]=4; 
    xr=3+ry; 

    for(int u=7;u>1;u--){ 

     int yu=rand.nextInt(3); 
     plant[xr-1+yu][u]=4; 
     xr=xr-1+yu; 

    } 

} 

public void generateSimpleFlower(){ 

    plant[xr][2]=3; 

    for(int q2=1;q2<4;q2++) 
     if((2-q2)!=0) 
      plant[xr][q2]=2; 

    for(int q3=xr-1;q3<=xr+1;q3++) 
     if((xr-q3)!=0) 
      plant[q3][2]=2; 

} 
+2

你說的*真正隨機的*是什麼意思? – jeremy

+4

我第二@Nile的評論。你可能首先要分離出你想要隨機的花。顏色,花瓣大小,核心大小/顏色,莖幹大小等。該花的真正隨機化將會使真實的雜亂植物變得困難。 – brainmurphy1

+0

我的意思是,我用來生成在我的個人資料PIC花算法使用的算法比僅改變了幹ooked的方式。花仍然看起來一樣。所以我想讓它成爲「真正的randam」,即使enteire程序是隨機的。 – Hele

回答

4

這聽起來像一個相當簡單的問題,你只是在一個時間產生1個參數,可能是基於前面變量的輸出。

我的一朵花的模型將是:它只有一個合理挺直的莖,一個完美的圓形中心,在莖的交替兩側有一定數量的葉子,花瓣完美分佈在中心周圍。

random()只是一些選定邊界內的一個隨機數,邊界對於每個變量可能是唯一的。 random(x1, x2, ..., xn)根據變量x1,x2,...,xn(如在stemWidth < stemHeight/2,一個合理的假設)在一些範圍內生成一個隨機數。

stemXPosition = width/2 
stemHeight = random() 
stemWidth = random(stemHeight) 
stemColour = randomColour() 
stemWidthVariationMax = random(stemWidth, stemHeight) 
stemWidthVariationPerPixel = random(stemWidth, stemHeight) 

stemWidthVariationMax/-PerPixel是產生一個幹,是不是完全筆直的(如果你想要做一些複雜的,低PerPixel是平滑度)。生成使用這些如下幹:

pixelRelative[y-position][0] := left x-position at that y-position relative to the stem 
pixelRelative[y-position][1] := right x-position at that y-position relative to the stem 

pixelRelative[0][0] = randomInRange(-stemWidthVariationMax, stemWidthVariationMax) 
for each y > 0: 
    pixelRelative[y-1][0] = max(min(randomInRange(pixel[y] - stemWidthVariationPerPixel, 
             pixel[y] + stemWidthVariationPerPixel), 
          -stemWidthVariationMax), 
         stemWidthVariationMax) 
//pixelRelative[0][1] and pixelRelative[y-1][1] generated same as pixelRelative[y-1][i] 
for each y: 
    pixelAbsolute[y][0] = width/2 - stemWidth/2 + pixelRelative[y][0] 
    pixelAbsolute[y][1] = width/2 + stemWidth/2 + pixelRelative[y][1] 

您還可以使用弧線把事情簡單化,並在同一時間去超過1個像素。

頂部

centerRadius = random(stemHeight) 
petalCount = random() // probably >= 3 
petalSize = random(centerRadius, petalCount) 

這不是太容易產生花瓣,你需要從0到2 * PI步驟與2*PI/petalCount步長和產生電弧繞了一圈。它需要一個良好的圖形API或一些體面的數學。

Here's一些很好地生成了花的頂端,雖然看起來不是開源的。請注意,他們根本沒有中心。 (或centerRadius = 0)

葉子

你也許可以寫這類整個紙,(像this one),但一個簡單的想法也只是以生成1/2圈和向外延伸線從那裏以2 *圓的半徑相遇並在花上繪製平行線。

一旦你有一個葉生成算法:

leafSize = random(stemHeight) // either all leaves are the same size or generate the size for each randomly 
leafStemLength = random(leafSize) // either all leaves have the same stem length or generate for each randomly 
leafStemWidth = random(leafStemLength) 
leaf[0].YPosition = random(stemHeight) 
leaf[0].XSide = randomly either left or right 
leaf[0].rotation = random between say 0 and 80 degrees 
for each leaf i: 
    leaf[i].YPosition = random(stemHeight, leaf[i-1]) // only generate new leaves above previous leaves 
    leaf[i].XSide = opposite of leaf[i].XSide 

遺言

,以確定每個random的界限將是要麼認爲它,或給它一些固定值的方法,隨機生成其他所有東西幾次,不斷增加/減少它,直到它開始看起來很奇怪。

10×10與500×500很可能需要大大不同的算法,我不會推薦以上低於100×100,可能產生一個更大的圖像和簡單地使用平均或東西它收縮。

代碼

我開始寫一些Java代碼,當我意識到這可能需要更長的時間比我想在此度過,所以我會告訴你什麼是我到目前爲止所。

// some other code, including these functions to generate random numbers: 
    float nextFloat(float rangeStart, float rangeEnd); 
    int nextInt(int rangeStart, int rangeEnd); 

    ... 

    // generates a color somewhere between green and brown 
    Color stemColor = Color.getHSBColor(nextFloat(0.1, 0.2), nextFloat(0.5, 1), nextFloat(0.2, 0.8)); 
    int stemHeight = nextInt(height/2, 3*height/4); 
    int stemWidth = nextInt(height/20, height/20 + height/5); 
    Color flowerColor = ??? // I just couldn't use the same method as above to generate bright colors, but I'm sure it's not too difficult 
    int flowerRadius = nextInt(Math.min(stemHeight, height - stemHeight)/4, 3*Math.min(stemHeight, height - stemHeight)/4); 
+0

簡單地說,你真棒! – Hele