

源代码如下 (python 2 使用Turtle绘图):
# coding=utf-8 import turtle PROGNAME = 'Sierpinski Carpet' COLOR_TABLES = [ ["#000000", "#000000", "#000000", "#000000" ], ["#00649F", "#97ECC5", "#00DBE7", "#01AAC1" ], ["#085F63", "#EF255F", "#FCCF4D", "#49BEB7" ], ["#9B5D73", "#FFF1C5", "#C38B8B", "#B0757C" ], ["#F0F0F0", "#FC5185", "#364F6B", "#43DDE6" ], ["#F67280", "#355C7D", "#6C5B7B", "#C06C84" ], ["#F3ECC8", "#0D1B4C", "#3F6699", "#78C2C3" ] ] def box(pen, boxSize): pen.begin_fill() # 0 deg. pen.forward(boxSize) pen.left(90) # 90 deg. pen.forward(boxSize) pen.left(90) # 180 deg. pen.forward(boxSize) pen.left(90) # 270 deg. pen.forward(boxSize) pen.end_fill() pen.setheading(0) def move_box(pen, xy, length, color): (x, y) = xy pen.color(color) pen.penup() pen.goto(x, y) pen.pendown() box(pen, length) def draw_all(pen, xy, length, depth, color_table): if depth <= 0: return length3 = length / 3 (x, y) = xy pos1_9 = [ (x-length3*2, y+length+length3), (x+length3, y+length+length3), (x+length+length3, y+length+length3), (x-length3*2, y+length3), (x+length+length3, y+length3), (x-length3*2, y-length3*2), (x+length3, y-length3*2), (x+length+length3, y-length3*2) ] for pos in pos1_9: move_box(pen, pos, length3, color_table[depth%len(color_table)]) draw_all(pen, pos, length3, depth-1, color_table) def main(): length = 270 # size start_x = -length/2 start_y = -length/2 depth = 3 turtle.setup(width=1.3*turtle.window_width(), height=1.3*turtle.window_height()) # not necessary for color_table in COLOR_TABLES: my_pen = turtle.Turtle() my_pen.ht() # hiden turtle my_pen.speed(20000) # set speed move_box(my_pen, (start_x, start_y), length, color_table[0]) draw_all(my_pen, (start_x, start_y), length, depth, color_table) my_pen.getscreen().getcanvas().postscript(file=color_table[0]+".eps") # save to eps file my_pen.clear() # clear for next draw if __name__ == "__main__": main() pass






(↓ - 有些内容只在小龙家发,可关注同名“趣Python”号,谢谢 - ↓)


发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/226611.html原文链接:https://javaforall.net
