python-L系统森林中的重叠树
作者:互联网
我使用python的turtle图形创建了一个程序,该程序模拟了森林中树木的生长.有3种随机选择的树模式,它们的起始坐标和角度也是随机选择的.我选择了一些看起来很酷的树图案,但是我遇到的问题是许多树都重叠了,所以看起来像是一棵坏的5岁孩子的画,而不是看起来像树木的树林.
有没有办法使这种重叠不太常见?当您看森林时,有些树木和它们的叶子确实重叠了,但是绝对不是这样的:
由于涉及大量随机化,因此我不确定如何处理.
这是我的代码:
import turtle
import random
stack = []
#max_it = maximum iterations, word = starting axiom such as 'F', proc_rules are the rules that
#change the elements of word if it's key is found in dictionary notation, x and y are the
#coordinates, and turn is the starting angle
def createWord(max_it, word, proc_rules, x, y, turn):
turtle.up()
turtle.home()
turtle.goto(x, y)
turtle.right(turn)
turtle.down()
t = 0
while t < max_it:
word = rewrite(word, proc_rules)
drawit(word, 5, 20)
t = t+1
def rewrite(word, proc_rules):
#rewrite changes the word at each iteration depending on proc_rules
wordList = list(word)
for i in range(len(wordList)):
curChar = wordList[i]
if curChar in proc_rules:
wordList[i] = proc_rules[curChar]
return "".join(wordList)
def drawit(newWord, d, angle):
#drawit 'draws' the words
newWordLs = list(newWord)
for i in range(len(newWordLs)):
cur_Char = newWordLs[i]
if cur_Char == 'F':
turtle.forward(d)
elif cur_Char == '+':
turtle.right(angle)
elif cur_Char == '-':
turtle.left(angle)
elif cur_Char == '[':
state_push()
elif cur_Char == ']':
state_pop()
def state_push():
global stack
stack.append((turtle.position(), turtle.heading()))
def state_pop():
global stack
position, heading = stack.pop()
turtle.up()
turtle.goto(position)
turtle.setheading(heading)
turtle.down()
def randomStart():
#x can be anywhere from -300 to 300, all across the canvas
x = random.randint(-300, 300)
#these are trees, so we need to constrain the 'root' of each
# to a fairly narrow range from -320 to -280
y = random.randint(-320, -280)
#heading (the angle of the 'stalk') will be constrained
#from -80 to -100 (10 degrees either side of straight up)
heading = random.randint(-100, -80)
return ((x, y), heading)
def main():
#define the list for rule sets.
#each set is iteration range [i_range], the axiom and the rule for making a tree.
#the randomizer will select one of these for building.
rule_sets = []
rule_sets.append(((3, 5), 'F', {'F':'F[+F][-F]F'}))
rule_sets.append(((4, 6), 'B', {'B':'F[-B][+ B]', 'F':'FF'}))
rule_sets.append(((2, 4), 'F', {'F':'FF+[+F-F-F]-[-F+F+F]'}))
#define the number of trees to build
tree_count = 50
#speed up the turtle
turtle.tracer(10, 0)
#for each tree...
for x in range(tree_count):
#pick a random number between 0 and the length
#of the rule set -1 - this results in selecting
#a result randomly from the list of possible rules.
rand_i = random.randint(0, len(rule_sets) - 1)
selected_ruleset = rule_sets[rand_i]
#unpack the tuple stored for this ruleset
i_range, word, rule = selected_ruleset
#pick a random number inside the given iteration_range to be the
#iteration length for this command list.
low, high = i_range
i = random.randint(low, high)
#get a random starting location and heading for the tree
start_position, start_heading = randomStart()
#unpack the x & y coordinates from the position
start_x, start_y = start_position
#build the current tree
createWord(i, word, rule, start_x, start_y, start_heading)
if __name__ == '__main__': main()
解决方法:
我认为问题更多在于树木本身的特征规律性,而不是其本身的位置.
一种可能的解决方案是添加突变.对于“突然增长”的全球控制,您可以抑制5%的生产应用程序.这样可以使稀疏树更宽松地遵循模型.
为了更好地控制,您可以用不同的重量抑制每个生产.
查看The Algorithmic Beauty of Plants的第1.7节“随机L系统”以了解更多信息.他们使用概率在单个规则的多个变体中进行选择.
标签:turtle-graphics,l-systems,python 来源: https://codeday.me/bug/20191102/1989241.html