COSC 8 -- Problem Solving with CS

CS 8, Fall 2009

Short Assignment 3: Drawing Nested Squares

This short assignment asks you to write a program to draw nested squares.

Instructions

You should print out your solutions to this assignment, and leave them in your section leader's mailbox in Kemeny by class time on Fri. Oct. 2, 2009. Include sample output.

Please also e-mail an electronic version of your code to cs8hw@cs.dartmouth.edu, with the subject "CS 8 Short Assignment 3". Please attach your code as one or more enclosures to the message, and do not include it directly in the body of the e-mail message.

You must work alone on this short assignment.


  1. Creating and drawing a square, given center and "radius"

    To draw nested squares it will be useful to specify a square by giving a center point and a "radius", where radius is half the side length. Write a function makeSquare that returns a colored square, given its color, center point, and radius. It should be an outlined square, not a filled one. (So use polyline, not polygon.) The type signature for the function should be:

    makeSquare :: Color -> Point -> Int -> Graphic

    Assign a name to a runGraphics call that draws a blue square centered at (100, 100) with radius 50 in a 300 by 300 graphics window. See the code on p. 42 for a model of how to do this. (You can copy this example and spaceClose from SimpleGraphics.hs.) Load your program into ghci and type the name to display the square.

  2. Drawing nested squares

    Use the function that you wrote above and whatever other functions you find useful to write to create the following picture:

    Nested boxes

    The window is 300 by 300, all the squares are centered at (150, 150), and the radii range from 10 to 140 in steps of 10.

    Hint: The first box on p. 62 shows how to easily create a list that is an arithmetic sequence. To create a list of the numbers from 10 to 140 in steps of 10 you can write [10, 20..140] There are approaches to solving this problem where this is useful and other approaches where it is not useful.



Department of Computer Science, Dartmouth College, Hanover, New Hampshire, USA

Last modified: 29-September-2009