0

我为一个类编写了一个程序,使用递归来模拟某些类型的简单分支结构,如树。在我向教授展示之前,我认为我的代码很棒。他告诉我的代码太复杂了,并说我需要简化它。除了将它们分开之外,我不确定我还能做什么。有小费吗?(我是初学者,所以请放轻松。)这个程序创建了多个具有不同厚度、分支数量和不同坐标的树。

import random 
import turtle
##I'm using a python module called turtle to visualize results
p1 = turtle.Pen()
##Creates a pen
p1.tracer(True)
## Shows pen drawing
p1.up()
p1.left(90)

d=random.randint(0,2)
## Varying thickness of branch
length=150
##Length of branches
contract=random.uniform(.5,1)
## Varying degree of contraction
branch=random.randint(5,8)
## Varying amount of branches
first=random.randint(30,70)
## Varying first degree of branch
next=random.randint(1,30)
## Varying degree between each branches
number1=random.randint(10,20)
number2=random.randint(-100,100)
number3=random.randint(-100,100)
# Range of numbers used for coordinates 
def drawFern1(pen, depth, length, contractBy, branches, firstBranchAngle, nextBranchAngle):
    if depth > 0:
       #Pen's Position and heading
       heading = pen.heading()
       position = pen.position()
       pen.width(depth)
       pen.forward(length)
       pen.left(firstBranchAngle)
       for i in range(branches):
        drawFern1(pen, depth-1, contractBy*length, contractBy,branches,firstBranchAngle,nextBranchAngle)
        pen.right(nextBranchAngle)
      pen.setheading(heading)
      pen.setposition(position)
# Ensures that multiple trees are created each at different coordinates. 
for i in range(number1):
   p1.sety(number2)
   p1.setx(number3)
   p1.down()
   drawFern1(p1,d,length,contract,branch,first,next)
   number2 = random.randint(-100,100)
   number3 = random.randint(-100,100)
   p1.up()
4

2 回答 2

0

这段代码对我来说看起来很可靠,尤其是对于 Python 初学者。我见过更糟糕的。

如果我正在编写它,我想我会计算number2并且number3只在主for循环内进行 - 你在这里拥有的启动定义通常对while循环很方便,但在这种情况下不是必需的。我还会尝试使用更多解释性变量名称,并且根据问题陈述,我可能要求随机生成的depth值至少为 1 - 如果depth生成为 0,则不会绘制任何内容。

我的版本如下所示:

import random 
import turtle

def drawFern(pen, depth, length, contraction, branches, firstBranchAngle, nextBranchAngle):
    if depth > 0:
        # Pen's Position and heading
        heading = pen.heading()
        position = pen.position()
        pen.width(depth)
        pen.forward(length)
        pen.left(firstBranchAngle)
        for i in xrange(branches):
            drawFern(pen, depth-1, contraction*length, contraction, branches, firstBranchAngle, nextBranchAngle)
            pen.right(nextBranchAngle)
        pen.setheading(heading)
        pen.setposition(position)

# I'm using a python module called turtle to visualize results
# Creates a pen
pen = turtle.Pen()
#  Shows pen drawing
pen.tracer(True)
pen.up()
pen.left(90)

# Configure initial state
# Varying depth of recursive fern
depth = random.randint(1,2)
# Length of branches
length = 150
# Varying degree of contraction
contraction = random.uniform(.5,1)
# Varying number of branches
branches = random.randint(5,8)
# Varying first degree of branch
first_angle = random.randint(30,70)
#  Varying degree between each branches
next_angle = random.randint(1,30)

number_of_trees =random.randint(10,20)

for i in xrange(number_of_trees):
    new_x = random.randint(-100, 100)
    new_y = random.randint(-100, 100)
    pen.setx(new_x)
    pen.sety(new_y)   
    pen.down()
    drawFern(pen, depth, length, contraction, branches, first_angle, next_angle)
    pen.up()

除了将 x 和 y 坐标随机化移动到主循环中,将递归函数定义移动到文件的前面,并使用一些更明确的变量名称,我使用xrange调用而不是range调用 - 如果你在使用,这是一个微不足道的优化Python 2.x。如果您使用的是 Python 3,range则正确。但这些都是微小的变化。

你也可以在循环if之前加入一个子句,range(branches)甚至不尝试 if depthequals 1 - 这是另一个小的优化,尽管不会产生很大的不同。

于 2013-10-08T09:32:50.810 回答
0

在我向教授展示之前,我认为我的代码很棒。他告诉我的代码太复杂了,并说我需要简化它。

考虑到它绘制的树的质量,这是相当复杂的代码:

在此处输入图像描述

仅绘制垂直线和空白屏幕在编写的程序的随机参数内!让我们重新编写程序,将一些随机性从静态配置代码转移到递归例程本身。我们还将微调随机范围并清理代码,主要是通过消除仅设置和使用一次的变量:

from random import randint, uniform
from turtle import Screen, Pen  # Using python turtle module to visualize results

# Configure initial state

DEPTH = randint(3, 4)  # Varying thickness and splitting of branches
LENGTH = randint(125, 150)  # Length of branches
CONTRACT_BY = uniform(0.4, 0.8)  # Varying degree of contraction

def drawFern(pen, depth, length, contractBy):
    if depth < 1:
        return

    # Save pen's position and heading
    heading = pen.heading()
    position = pen.position()

    pen.width(depth * 1.5)  # pen thickness depends on branching
    pen.forward(length)
    pen.left(randint(30, 70)) # Varying first degree of branch)

    for _ in range(randint(5, 8)):  # Varying amount of branches
        drawFern(pen, depth - 1, contractBy * length, contractBy)
        pen.right(randint(5, 30))  # Varying degree between each branches

    # Restore pen's Position and heading
    pen.setheading(heading)
    pen.setposition(position)

screen = Screen()

pen = Pen(visible=False)
pen.left(90)

screen.tracer(False)
# Ensure that multiple trees are created each at different coordinates.
for i in range(randint(10, 20)):
    pen.penup()
    pen.setposition(randint(-200, 200), randint(-300, 0))
    pen.pendown()
    drawFern(pen, DEPTH, LENGTH, CONTRACT_BY)
screen.tracer(True)

screen.mainloop()

在此处输入图像描述

于 2018-12-24T06:30:39.940 回答