简体   繁体   中英

Python Zelle Graphics - Recursive functions in drawing?

目标

在此处输入图片说明

import graphics
def main():
    window = graphics.GraphWin("x", 600, 400)
    cntr = graphics.Point(300,200)
    size = 200
    wrapdiamond(size, window,cntr)
    window.getMouse()
def wrapdiamond(size, window,cntr):
    count = 0
    if count == 4:
        return
    if count < 4:
        diamond(size,window,"black", cntr)
        x= cntr.getX()+-0.5*size
        y = cntr.getY()+-0.5*size
        cntr= graphics.Point(x,y)
        size = size*0.33
        count +=1
        diamond(size,window,"black", cntr)

def diamond(size,window,color,cntr):
    p1 = cntr.clone()
    p1.move(0,-0.5*size)
    x1 = p1.getY()
    newcntr = graphics.Point(300,x1)
    p2 = cntr.clone()
    p2.move(-0.5*size,0)
    p3 = cntr.clone()
    p3.move(0,0.5*size)
    p4 = cntr.clone()
    p4.move(0.5*size,0)
    diamond= graphics.Polygon(p1, p2, p3, p4)
    diamond.setFill("black")
    diamond.draw(window)

So the top picture is my goal. I need to do this recursively (calling the same function), shifting the center point, size, and color. I feel like my current method is very likely to have me hardcoding much of this. How would I implement this recursively?

It's been a couple of years so I thought it's about time this had a solution:

import graphics

def wrapdiamond(size, window, outline_color, fill_color, center, depth=1):

    if depth <= 0:
        return

    diamond(size, window, outline_color, fill_color, center)

    if depth <= 1:  # avoid going through the motions...
        return

    new_size = size * 0.33

    for x in (-1.0, 1.0):
        for y in (-1.0, 1.0):
            new_center = center.clone()
            new_center.move(x * size/2, y * size/2)
            # swap fill and outline colors on recursion
            wrapdiamond(new_size, window, fill_color, outline_color, new_center, depth - 1)

def diamond(size, window, outline_color, fill_color, center):
    p1 = center.clone()
    p1.move(0, -0.5 * size)
    p2 = center.clone()
    p2.move(-0.5 * size, 0)
    p3 = center.clone()
    p3.move(0, 0.5 * size)
    p4 = center.clone()
    p4.move(0.5 * size, 0)

    diamond = graphics.Polygon(p1, p2, p3, p4)
    diamond.setOutline(outline_color)
    diamond.setFill(fill_color)
    diamond.draw(window)

my_window = graphics.GraphWin("Recursive Diamonds", 900, 600)
my_center = graphics.Point(450, 300)

wrapdiamond(250, my_window, "gold", "black", my_center, 4)

my_window.getMouse()
my_window.close()

wrapdiamond() is the recursive function which calls itself in a loop with a smaller size and readjusted centers. It also swaps the outline and fill colors to make an alternating pattern:

在此处输入图片说明

If you're stuck with recursion, this could get you started again

def rdraw(x,y,level=1,max_level=5):
    draw_diamond(level)
    d = distance(level)
    if (level+1)<=max_level:
        rdraw(x+d,y+d,level+1)
        rdraw(x+y,y-d,level+1)
        rdraw(x-y,y+d,level+1)
        rdraw(x-y,y-d,level+1)

rdraw(0,0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM