Warning: include(/web/html/ceeaton/courses/cs151s18/navLab.html): failed to open stream: No such file or directory in /web/html/ceeaton/courses/cs151/s18/geometry.php on line 10

Warning: include(): Failed opening '/web/html/ceeaton/courses/cs151s18/navLab.html' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /web/html/ceeaton/courses/cs151/s18/geometry.php on line 10

Thinking Geometrically

The goal of the next couple of assignments is to get you thinking about processes, organization, and taking advantage of repetition and the use of variables in place of hard numbers. The latter, in particular, is a form of generalization. If you can use variables instead of numbers, then a single function can serve many purposes.

The following is a quick note on one way of thinking about making an aggregate shape, which is a more complex shape that is made out of simpler shapes. In the case below, a chair, or bench, is made from three blocks. The challenge is to describe the relative location of the blocks in such a way that the function will draw the chair appropriately no matter the location or size.

The process is as follows:

  1. Draw the complex object you want to create out of the simpler objects on a piece of paper. Graph paper is nice because you can count blocks, but you can also just eyeball relative measurements. The image below is an image of a chair built from three blocks.
  2. Pick one of the blocks as the main block and circle its lower left corner. In the example above, it is the red circle. This is the origin of the chair shape.
  3. Make a table with a row for each block, similar to the one to the right of the grid. For each block find the offset of its lower left corner from the origin. In the example, block A has offsets of (0, 0) because its lower left corner is the origin; block B has an offset of (-1, 5). Then write down the width and height of each block. In the example, block A has a width of 1 and a height of 5; block B has a width of 8 and a height of 1.
  4. Now you are ready to write code. For each block in your shape, use the block function call as given in the figure, substituting in the numbers from the table for xoff, yoff, width, and height. For example, the proper function to create the chair would be as follows.
    def chair( x, y, scale ):
        # block A
        block( x, y, 1*scale, 5*scale )
        # block B
        block( x - 1 * scale, y + 5 * scale, 8*scale, 1*scale )
        # block C
        block( x + 6*scale, y, 1*scale, 12*scale )
  5. If you call the chair function with different arguments, it should draw the chair in different locations with different sizes. Try out the following as part of your main code.
    chair( -100, 0, 10 )
    chair( 0, 0, 5 )
    chair( 100, 0, 20 )

    Note that this method of defining the relative locations of the blocks will not work if you rotate the turtle. That requires a more sophisticated formula for the x and y offsets. But since a tipsy chair wouldn't work very well, that's ok for this example.

Back to the assignment...

© 2015 Bruce Maxwell