Lab Module G -- Painters: graphics and higher order abstractions.

CISC 280 Program Development Techniques

This module is a two week unit and constitutes the first of two programming projects. It counts for 9% of the grade (one module and one half of the project credit). Only exercise 1 is due in lab on Monday, March 25. The rest is due on April 8. There will be no Module G topping exercise in lab on March 25. Thus March 25 is a good opportunity to make up topping off exercises for previous modules, if not yet completed. In fact, it is crucial to get completely caught up with modules A-F at this time. See the lab modules note at the course web site for details. This is also a good lab to get design and debugging help on the painter graphics. This means you must work on the painter project this week so that you discover what parts of it you don't fully understand and can come to lab with your questions about it.

Goals:

  1. Gain further experience of the powers of abstraction levels -- doing so in a graphical context where the output visually reflects the programming effort.
  2. Gain familiarity with some transformational principles used in computer graphics.
  3. Design some aesthetically pleasing pictures.

Reading: SICP (Structure and Interpretation of Computer Programs), Section 2.2.4
Code on composers: ~saunders/280/Painters.scm.
The command: drscheme ~saunders/280/graphics/painters.scm
will start scheme with the painter code loaded. Otherwise you can open it from the file menu. The summary lists most of the procedures defined in the painter system.


Homework exercise:

Painter project

(due at start of lab on 8 Apr 2002.) Submit printouts of your code and of your graphical images for each problem.
  1. Write a painter that consists of your painter from exercise 0 done 15 times in an 3 by 5 grid. Fourteen of the copies should be right-side-up and exactly one should be rotated 90 degrees (left or right as you choose). Each copy should be of the same size, having as height 1/3 the height of the overall frame and as width 1/5 the width of the frame. You can place the rotated copy anywhere in the interior of the frame. That is to say, don't make it one of the copies that touches a side of the overall frame. So the pattern is like this:
    UUUUU
    UCUUU
    UUUUU
    
  2. Write Painter operator diamond.

    (stack fg:painter bg:painter n:odd-positive-int) -> q:painter

    We will use the notation "x:t" to indicate that the argument x is supposed to be of type t. The output of stack is a painter q, where (q f:frame) will draw a diamond of fg's on a background of bg's in frame f. More precisely, on the i-th strip (of width (1/n)th of the frame width), q will paint j bg's followed by k fg's followed by k more bg's, each occupying (1/n)th of the strip. For the i-th strip, k would be 2*min(i, n+1-i) - 1 and j would be (n - k)/2. Something like this (for the case when fg paints a "p", bg paints a "." and n is 5).

    . . p . .
    . p p p .
    p p p p p 
    . p p p .
    . . p . .
    
    Illustrate on

  3. Let UPO stand for "unary painter operator". A UPO takes a painter as argument and returns a painter. For example, flip-vert is a UPO. Let BPO stand for "binary painter operator". A BPO takes two painters as arguments and returns a painter. For example, beside is a BPO. Write a higher-order operator which takes one or more UPO's and/or BPO's among it's arguments and returns a UPO or BPO. The higher-order operator square-of-four is a model for this exercise. It takes 4 UPO's and returns a UPO. You can be creative as to what your higher order operator does. It is acceptable for it to take some lower order arguments such as painters or numbers as well as one or more UPO's or BPO's. For example, a higher-order operator might have this signature:
    (wheel-of-four top-left:UPO top-right:UPO bottom-left:UPO bottom-right:UPO center:painter) -> output:UPO
    such that, for the frame divided into 9 segments as
    123
    456
    789,
    
    The output UPO takes a painter p and paints (top-left p) spread over positions 1&2, paints (top-right p) in 3&6, paints (bottom-right p) in 8&9, paints (bottom-left p) in 4&7, and paints center in 5.

    Use your imagination and cook up something you'll enjoy.


Here is a way to print a graphics window in unix (for submitting the result):

  1. enter "xv".
  2. pop up the "xv controls" window by clicking right button on the xv window.
  3. click the "Grab" command.
  4. Click left button on the window to be captured. Note: this
  5. click will NOT destroy a painter window, the next click will.
  6. Return to "xv controls" and select "Print".
  7. Finally, select "Quit" on the xv controls to exit xv.

Module G Flourishes (optional problems):

  1. Project flourish: Write additional higher-order operator(s) like the one for part 3 above.

Summary of data types used in painter system

Types: Summary of the procedures used in the painter module and defined (except where *starred) in the text.
  • Vect operators( add-vect, sub-vect, scale-vect):
  • Segment primitives ( make-segment, start-segment, end-segment):
  • Frame primitives ( make-frame, origin-frame, edge1-frame, edge2-frame, frame-coord-map):
  • Viewport primitives: See Dr Scheme online help if interested in more features of viewports (windows).