Saint Louis University |
Computer Science 150
|
Dept. of Math & Computer Science |
All of these problems are for practice. You are freely encouraged to work together and you do not need to submit any of this work for a grade.
The book gives code for drawing a pyramid, with one rectangle
per level (see /Public/goldwasser/150/book/ch04/pyramidLoop.py)
It uses a geometric sketch (left) as a model, producing the final
image (right) with a parameter controlling the size and number of levels.
A second version of the code
(see /Public/goldwasser/150/book/ch04/pyramidNested.py)
uses a nested loop to build the levels out of individual squares.
Our goal today is to create new patterns by either modifying those
programs, or generate new ones.
Problems
How about the following rainbow:
in which the user specifies the outer and inner radius, as
well as a list of colors. (We used the list
art = Image(width,height)Then you can manipulate any number of individual pixels with commands such as
art.setPixel(x, y, color)where color can be specified by name or by (r,g,b) tuple, where those are intensity values from 0 to 255. For example
Using the new Image class, we can have some fun with colors,
in particular by setting (r,g,b) tuples based on loop
variables. For example, here is one that does a linear
interpoloation from red to blue based on the
x-coordinate.
Here is another image that is based on letting the red
component vary inversely with the x-coordinate, the green
component vary based in (x+y) combined, and the blue component
vary based solely on the y-coordinate.
Here are some other effects based on various uses of the (x,y)
values to model the color choice.