



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CS106 Handout # J Zelenski Jan 16, 2008
A note about graphics: The graphics library we use in CS106B/X supports simple pen-based drawing in a Cartesian coordinate system scaled on inches. To familiarize yourself with it, check out section 5.3 of the reader and peruse the graphics.h and extgraph.h header files in the reader appendix or browse the "Documentation" link of our class web site. We will ask you to write some simple graphics code this quarter but it's not a focus for us, so I'd advise picking up the details on an as-needed basis. For assignments that use fancy graphics, we'll typically provide the graphics for you since writing that code can be boring and tedious and we'd rather not bog you down. This handout is adopted from our old CS106A (in C) course, where we used this as an example for learning how to decompose a problem. Decomposition in the procedural paradigm means something slightly different than the decomposition you practiced in the object-oriented world. An object- oriented program is divided in classes, which then separate their operations into methods. The approach in a procedural program is to divide the problem into its component tasks, implemented as separate functions, each of which is potentially further subdivided into smaller functions. For example, the usher.cpp program presented below is defined in terms of boxes, circles, grids, and triangles. Identifying the general tools is part of the strategy of bottom-up implementation. Breaking the program down step by step is the process of top-down design. Both strategies are essential to your success as a programmer. This example is drawn from Exercise 8 of Chapter 7 of The Art and Science of C , which asks you to draw a diagram of the Edgar Allen Poe's (very scary) House of Usher.
The point of this exercise was further practice with the graphics library and an exercise in top-down design and stepwise refinement. Look the picture, try to decompose the picture into its components, look for opportunities for unification and come up with a reasonable set of functions that can accomplish the job. Use our solution below as an illustrative example of simple, complete C++ program that demonstrates good design and style. Note the many helper routines and how parameters allow those helpers to be as general as possible. A good design should make it relatively straightforward to make modifications — how would you change the program to make the towers taller or add panes to the windows or change the towers to rounded instead of peaked tops? A clean design should also allow you to quickly zero in on the troubled area when hunting bugs — if the door was being drawn in the wrong location, where would you look to try to fix it? */
*/
*/
purpose DrawPolygon function. Therefore, a client's use of our functions would be cleaner and more readable. Lastly, just for fun, we can create a function to draw spirograph-like nested polygons by constructing a function that repeatedly calls DrawPolygon in a loop while slightly shrinking and rotating on each nesting to get resulting artwork like this: n = 3 n = 4 n = 8 */