任何人都可以建议任何链接,想法或算法随机生成花朵像我的个人资料图片?配置文件pic花只有10 x 10网格,算法不是真正随机的.我还希望新算法使用大约500 x 500甚至更好的网格,允许用户选择网格的大小.
[Plant [] []声明为int plant [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;
}
Run Code Online (Sandbox Code Playgroud)
这听起来像一个相当简单的问题,你只需要一次生成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)
Run Code Online (Sandbox Code Playgroud)
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]
Run Code Online (Sandbox Code Playgroud)
您还可以使用弧来简化操作,一次超过1个像素.
顶端
centerRadius = random(stemHeight)
petalCount = random() // probably >= 3
petalSize = random(centerRadius, petalCount)
Run Code Online (Sandbox Code Playgroud)
生成花瓣并不太容易,你需要步长从0到2*PI,2*PI/petalCount并在圆周周围产生弧线.它需要一个好的图形API或一些不错的数学.
这里有一些很好的花朵顶部,虽然看似不是开源的.请注意,他们根本没有中心.(或centerRadius = 0)
叶子
你可以写一篇关于这个的全文,(就像这一篇)但是一个简单的想法就是生成一个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
Run Code Online (Sandbox Code Playgroud)
最后的话
确定每个边界的random方法可以是争论它,或者给它一些固定值,随机生成其他所有东西几次,继续增加/减少它直到它看起来很奇怪.
10 x 10对500 x 500可能需要大大不同的算法,我不推荐上面的100 x 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);
Run Code Online (Sandbox Code Playgroud)