CWYAlpha

Just another WordPress.com site

Thought this was cool: Python turtle!

leave a comment »


I’ve recently started learning Python and was surprised and delighted to find that it has an implementation of the old Turtle program that I used to play around with back in primary. For some reason it even runs as slow as the original! (intentional?). Here is a simple geometric flower pattern I whipped up that I thought I’d share.

Seems to be lacking anti-aliasing support …

Here is the Python code.

import turtle
import math
 
turtle.speed('fastest')
 
def Petal():
    step = 4 # the larger the faster, but coarser
    radius = 100 # circle radius
    petal_arc = 0.25; # percent of full circle
 
    sides = 2*math.pi*radius/step
    turn = 360 / sides
 
    for i in range(2):
        for x in range(int(sides*petal_arc)):
            turtle.forward(step)
            turtle.right(turn)
 
        turtle.right(180 - 360*petal_arc)
 
num_petals = 8
 
for i in range(num_petals):
    turtle.home()
    turtle.setheading(0)
    turtle.right(360*i/num_petals)
    Petal()
 
print "Press any key to exit ..."
raw_input()

from Nghia Ho: http://nghiaho.com/?p=1603

Written by cwyalpha

八月 25, 2012 在 1:25 上午

发表在 Uncategorized

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: