As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.
$ cd ~/cs120/labs $ mkdir lab10 $ cd lab10
Create the function draw_triangle(pen)
that draws a
triangle. The parameter pen
is a turtle object to use
to draw the triangle. The triangle can be any orientation and can
be any size.
gertrude = turtle.Turtle() draw_triangle(gertrude) gertrude.up() gertrude.forward(50) gertrude.down() draw_triangle(gertrude)
Create the function draw_circle(pen, x_loc, y_loc,
radius)
that draws a circle. The parameter pen
is a turtle object which you can use to draw the circle. Note that
the turtle may be located anywhere and have any heading. The
parameters x_loc
and y_loc
are the center
of the circle. The parameter radius
is the radius of
the circle.
gertrude = turtle.Turtle() num_circles = 3 size = 50 y = 0 for i in range(num_circles): x = size * i draw_circle(gertrude, x, y, size)
num_circles = 100 size = 100 RADIANS_IN_CIRCLE = 2.0 * math.pi for i in range(num_circles + 1): x = size * math.cos((i / num_circles) * RADIANS_IN_CIRCLE) y = size * math.sin((i / num_circles)* RADIANS_IN_CIRCLE) - (size * 2.0) draw_circle(gertrude, x, y, size)
One of the benefits of functions is to abstract complex processes into simple to call bits of code. This not only reduces the amount of code we have to write, but can also make our programs a lot easier to read.
In a file called polar_conversion.py, write a function
called goto_polar(pen, angle, magnitude)
. This
function should accept a turtle object, a floating point value
representing a range, and an integer representing a distance from
(0, 0). Your program should convert the passed parameters from
their polar coordinate representation into a typical cartesian
coordinate system, and move the specified turtle (pen) to
that location on the turtle window.
Make sure to test your code several times, and make sure your code follows the courses code conventions.
goto_polar(myrtle, math.pi / 4, 100)
goto_polar(myrtle, -3 * math.pi / 4, 100)
Right now, subsequent calls to goto_polar
will always
move the turtle to another location relative to the origin of the
screen. Alter the function so that the magnitude and angle
specified to goto_polar
are relative to the current
position of the turtle. Look at the position
method
of the turtle object for help getting the current position of the
turtle.
Another benefit of functions is to save on repeated code. There are some things we do A LOT in our programs, and many things that we do across many programs. One of these is drawing a filled square, which you will implement today.
Download and open in Emacs the Python program
called pixel_art.py. In
this file, there is an empty function definition
fill_square(pen, color, x_loc, y_loc, size)
. This
function should accept a turtle object, a string representing a
turtle color, integers x_loc and y_loc representing a location on
the turtle window, and an integer > 0 which is the size of one of
the sides of the square. Your function should draw a filled square
of the correct dimensions centered at the specified
(x_loc, y_loc).
Run your program and make sure you get the correct output. Also, make sure your code follows the courses code conventions.
$ python3 pixel_art.py
color
, begin_fill
and end_fill
functions for setting the turtles
color, as well as causing the shape to get filled in with
color afterwards.
This function allows you to draw an arbitrary filled in block at some location on the turtle window. "Old-school" computer graphics relied on this type of art to create visible shapes on the screen. This is known as pixel art, and there are TONS of examples around the Internet. Either copy your favorite, or create your own!