标签:tle for main blog ide log 做了 pre 移动
空调一如既往的吵,好吵
今天在做turtle的模块的联系,做了一个多边形的函数,虽说没任何含金量,但觉得还是重在要分享记录吧。
一个多边形的函数
1 import turtle 2 3 def square(t, length): 4 for i in range(4): 5 t.fd(length) 6 t.lt(90) 7 8 def polygon(turtle, length, numOfSide): 9 degree = 360 / numOfSide # 根据几边形决定每次转动的角度 10 for i in range(numOfSide): 11 turtle.fd(length) 12 turtle.lt(degree) 13 14 bob = turtle.Turtle() 15 polygon(turtle = bob, numOfSide = 9, length = 100) 16 turtle.mainloop()
一个不是很准确的圆
1 import turtle 2 3 def square(t, length): 4 for i in range(4): 5 t.fd(length) 6 t.lt(90) 7 8 def polygon(turtle, length, numOfSide): 9 degree = 360 / numOfSide # 根据几边形决定每次转动的角度 10 for i in range(numOfSide): 11 turtle.fd(length) 12 turtle.lt(degree) 13 14 15 def circle(turtle, radius): 16 ‘‘‘ 17 由于不能精确算出一个半径对应几边形,但是一个基本的逻辑是半径越大,边数也要越大 18 还有一个问题就是,我也无法精确算出每一步到底改走多远,一般来说半径越大,边数越大,移动举例要越小 19 ‘‘‘ 20 numOfSide = radius 21 length = 1000 / numOfSide 22 polygon(turtle, length, numOfSide) 23 24 25 26 bob = turtle.Turtle() 27 # polygon(turtle = bob, numOfSide = 9, length = 100) 28 circle(bob, 200) 29 turtle.mainloop()
标签:tle for main blog ide log 做了 pre 移动
原文地址:http://www.cnblogs.com/onhacker/p/7011373.html