Due: 8:00am, Thursday, March 4. Value: 30 pts. Submit to Sauron.
(This assignment again uses the graphics package.
If you lost it, right-click
graphics.py
,
select Save Link As…
or Save Target As…
from the pop-up menu, and save the file on your computer. Then start
a new file in the Wing IDE for your own work and save it in the
same directory where you placed the graphics.py
file.)
Compose a program that draws a rainbow, analogous to the one at right. I drew the image as 60 nested circles, the largest with a radius of 80 pixels and each successive one with a radius 1 pixel smaller than the last. Finally, I drew a light blue circle to hollow out the middle and a light blue rectangle to block off the bottom half of all the circles.
(Python includes a color named lightblue. You can
also set the window's background to be lightblue by
using its setBackground method:
.)win.setBackground('lightblue')
The circles in the rainbow should have colors that vary according to the below graph. The graph's red line indicates that the first 20 circles should have a red value of 250, while the blue line indicates that these circles should have a blue value of 0. The green line indicates that each circle's green value should vary from 0 for circle 0 to 250 for circle 20. Thus, the first circle will have only red, while the 20th circle will have an equal mixture of red and green, which appears yellow. As graphed, the 20th through 30th would have decreasing red values, while the green value stays constant at 250 and the blue value stays at 0.
Pay particular attention to the innermost circles (at 60 in the graph): The innermost circle has a red value of only 125 — still much less than the amount of blue. This allows the rainbow to have a purple interior rather than a brighter fuchsia, and it looks quite a bit better.
So far, we've used setFill to configure the
color of a circle, but this doesn't affect the shape's outline,
which remains black.
To color the outline, use setOutline also.
To compute the color of each circle, you'll want to use the
graphics module's color_rgb function,
which takes three integers between 0 and 255 as parameters and
creates a color value that can then be given to both
setFill and setOutline. The below
example illustrates how to use this.
char_color = color_rgb(127, 255, 0) # Creates a color value for chartreuse
center = Point(100, 100)
bow = Circle(center, 50)
bow.setFill(char_color) # Configures shape to be filled
bow.setOutline(char_color) # and outlined with this color
You also need to know how to create a rectangle in order to
block out the bottom half of the circles for a good bow
effect. To create a rectangle, first
create two points representing opposite corners of the
rectangle, and then create the rectangle by providing those two
points as parameters:
upper_left = Point(20, 50) # This rectangle will be 160 pixels
lower_right = Point(180, 150) # wide and 100 pixels tall
box = Rectangle(upper_left, lower_right)
box.draw(window)
Of course, you should strive to have a solution that involves as little repetition of code as possible. You could conceivably perform this assignment by having separate code to create each of the 60 different circles, but that solution will recieve little credit.