2015-02-06 57 views
0

我已經通過蟒蛇中的龜創建了一片楓葉。我找到了一種方法來翻譯x和y軸上的形狀,但是我沒有找到一種方法來調整楓葉的大小,同時保持它的原始形式。如何使用x,y座標(烏龜圖形)重新生成圖像

import turtle 
def MapleLeaf(x=None,y=None): 
    if x==None: 
     x=0 
    if y==None: 
     y=0 
    turtle.penup() 
    turtle.goto(1+x,-3+y) 
    turtle.pendown() 
    turtle.goto(5+x,-4+y) 
    turtle.goto(4+x,-3+y) 
    turtle.goto(9+x,1+y) 
    turtle.goto(7+x,2+y) 
    turtle.goto(8+x,5+y) 
    turtle.goto(5+x,4+y) 
    turtle.goto(5+x,5+y) 
    turtle.goto(3+x,4+y) 
    turtle.goto(4+x,9+y) 
    turtle.goto(2+x,7+y) 
    turtle.goto(0+x,10+y) 
    turtle.goto(-2+x,7+y) 
    turtle.goto(-4+x,8+y) 
    turtle.goto(-3+x,3+y) 
    turtle.goto(-5+x,6+y) 
    turtle.goto(-5+x,4+y) 
    turtle.goto(-8+x,5+y) 
    turtle.goto(-7+x,2+y) 
    turtle.goto(-9+x,1+y) 
    turtle.goto(-4+x,-3+y) 
    turtle.goto(-5+x,-4+y) 
    turtle.goto(0+x,-3+y) 
    turtle.goto(2+x,-7+y) 
    turtle.goto(2+x,-6+y) 
    turtle.goto(1+x,-3+y) 
    turtle.hideturtle() 

turtle.pencolor("black") 
turtle.fillcolor("red") 
turtle.begin_fill() 
MapleLeaf(50,50) 
turtle.end_fill() 
turtle.done() 

回答

1

爲了改變你正在繪製圖的規模,縮放係數乘以所有偏移從您xy的位置:

def MapleLeaf(x=0, y=0, scale=1): 
    turtle.penup() 
    turtle.goto(1*scale+x,-3*scale+y) 
    turtle.pendown() 
    turtle.goto(5*scale+x,-4*scale+y) 
    turtle.goto(4*scale+x,-3*scale+y) 
    turtle.goto(9*scale+x,1*scale+y) 
    # ... 

注意,我擺脫因爲您可以使用0作爲默認值,因此在開始時您的語句爲if。它的唯一可變默認值(如列表),你需要總是使用像None哨兵信號的默認值是想要的。

由於規模和xy偏移重複了這麼多,你可能要進一步將它們剔除成一個函數:

def MapleLeaf(x=0, y=0, scale=1): 
    def my_goto(x_offset, y_offset): 
     turtle.goto(x + scale*x_offset, y + scale*y_offset) 

    turtle.penup() 
    my_goto(1, -3) 
    turtle.pendown() 
    my_goto(5, -4) 
    my_goto(4, -3) 
    my_goto(9, 1) 
    # ... 

另一種方法是把偏移到一個列表,並反覆測試在他們的循環中。