Tia*_*art 10 python recursion pygame fractals
因此,对于我目前的大学论文,我们的目的是创建一个Sierpinksi三角形,并在内部递归绘制新的三角形.
我们得到的原始代码是这样的:
import sys, pygame
# a function that will draw a right-angled triangle of a given size anchored at a given location
def draw_triangle(screen, x, y, size):
pygame.draw.polygon(screen,white,[[x,y], [x+size,y], [x,y-size]])
#############################################################################################
# Define a function that will draw Sierpinski's Triangle at a given size anchored at a given location
# You need to update this function
# currently only one triangle is drawn
def sierpinski(screen, x, y, size):
draw_triangle(screen, x, y, size)
#############################################################################################
# Initialize the game engine
pygame.init()
# Define the colors we will use in RGB format
black = [ 0, 0, 0]
white = [255,255,255]
blue = [ 0, 0,255]
green = [ 0,255, 0]
red = [255, 0, 0]
# Set the height and width of the screen
size=[512, 512]
screen=pygame.display.set_mode(size)
# Loop until the user clicks the close button.
done=False
clock = pygame.time.Clock()
while done==False:
# This limits the while loop to a max of 10 times per second.
# Leave this out and we will use all CPU we can.
clock.tick(10)
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
# Clear the screen and set the screen background
screen.fill(black)
# Draw Sierpinski's triangle at a given size anchored at a given location
sierpinski(screen,0, 512, 512)
# Go ahead and update the screen with what we've drawn.
# This MUST happen after all the other drawing commands.
pygame.display.flip()
# Tidy up
pygame.quit ()
Run Code Online (Sandbox Code Playgroud)
好的,我知道这只会创建一个三角形.以下是我所做的工作,使其"有点"工作:
我创建了一个新的三角形函数来绘制一个倒三角形:
def draw_upside_down_triangle(screen, x, y, size, color):
pygame.draw.polygon(screen, color, [[x+size, y+size], [x+size, y], [x, y]])
Run Code Online (Sandbox Code Playgroud)
然后我更新旧三角函数以接受颜色变量:
def draw_triangle(screen, x, y, size, color):
pygame.draw.polygon(screen, color, [[x, y], [x+size, y], [x, y-size]])
Run Code Online (Sandbox Code Playgroud)
之后我更新了主函数,它将以递归方式绘制三角形:
def sierpinski(screen, x, y, size):
if size < 10:
return False
else:
draw_triangle(screen, x, y, size, white)
draw_upside_down_triangle(screen, x, y/2, size/2, black)
sierpinski(screen, x+size/2, y+size/2, size/2)
sierpinski(screen, x, y-size/2, size/2)
sierpinski(screen, x, y, size/2)
sierpinski(screen, x, y+size/2, size/2)
Run Code Online (Sandbox Code Playgroud)
我开始关闭这个功能
目前的输出如下:
我并不是要求任何人完成或更正我的代码只是为了更好地理解或指向正确的方向.几个小时一直在和这个人争斗.
谢谢!
看看下面实现谢尔宾斯基三角形的链接......
http://interactivepython.org/runestone/static/pythonds/Recursion/graphical.html#sierpinski-triangle
围绕这个问题进行了很多很好的讨论,并有 40 行代码来实现它。
另外,由于海龟模块的工作方式,您可以看到每个三角形都被一一绘制。当您查看代码时,这非常有帮助,因为您可以直观地看到递归级别及其发生时间。我不知道这在 pygame 中实现起来有多难,但如果你可以减慢三角形的创建速度,那么理解逻辑就会容易得多。
你说你需要基于实验的 4 次递归调用,但你能解释一下它背后的逻辑吗?直观上这似乎是错误的,因为您只需要三个新三角形加上一个部分覆盖的父三角形就等于四个较小的等边三角形。(看看链接中这是如何完成的?)
您能解释一下为什么使用倒三角形方法吗?这看起来有点像一个容易出现错误的解决方法?您应该能够使用正常三角形函数中的负空间绘制倒三角形。在链接中,您会看到作者绘制了一个与其他所有三角形面向相同方向的绿色三角形,但随后用更多三角形覆盖它,直到绿色三角形面向相反方向。
总而言之,看起来你们很接近。您只需要正确处理最后一段递归逻辑即可。
聚苯乙烯
一点小小的风格批评 - 只是因为这是用 python 编写的并且可读性很重要。您可以使用While Trueand thenbreak来避免额外的变量done。
| 归档时间: |
|
| 查看次数: |
1698 次 |
| 最近记录: |