In [ ]:
from swampy.TurtleWorld import *
def polygon(t, n, length):
angle = 360.0 / n
for i in range(n):
fd(t, length)
lt(t, angle)
if __name__ == '__main__':
world = TurtleWorld()
bob = Turtle()
bob.delay = 0.01
polygon(bob, n=5, length=50)
wait_for_user()
Let's modify this so that we pass in the arguments to polygon from the command line. To do this we will use the argv function from the sys module:
The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string.
https://docs.python.org/2/library/sys.html#sys.argv
What we want to be able to do is call
$ ./draw_polygon 5 50
and this will pass in 5 as the number of sides of the polygon and 50 the length of each side. (Note that in this case I have used the hashbang #!/usr/bin/env python and chmod u+x.)
In [ ]:
#!/usr/bin/env python
import sys
from swampy.TurtleWorld import *
def polygon(t, n, length):
angle = 360.0 / n
for i in range(n):
fd(t, length)
lt(t, angle)
if __name__ == '__main__':
world = TurtleWorld()
bob = Turtle()
bob.delay = 0.01
if len(sys.argv) > 1:
n = int(sys.argv[1])
l = int(sys.argv[2])
polygon(bob, n=5, length=50)
wait_for_user()
In [41]:
def polyline(t, n, length, angle):
"""
Draws a n-connected lines.
Params:
t (Turtle): turtle object to draw the lines
n (int): number of lines to draw
length (float): length of each line
angle (float): angle between successive lines
Returns:
None
"""
for i in range(n):
fd(t, length)
lt(t, angle)
def arc(t, r, angle):
"""
Draws the arc of a circle.
Params:
t (Turtle): turtle object to draw the arc
r (float): radius of the arc
angle (float): angle subtended by the arc, in degrees
Returns:
None
"""
arc_length = 2 * math.pi * r * angle / 360
n = int(arc_length / 3) + 1 # QUESTION: why do we need int here?
step_length = arc_length / n
step_angle = float(angle) / n # QUESTION: why do we need float here?
polyline(t, n, step_length, step_angle)
def circle(t, r):
"""
Draws a circle.
Params:
t (Turtle): turtle object to draw the circle
r (float): radius of the circle
Returns:
None
"""
arc(t, r, 360)
In [ ]:
def petal(t, r, angle):
"""
Draws a single flower petal.
Params:
t (Turtle): turtle object to draw the petal
r (float): radius of the arcs that constitute the petal
angle (float): the angle between the petals
Returns:
None
"""
for i in range(2):
arc(t, r, angle)
lt(t, 180 - angle)
def flower(t, n, r, angle):
"""
Draws a single flower petal.
Params:
t (Turtle): turtle object to draw the petal
r (float): radius of the arcs that constitute the petal
angle (float): the angle between the petals
Returns:
None
"""
for i in range(n):
petal(t, r, angle)
lt(t, 360.0/n)
In [ ]:
world = TurtleWorld()
bob = Turtle()
bob.delay = 0.01
flower(bob, n=10, r=400, angle=25)
wait_for_user()
See http://thinkpython.com/code/pie.py for solution.
In [ ]: