Course Home | Assignments | Computing Resources | Lab Hours/Tutoring | Python | Schedule | Submit

Saint Louis University

Computer Science 150
Introduction to Object-Oriented Programming

Michael Goldwasser

Spring 2013

Dept. of Math & Computer Science

Hands-on Day

For Loops


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

  1. Instead of the basic pyramid, make a staircase rising to the right, with one rectangle per level

  2. Do the staircase but with many squares per level.

  3. Try to build a single Polygon object having a staircase shape.
  4. Can you make a picket fence, with a variable height, width, and number of pickets? We suggest making the gap between pickets exactly the same as the width of a picket. Here is a view of such a fence by itself

    and here is the fence incorporated into our standard house scene.
  5. 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 colors = ['red', 'orange', 'yellow', 'green', 'blue', 'blueviolet', 'darkviolet'].)

  6. Try to build a brick wall using 3x1 ratio bricks
  7. Feeling really board? How about a hex-brick walkway (bring your trigonometry)
  8. Another way for us to have fun is with colors. Our latest cs1graphics release has an Image class that let's you make pixel-level manipulations. As a brief tutorial, you can create an initially transparent image object with a given width and height as
        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 setPixel(x,y, (255,0,255)) produes bright magenta. For the Image class, we intentionally do not re-render changes after each call to setPixel. Instead, there is a updatePixels() method that must be called to force the rendering of the image to the screen.

    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.


Michael Goldwasser
CSCI 150, Spring 2013
Last modified: Tuesday, 05 February 2013
Course Home | Assignments | Computing Resources | Lab Hours/Tutoring | Python | Schedule | Submit