CS 1101.py Computer Science I
Spring 2016

Computer Science Department
The Morrissey College of Arts and Sciences
Boston College

About Staff Textbook Grading Schedule Canvas
Piazza Library Resources Labs GitHub Problem Sets
Problem Set 2: Repetition

Assigned: Tuesday January 26, 2016
Due: Thursday February 4, 2016 at midnight
Points: 16 Points up to 22 Points Total
Points: 12 Points up to 15 Points Total

This is a pair problem set. You must work with a partner. If you don't know anyone yet (perfectly understandable!), you can use the Piazza partner finding tool or you can ask a staffer to help you find a partner.

There are 6 required 2-point problems A through F.

The problems, labeled A through F, all involve writing functions that use some of the stddraw.Picture functions to draw images in the graphics window. As we've discussed in class, the graphics window is Cartesian or xy-plane. The pixel at the extreme lower left corner is at (x, y) coordinates (0, 0) while the pixel at the extreme upper right corner is at (x, y) coordinates (1.0, 1.0).

Parts A through E involve drawing rings in the graphics window; all are worth 2 points. Part F requires a drawing of squares running on a diagonal. Part A is relatively simple and can be done right away. Parts B and C require branching and parts D, E and F all require repetition which we will continue to cover in the coming lectures.

Setup

Start by creating a folder named with your surname followed by the digit 2. (Mine would be called Muller2.) In this problem set you'll populate the folder with one .py file for each problem. All of the problems are couched in terms of writing a specific function. In each case, you should demonstrate your function by embedding a call to your function in an appropriate tester function that calls the required function with arguments that demostrate that it works correctly. For example, the first problem requires a function ring. It should be defined in a file ring.py. The ring.py file should look something like:
# file: ring.py
# author: Bob Muller
# date: September 15, 2014
#
from stddraw import *

def ring(picture, x, y, radius, width, color):
    YOUR CODE HERE

def testRing():
    picture = Picture()
    color = picture.randomColor()
    ring(picture, .5, .5, .3, .2, color)
    picture.start()

testRing()
Or, if you prefer to run your program by clicking the picture as demonstrated in class:
# file: ring.py
# author: Bob Muller
# date: September 1, 2014
#
from stddraw import *

def ring(picture, x, y, radius, width, color):
    YOUR CODE HERE

def testRing():
    def responder(event):
        color = picture.randomColor()
        ring(picture, .5, .5, .3, .2, color)

    picture = Picture()
    picture.bind('<Button-1>', responder)
    picture.start()

testRing()
All of the problems in refer to the following images.

Figure A Figure C Figure D
Figure E Figure F

Part A (2 Points) ring

Write a function ring : Picture * float * float * float * float * color -> void such that, given a call ring(pict, x, y, radius, width, color), the ring function draws a ring with the given radius and width and color centered at point (x, y). Figure A show the result of the call ring(picture, .5, .5, .3, .2, 'green').

Part B (2 Points) goodRing

The radius of a ring is the distance from its center to its outer edge. The width of a ring is the width of its colored band. Clearly, the width of a ring cannot be greater than or equal to its radius. Write a new version of ring called goodRing(picture, x, y, radius, width, color) that will only draw a ring if the width is less than the radius. If the width and radius provided as inputs to goodRing are such that the width is greater than or equal to the radius, the goodRing function should print an error message and return the integer 0.

Part C (2 Points) ringInBounds

Write a new version of ring called ringInBounds(picture, x, y, radius, width, color) that will only draw a ring if it is good, i.e., such that width < radius and it is completely contained within the graphics window. It would rule out rings like the one show in Figure C. If the x, y and radius provided as inputs to ringInBounds give rise to a ring that would not be completely included in the picture, the ringInBounds function should use the print statement to print an error message to the console. For example, the call ringInBounds(picture, .5, .5, .8, .2, 'red') should give rise to an error message along the following lines:
Error: Ring centered at (.5, .5) with radius .8 is out of bounds.
If the ring is good and contained within the unit square, the function should go ahead and render it. Note that you should fee free to use your previously built ring and goodRing functions for this purpose if you want to. In order to get access to these functions you'll need to import the files containing their definitions. Simply include the line import ring, goodRing at the top of the file.

Part D (2 Points) Random Rings

Python's Standard Library includes a handy random module that has a number of functions relating to pseudo-random numbers. In order to access the definitions within this module, put the line:
import random
somewhere near the top of your file. In this problem we will use the random.random function. The function:
random.random : void -> float 
returns a random number r such that 0 <= r < 1. Fire up a Python shell and try it! Among many other applications, the random.random function can be used to randomly select x or y coordinates in the unit square. For example:
...
x = random.random()
y = random.random()
...
We'll work extensively with random functions throughout the semester.

In this problem, you will write a function randomRings : Picture * int -> void such that given a call randomRings(picture, N), the randomRings function draws N random rings in the picture. For the purposes of this problem, it doesn't matter whether or not the rings are strictly contained in the picture. See Figure D.

Everything about a given ring should be randomly generated: the center-point, the radius, the width and the color. But note that the randomly selected width of the ring should not be larger than the radius. You can use the stddraw function randomColor to generate a random color.

Your randomRing function should be accompanied by a tester function in the usual way.

Part E (2 Points) rowOfRings

Write a function rowOfRings : Picture * float * int -> void such that given a call rowOfRings(picture, y, N) the rowOfRings function draws a horizontal row of N equally sized rings across the square picture centered at height y, as shown in Figure E. The radii of all the rings should be the same and can be easily computed from N (and the width of the picture). The width and colorof each the rings should be computed randomly. See Figure E.

Your function should have the usual tester function.

Part F (2 Points) Diamonds

Write a function diamonds : Picture * int -> void such that given a call diamonds(picture, n), the diamonds function draws a sequence of n square randomly colored blocks as shown in Figure F. Your function should have the usual tester function.

Part 2: Tessellation HAS BEEN MOVED TO PROBLEM SET 3

Created on 01-19-2016 23:10.