Difference between revisions of "CS110:Lab09"

From IPRE Wiki
Jump to: navigation, search
('''Musical Scales''')
(Links)
 
(47 intermediate revisions by 3 users not shown)
Line 1: Line 1:
='''Lab 09: Exploring Music''' =
+
='''Lab 09: Objects in Graphics''' =
 +
 
  
 
=='''Objective'''==
 
=='''Objective'''==
* Learn some fundamentals of sound and music
+
* Use various Myro commands to create and manipulate graphical objects
* Create musical compositions
+
 
 +
==To Do==
 +
* Execute and modify the pieces of code introduced in this session
 +
* Read Chapter 8 of your text
 +
 
 +
== Useful Links ==
 +
 
 +
[[Myro_Reference_Manual#Graphics_Objects_Interface| Myro Graphics Objects Interface]] <br>
 +
[http://cs.brynmawr.edu/~dkumar/Myro/Text/June09/PDF/Chapter8.pdf| Chapter 8 of your Text]
 +
 
 +
== Creating a Graphics Window ==
  
=='''Sound'''==
+
You have probably used different computer programs such as ''Paint'' or ''GIMP'' to draw different shapes and edit images.
Having explored and used many of the robot commands by now, you have seen
+
In this lab session you will be creating and manipulating various drawings.  
that your robot make beeps when you call the '''beep()''' function. For
+
The Myro module provides various commands you can use to draw objects, color them and
instance, if you execute the following command:
+
manipulate them in other ways. In order to draw anything you first need to create a window or canvas
 +
to draw it on. You can create such a window using the following command:
  
 
<pre>
 
<pre>
beep(3, 880)
+
myCanvas = GraphWin()
 
</pre>
 
</pre>
  
This command tells your robot to play a tone at 880 Hertz for 3 seconds. Hertz is
+
When you enter this command. A window similar to the one below should appear:
a unit that measures frequency. <br>
+
[[Image:Graphwin.GIF|center]]
  
<b>1Hertz = 1cycle / second </b>
+
To close the window, use the following command:
 +
<pre>
 +
myCanvas.close()
 +
</pre>
  
Therefore, a beep at 880 Hz represents 880 complete cycles per second.
+
By default, the size of the window is 200 pixels high and 200 pixels wide and its name is
Humans can hear frequencies in the 20 Hz to 20000 Hz (or 20 Kilo Hertz) range and
+
“Graphics Window”. To change the size of the window and to rename it, you can execute the command below:
are able to distinguish sounds that differ only by a few
+
<pre>
Hertz (as little as 1 Hz). This ability varies from person to person.
+
myCanvas = GraphWin(“My BigCanvas”, 300, 300)
 +
</pre>
  
Try the following commands and see if you can distinguish between the two tones:
+
The command above creates a window called “My BigCanvas” that is 300 pixels wide and 300 pixels tall.
 +
You can change the background color of your graphics window to yellow as shown below:
 +
<pre>
 +
myCanvas.setBackground(“yellow”)
 +
</pre>
 +
[[Image:YellowWin.GIF|center]]
 +
 
 +
Examples of colors you can use are: “red”, “blue”, “gray”, “yellow”, “AntiqueWhite”, “LavenderBlush”, “WhiteSmoke”.
 +
There are several thousand color names to choose from. To see a list of these colors, Google: "color names list".
  
 +
You can also create your own colors by specifying their red, green, and blue components.
 +
Recall from previous labs and lectures that each color is made up of three values: '''RGB''' or red, green and blue color
 +
values. Each of the these values can be in the range 0 to 255. For instance, the RGB values of pure red are: (255, 0, 0).
 +
White is defined as (255,255, 255), black as (0, 0, 0) and pink as (255, 175, 175). There are as many as 256x256x256 colors
 +
(i.e. over 16 million colors!). Given specific RGB values, you can create a new color by using the
 +
command, color_rgb:
 
<pre>
 
<pre>
beep(1, 440)
+
myColor = color_rgb(255, 175, 175)
beep(1, 450)
+
 
</pre>
 
</pre>
  
To make the tones more distinctive, place the commands above in a loop so
+
== Drawing Objects on your Canvas ==
that you can repeatedly hear the alternating tones. <br>
+
  
 +
Once you've created a window, you can then draw a variety of geometrical objects (points, lines, circles, rectangle, and
 +
even text and images) on your canvas. Before you draw an object, however, you must first ''create'' it.
 +
Before you can do this, you should become familiar with the coordinate system of the graphics window.
  
'''Do This:''' Program your Scribbler to create a siren by repeating two
+
In a graphics window with width, W=200 and height H=400 (i.e WxH pixels) the pixel
different tones. You will have to experiment
+
(0, 0) is at the top left corner and the pixel (199, 399) will be at the bottom
with different pairs of frequencies (they may be close together or far apart) to
+
right corner. That is, x-coordinates increase as you go right and y-coordinates
produce a realistic sounding siren. Write your program to play the siren for 15
+
increase as you go down. <br>
seconds. The louder the better!
+
You can also have Myro make a beep directly out of your computer, rather
+
than the robot, with the command:
+
  
 +
The simplest object that you can ''create'' is a point. This is done as follows:
 
<pre>
 
<pre>
computer.beep(1, 440)
+
p = Point(100, 50)
 +
</pre>
  
 +
In the above command, p is an object that is a Point whose x-coordinate is at 100 and y-coordinate
 +
is at 50. This command ''creates'' a Point object. In order to draw it on the canvas, you
 +
have to enter the command:
 +
<pre>
 +
p.draw(myCanvas)
 
</pre>
 
</pre>
  
'''Do This:''' Try the siren program on your computer instead of on your robot.
+
In python, objects such as points, strings, lists etc. have standard operations that can be
 +
performed on them. The general syntax for carrying out an operation on an object is: <br>
  
=='''Musical Scales'''==
+
<object>.<function>(<parameters>)
  
In western music, a ''scale'' is divided into 12 notes (from 7 major notes:
+
In the example above, <object> is the name p which was earlier
ABCDEFG). An ''octave'' in C comprises of the 12 notes shown below: <br>
+
defined to be a Point object, <function> is draw, and <parameters> is
 +
myCanvas. The draw function requires the graphics window as the parameter.
 +
That is, you are asking the point represented by p to be drawn in the window
 +
specified as its parameter. The Point objects have other useful functions.
 +
For instance, you can use the following commands to get the  x- and y-coordinates
 +
of the point:  
 +
<pre>
 +
>>> p.getX()
 +
100
 +
>>> p.getY()
 +
50
 +
</pre>
  
<b> C C#/Db D D#/Eb E F F#/Gb G G#/Ab A A#/Bb B </b> <br><br>
+
Objects are created using their ''constructors'' like the Point(x, y) constructor above. You
 +
will use lots of constructors in this section for creating other graphics objects. For instance,
 +
you can create a line object in a similar manner. To create a line, you must specify the location the two end
 +
points of the line. Therefore a line that starts from the point (0, 0) and ends at the point
 +
(100, 200) can be created and drawn as follows:
  
C# (pronounced "C sharp") is the same tone as Db (pronounced "D flat").
+
<pre>
Frequencies corresponding to a specific note, for example C, are multiplied (or
+
L = Line(Point(0,0), Point(100,200))
divided) by 2 to generate the same note in a higher (or lower) octave. For instance
+
L.draw(myCanvas)
in the two tones shown below, the second tone is one octave higher than the first: <br>
+
</pre>
beep(1, 440)<br>
+
beep(1, 880) <br> <br>
+
  
Therefore in order to raise a tone by 1 octave, you multiply the frequency by 2.
+
The image below shows the canvas with the objects drawn so far.
Likewise, to make a tone 1 octave lower, you divide by 2.
+
Notes indicating an octave can be denoted as follows: <br>
+
<b>C0 C1 C2 C3 C4 C5 C6 C7 C8 </b><br>
+
That is, C0 is the note for C in the lowest (or 0) octave. The fifth octave
+
(numbered 4) is commonly referred to as a middle octave. Thus C4 is the C
+
note in the middle octave. The frequency corresponding to C4 is 261.63 Hz. <br> <br>
+
  
'''Do This:'''
+
[[Image:WinLine.GIF|center]]
Try playing it on the Scribbler. Also try C5 (523.25) which is twice the
+
You can then draw the line using the same draw command as above:
frequency of C4 and C3 (130.815). <br>
+
<pre>
 +
L.draw(myCanvas)
 +
</pre>
  
=='''Computing the Scribbler's Range of Tones'''==
+
To get the values of a line’s end points, use the following commands:
 +
<pre>
 +
>>> start = L.getP1()
 +
>>> start.getX
 +
0
 +
>>> end = L.getP2()
 +
>>> end.getY()
 +
200
 +
</pre>
  
In common tuning, the 12 notes are equidistant. Therfore, if the frequency doubles every octave, each
+
Here is a small Python loop that can be used to create and draw several lines:
successive note is 21 / 12 apart. That is, if C4 is 261.63 Hz, C# (or Db) will be:
+
<pre>
C#4/Db4 = 261.63*2^(1/12) = 277.18 <br>
+
for n in range(0, 200, 5):
We can then compute all successive note frequencies: <br>
+
  L=Line(Point(n,25),Point(100,100))
D4 = 277.18 * 2^(1/12) = 293.66
+
  L.draw(myCanvas)
D#4/Eb = 293.66*2^(1/12) = 311.13  <br>
+
</pre>
  
etc. <br>
+
[[Image:LinesWin.GIF|center]]
  
The lowest tone that the Scribbler can play is A0 and the highest tone is C8.
+
In the loop above the value of n starts at 0 and increases by
A0 has a frequency of 27.5 Hz, and C8 has a frequency of 4186 Hz. That's
+
5 after each iteration all the way up to but not including 200 (i.e. 195). For each value of
quite a range! See if you can you hear the entire range. Try this:
+
n a new Line object is created with starting co-ordinates (n,25) and end point at (100, 100).
  
 +
'''Do This:''' Try out all the commands introduced in this lab. Then observe the
 +
effects produced by the loop above. Change the increment 5 in the loop above
 +
to different values (1, 3, etc.) and observe the effect. Next, try out the
 +
following loop:
 
<pre>
 
<pre>
beep(1, 27.5)
+
for n in range(0, 200, 3):
beep(1, 4186)
+
L = Line(Point(n, 100), Point(180, 180))
 +
L.draw(myCanvas)
 +
wait(0.3)
 +
L.undraw()
 
</pre>
 
</pre>
  
'''Do This:''' Write a Scribbler program to play all the 12 notes in an octave
+
The ''undraw'' function, as the name implies, undraws a particular object. In the above for loop,
using the above computation. You may assume in your program that C0 is
+
a line is created for each value of n, drawn, an after 0.3 seconds, erased.
16.35 and then use that to compute all frequencies in a given octave (C4 is
+
Once again, you should modify different values in this loop, such as the increment or the wait time
16.35 * 24). Your program should input an octave (a number from 0 through
+
to see the effect.
8), produce all the notes in that octave and also printout a frequency chart for
+
 
each note in that octave.
+
== Drawing Other Geometric Objects ==
 +
 
 +
You can draw several other geometrical shapes such as: circles, rectangles, ovals, and
 +
polygons. Once again, in order to draw any of these shapes, you must first create it. For instance,
 +
to draw a circle, you can issue the commands:
 +
 
 +
<pre>
 +
centerPoint = Point(100, 150)
 +
radius = 30
 +
C = Circle(centerPoint, radius)
 +
C.draw(myCanvas)
 +
</pre>
 +
 
 +
centerPoint is a Point object and radius is specified in pixels. Thus, the circle C is
 +
centered at (100, 150) and has a radius of 30. For details on how other shapes are drawn,
 +
you can go to the link to ''Myro Graphics Objects Interface'' above. <br><br>
 +
 
 +
All the geometrical objects have several functions in common. For instance,
 +
you can get the center point of a circle, (as well as a rectangle, or an oval) by
 +
using the command:
 +
<pre>
 +
centerPoint = C.getCenter()
 +
</pre>
 +
 
 +
By default, all objects are drawn in black (this may be white if you are using a Mac).
 +
There are a number of ways to modify colors for objects. For each object you can specify a color for its
 +
outline as well as a color to fill it with. For example, to draw a circle centered
 +
at (100, 150), radius 30, and outline color red, and fill color yellow:
 +
<pre>
 +
C = Circle(Point(100, 150), 30)
 +
C.draw(myCanvas)
 +
C.setOutline(“red”)
 +
C.setFill(“yellow”)
 +
</pre>
 +
 
 +
Note: setFill and setOutline have the same effect on Point and Line objects
 +
(since there is no place to fill any color). Also, the line or the
 +
outline drawn is always 1 pixel thick. You can change the thickness by using
 +
the command setWidth(<pixels>). For example to change the width of the circle’s outline to 5 pixels:
 +
<pre>
 +
C.setWidth(5)
 +
</pre>
 +
 
 +
== Drawing Random Circles ==
 +
 
 +
'''Do This:''' The program below draws several circles of random sizes with
 +
random colors. Try it out! A sample output is shown below.
 +
 
 +
 
 +
[[Image:RandomCircles.GIF|center]]
 +
<pre>
 +
# File: randomCirles.py
 +
# Description: Program to draw a bunch of random colored circles
 +
 
 +
from random import *
 +
 
 +
def makeCircle(x, y, r):
 +
    # creates a Circle centered at point (x, y) of radius r
 +
    return Circle(Point(x, y), r)
 +
 
 +
def makeColor():
 +
    # creates a new color using random RGB values
 +
    red = randrange(0, 256)
 +
    green = randrange(0, 256)
 +
    blue = randrange(0, 256)
 +
    return color_rgb(red, green,blue)
 +
 
 +
def main():
 +
    # Create and display a graphics window
 +
    width = 300
 +
    height = 300
 +
    myCanvas = GraphWin(" Random Circles",width,height)
 +
    myCanvas.setBackground("white")
 +
    # draw a bunch of random circles with random colors
 +
    N = 200
 +
    for i in range(N):
 +
        # pick random center point and radius in the window
 +
        x = randrange(0,width)
 +
        y = randrange(0,height)
 +
        r = randrange(5, 25)
 +
        c = makeCircle(x, y, r)
 +
        # select a random color
 +
        c.setFill(makeColor())
 +
        c.draw(myCanvas)
 +
main()
 +
</pre>
 +
 
 +
'''Do This:''' Modify the program to input (specify) the number of circles
 +
to be drawn. Recall: randrange(m,n) returns a random number in range [m to n-1].
 +
Modify the program to create other geometric shapes and draw them randomly.
 +
 
 +
== Animating Geometric Objects ==
 +
You can move any geometric object you draw in the graphics window by using the command
 +
'''move(dx, dy)'''. For example, to move the circle, C, 10 pixels to the right and 5
 +
pixels down you can use the command (Note: C must have already been predefined):
 +
<pre>
 +
C.move(10, 5)
 +
</pre>
 +
'''Do This:''' Write a program that moves a circle about (randomly) in the
 +
graphics window. First, enter this following program and try it out.
 +
<pre>
 +
# Moving a circle
 +
 
 +
from random import *
 +
def main():
 +
    # create and draw the graphics window
 +
    w = GraphWin("Moving Circle", 500, 500)
 +
    w.setBackground("white")
 +
    # Create a red circle
 +
    c = Circle(Point(250, 250), 50)
 +
    c.setFill("purple")
 +
    c.draw(w)
 +
    # Do a simple animation for 200 steps
 +
    for i in range(200):
 +
        c.move(randrange(-4, 5), randrange(-4, 5))
 +
        wait(0.2)
 +
main()
 +
</pre>
 +
'''Do This:''' Write a program to move different geometric objects around in
 +
your graphics window.
 +
==Drawing Text and Images==
 +
 
 +
You can also insert '''text''' into your graphics window just like you did with geometric shapes.
 +
You will first have to create the text using the command:
 +
<pre>
 +
myText = Text(<anchor point>, <string>)
 +
</pre>
 +
 
 +
and then draw it. You can specify the type face, size, and style of the text. See the Myro graphics reference
 +
at the end of chapter 8 in your text or on the link provided above to get more details about the
 +
different features of texts. <br>
 +
 
 +
'''Images''' can also be treated like the geometric objects you have been creating and drawing. You can create an
 +
image using the Image command:
 +
<pre>
 +
myPhoto = Image(<centerPoint>, <file name>)
 +
</pre>
 +
 
 +
You have to have an already prepared image in one of the common image
 +
formats (like JPEG, GIF, etc.) and stored in a file (<file name>). Once the
 +
image object is created, it can be drawn, undrawn, or moved just like other
 +
shapes. <br>
 +
 
 +
You can use a combination of texts, images, and geometric objects to create
 +
simple and more advanced graphical programs such as making plots, creating art,
 +
developing video games or graphical user interfaces, or even an
 +
animated story. What you do with this knowledge is only limited by your creativity.
  
 
= Assignment 09 =
 
= Assignment 09 =
  
 +
'''*** Exercise 4 will be graded as a BONUS''' worth 4 points. Exercises 1-3 will be graded out of 9. <br>
 +
 +
---------------------------------------------------------------------------------------------------
 +
'''***Original Assignment below'''<br>
 +
 +
'''Chapter 8, Exercises 1 - 4. ''' '''Note: ''' Exercise Four is a BONUS Part. These exercises are detailed below: <br>
 +
 +
'''Exercise 1: ''' Using the graphics commands learned in this session (and detailed in your text), write a
 +
program to generate a seasonal drawing: winter, summer, beach, or a festival scene. <br>
 +
 +
'''Exercise 2: ''' Write a program that has a function drawRobot(x, y, w) such that it draws the
 +
figure of a robot shown on the right (see your text for this image, pg. 212). The robot is anchored at (x, y) and is drawn in the window, w. Also notice that the robot is entirely made up of rectangles (8 of them). <br>
 +
 +
'''Exercise 3: ''' Using the drawRobot function from the previous exercise, draw a pyramid of robot as shown on the right
 +
(see your text for this image, pg. 212).
 +
 +
'''Exercise 4: ''' Suppose that each robot from the previous exercise is colored using one of
 +
the colors: red, blue, green, and yellow. Each time a robot is drawn, it uses the
 +
next color in the sequence. Once the sequence is exhausted, it recycles. Write
 +
a program to draw the above pyramid so robots appear in these colors as they
 +
are drawn. You may decide to modify drawRobot to have an additional
 +
parameter: drawRobot(x, y, c, w) or you can write it so that drawRobot
 +
decides which color it chooses. Complete both versions and compare the
 +
resulting programs. Discuss the merits and/or pitfalls of these versions with
 +
friends. [Hint: Use a list of color names.] Note: You only need to ensure that each robot on the same level of the pyramid has the same color. <br>
 +
 +
= Links =
  
= Links to Course-Related Pages =
 
 
* Back to [http://wiki.roboteducation.org/CS110_Lab Lab Home Page]  
 
* Back to [http://wiki.roboteducation.org/CS110_Lab Lab Home Page]  
* Back to [http://cs.brynmawr.edu/Courses/cs110/fall2009/ Course Home Page]
+
* Back to [http://cs.brynmawr.edu/Courses/cs110/spring2010/ Course Home Page]

Latest revision as of 18:09, 25 March 2010

Lab 09: Objects in Graphics

Objective

  • Use various Myro commands to create and manipulate graphical objects

To Do

  • Execute and modify the pieces of code introduced in this session
  • Read Chapter 8 of your text

Useful Links

Myro Graphics Objects Interface
Chapter 8 of your Text

Creating a Graphics Window

You have probably used different computer programs such as Paint or GIMP to draw different shapes and edit images. In this lab session you will be creating and manipulating various drawings. The Myro module provides various commands you can use to draw objects, color them and manipulate them in other ways. In order to draw anything you first need to create a window or canvas to draw it on. You can create such a window using the following command:

myCanvas = GraphWin()

When you enter this command. A window similar to the one below should appear:

Graphwin.GIF

To close the window, use the following command:

myCanvas.close()

By default, the size of the window is 200 pixels high and 200 pixels wide and its name is “Graphics Window”. To change the size of the window and to rename it, you can execute the command below:

myCanvas = GraphWin(“My BigCanvas”, 300, 300)

The command above creates a window called “My BigCanvas” that is 300 pixels wide and 300 pixels tall. You can change the background color of your graphics window to yellow as shown below:

myCanvas.setBackground(“yellow”)
YellowWin.GIF

Examples of colors you can use are: “red”, “blue”, “gray”, “yellow”, “AntiqueWhite”, “LavenderBlush”, “WhiteSmoke”. There are several thousand color names to choose from. To see a list of these colors, Google: "color names list".

You can also create your own colors by specifying their red, green, and blue components. Recall from previous labs and lectures that each color is made up of three values: RGB or red, green and blue color values. Each of the these values can be in the range 0 to 255. For instance, the RGB values of pure red are: (255, 0, 0). White is defined as (255,255, 255), black as (0, 0, 0) and pink as (255, 175, 175). There are as many as 256x256x256 colors (i.e. over 16 million colors!). Given specific RGB values, you can create a new color by using the command, color_rgb:

myColor = color_rgb(255, 175, 175)

Drawing Objects on your Canvas

Once you've created a window, you can then draw a variety of geometrical objects (points, lines, circles, rectangle, and even text and images) on your canvas. Before you draw an object, however, you must first create it. Before you can do this, you should become familiar with the coordinate system of the graphics window.

In a graphics window with width, W=200 and height H=400 (i.e WxH pixels) the pixel (0, 0) is at the top left corner and the pixel (199, 399) will be at the bottom right corner. That is, x-coordinates increase as you go right and y-coordinates increase as you go down.

The simplest object that you can create is a point. This is done as follows:

p = Point(100, 50)

In the above command, p is an object that is a Point whose x-coordinate is at 100 and y-coordinate is at 50. This command creates a Point object. In order to draw it on the canvas, you have to enter the command:

p.draw(myCanvas)

In python, objects such as points, strings, lists etc. have standard operations that can be performed on them. The general syntax for carrying out an operation on an object is:

<object>.<function>(<parameters>)

In the example above, <object> is the name p which was earlier defined to be a Point object, <function> is draw, and <parameters> is myCanvas. The draw function requires the graphics window as the parameter. That is, you are asking the point represented by p to be drawn in the window specified as its parameter. The Point objects have other useful functions. For instance, you can use the following commands to get the x- and y-coordinates of the point:

>>> p.getX()
100
>>> p.getY()
50

Objects are created using their constructors like the Point(x, y) constructor above. You will use lots of constructors in this section for creating other graphics objects. For instance, you can create a line object in a similar manner. To create a line, you must specify the location the two end points of the line. Therefore a line that starts from the point (0, 0) and ends at the point (100, 200) can be created and drawn as follows:

L = Line(Point(0,0), Point(100,200))
L.draw(myCanvas)

The image below shows the canvas with the objects drawn so far.

WinLine.GIF

You can then draw the line using the same draw command as above:

L.draw(myCanvas)

To get the values of a line’s end points, use the following commands:

>>> start = L.getP1()
>>> start.getX
0
>>> end = L.getP2()
>>> end.getY()
200

Here is a small Python loop that can be used to create and draw several lines:

for n in range(0, 200, 5):
   L=Line(Point(n,25),Point(100,100))
   L.draw(myCanvas)
LinesWin.GIF

In the loop above the value of n starts at 0 and increases by 5 after each iteration all the way up to but not including 200 (i.e. 195). For each value of n a new Line object is created with starting co-ordinates (n,25) and end point at (100, 100).

Do This: Try out all the commands introduced in this lab. Then observe the effects produced by the loop above. Change the increment 5 in the loop above to different values (1, 3, etc.) and observe the effect. Next, try out the following loop:

for n in range(0, 200, 3):
	L = Line(Point(n, 100), Point(180, 180))
	L.draw(myCanvas)
	wait(0.3)
	L.undraw()

The undraw function, as the name implies, undraws a particular object. In the above for loop, a line is created for each value of n, drawn, an after 0.3 seconds, erased. Once again, you should modify different values in this loop, such as the increment or the wait time to see the effect.

Drawing Other Geometric Objects

You can draw several other geometrical shapes such as: circles, rectangles, ovals, and polygons. Once again, in order to draw any of these shapes, you must first create it. For instance, to draw a circle, you can issue the commands:

centerPoint = Point(100, 150)
radius = 30
C = Circle(centerPoint, radius)
C.draw(myCanvas)

centerPoint is a Point object and radius is specified in pixels. Thus, the circle C is centered at (100, 150) and has a radius of 30. For details on how other shapes are drawn, you can go to the link to Myro Graphics Objects Interface above.

All the geometrical objects have several functions in common. For instance, you can get the center point of a circle, (as well as a rectangle, or an oval) by using the command:

centerPoint = C.getCenter()

By default, all objects are drawn in black (this may be white if you are using a Mac). There are a number of ways to modify colors for objects. For each object you can specify a color for its outline as well as a color to fill it with. For example, to draw a circle centered at (100, 150), radius 30, and outline color red, and fill color yellow:

C = Circle(Point(100, 150), 30)
C.draw(myCanvas)
C.setOutline(“red”)
C.setFill(“yellow”)

Note: setFill and setOutline have the same effect on Point and Line objects (since there is no place to fill any color). Also, the line or the outline drawn is always 1 pixel thick. You can change the thickness by using the command setWidth(<pixels>). For example to change the width of the circle’s outline to 5 pixels:

C.setWidth(5)

Drawing Random Circles

Do This: The program below draws several circles of random sizes with random colors. Try it out! A sample output is shown below.


RandomCircles.GIF
# File: randomCirles.py
# Description: Program to draw a bunch of random colored circles

from random import *

def makeCircle(x, y, r):
    # creates a Circle centered at point (x, y) of radius r
    return Circle(Point(x, y), r)

def makeColor():
    # creates a new color using random RGB values
    red = randrange(0, 256)
    green = randrange(0, 256)
    blue = randrange(0, 256)
    return color_rgb(red, green,blue)

def main():
    # Create and display a graphics window
    width = 300
    height = 300
    myCanvas = GraphWin(" Random Circles",width,height)
    myCanvas.setBackground("white")
    # draw a bunch of random circles with random colors
    N = 200
    for i in range(N):
        # pick random center point and radius in the window
        x = randrange(0,width)
        y = randrange(0,height)
        r = randrange(5, 25)
        c = makeCircle(x, y, r)
        # select a random color
        c.setFill(makeColor())
        c.draw(myCanvas)
main()

Do This: Modify the program to input (specify) the number of circles to be drawn. Recall: randrange(m,n) returns a random number in range [m to n-1]. Modify the program to create other geometric shapes and draw them randomly.

Animating Geometric Objects

You can move any geometric object you draw in the graphics window by using the command move(dx, dy). For example, to move the circle, C, 10 pixels to the right and 5 pixels down you can use the command (Note: C must have already been predefined):

C.move(10, 5)

Do This: Write a program that moves a circle about (randomly) in the graphics window. First, enter this following program and try it out.

# Moving a circle

from random import *
def main():
    # create and draw the graphics window
    w = GraphWin("Moving Circle", 500, 500)
    w.setBackground("white")
    # Create a red circle
    c = Circle(Point(250, 250), 50)
    c.setFill("purple")
    c.draw(w)
    # Do a simple animation for 200 steps
    for i in range(200):
        c.move(randrange(-4, 5), randrange(-4, 5))
        wait(0.2)
main()

Do This: Write a program to move different geometric objects around in your graphics window.

Drawing Text and Images

You can also insert text into your graphics window just like you did with geometric shapes. You will first have to create the text using the command:

myText = Text(<anchor point>, <string>)

and then draw it. You can specify the type face, size, and style of the text. See the Myro graphics reference at the end of chapter 8 in your text or on the link provided above to get more details about the different features of texts.

Images can also be treated like the geometric objects you have been creating and drawing. You can create an image using the Image command:

myPhoto = Image(<centerPoint>, <file name>)

You have to have an already prepared image in one of the common image formats (like JPEG, GIF, etc.) and stored in a file (<file name>). Once the image object is created, it can be drawn, undrawn, or moved just like other shapes.

You can use a combination of texts, images, and geometric objects to create simple and more advanced graphical programs such as making plots, creating art, developing video games or graphical user interfaces, or even an animated story. What you do with this knowledge is only limited by your creativity.

Assignment 09

*** Exercise 4 will be graded as a BONUS worth 4 points. Exercises 1-3 will be graded out of 9.


***Original Assignment below

Chapter 8, Exercises 1 - 4. Note: Exercise Four is a BONUS Part. These exercises are detailed below:

Exercise 1: Using the graphics commands learned in this session (and detailed in your text), write a program to generate a seasonal drawing: winter, summer, beach, or a festival scene.

Exercise 2: Write a program that has a function drawRobot(x, y, w) such that it draws the figure of a robot shown on the right (see your text for this image, pg. 212). The robot is anchored at (x, y) and is drawn in the window, w. Also notice that the robot is entirely made up of rectangles (8 of them).

Exercise 3: Using the drawRobot function from the previous exercise, draw a pyramid of robot as shown on the right (see your text for this image, pg. 212).

Exercise 4: Suppose that each robot from the previous exercise is colored using one of the colors: red, blue, green, and yellow. Each time a robot is drawn, it uses the next color in the sequence. Once the sequence is exhausted, it recycles. Write a program to draw the above pyramid so robots appear in these colors as they are drawn. You may decide to modify drawRobot to have an additional parameter: drawRobot(x, y, c, w) or you can write it so that drawRobot decides which color it chooses. Complete both versions and compare the resulting programs. Discuss the merits and/or pitfalls of these versions with friends. [Hint: Use a list of color names.] Note: You only need to ensure that each robot on the same level of the pyramid has the same color.

Links