University of Massachusetts Lowell : Organization of Programming Languages Spring 2004 Problem Set 4. Henderson Picture Language

Similar documents
1. The Square limit language

Fall Semester, Lecture Notes { September 26, Today's lecture will use the Henderson Picture Language as an example of how we can merge

6.001: Structure and Interpretation of Computer Programs. Themes to be integrated. Our target the art of M. C. Escher

Today's Lecture. Programming as the process of creating a new task-specific language. data abstractions procedure abstractions higher-order procedures

CISC 1600, Lab 3.1: Processing

HOW TO. In this section, you will find. miscellaneous handouts that explain. HOW TO do various things.

THE PAINT WINDOW. At the very top is the Title Bar, just as in all programs, below it is a very simple Menu Bar and below that is the Ribbon.

CSE 167: Lecture #5: Rasterization. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

GIMP WEB 2.0 ICONS. GIMP is all about IT (Images and Text) OPEN GIMP

Unit 1, Lesson 1: Moving in the Plane

Corel Draw 11. What is Vector Graphics?

CISC 1600, Lab 2.1: Processing

Word 2007: Flowcharts Learning guide

Gallery of Transition Effects. Transition Effects Dissolves Wipes Digital Video Effects

Measurement and Geometry (M&G3)

Graphics (Output) Primitives. Chapters 3 & 4

Word 2003: Flowcharts Learning guide

Turn in a printout of your code exercises stapled to your answers to the written exercises by 2:10 PM on Tuesday, January 18th.

Creating a Title Block & Border Using Chief Architect. Architectural Design & Residential Construction Penncrest High School

Ancient Cell Phone Tracing an Object and Drawing with Layers

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

Tutorial 3: Constructive Editing (2D-CAD)

2.2 Hierarchical Data and the Closure Property

The National Strategies Secondary Mathematics exemplification: Y8, 9

Paint Tutorial (Project #14a)

Microsoft PowerPoint 2016 Part 2: Notes, Links, & Graphics. Choosing a Design. Format Background

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Intermediate Microsoft Word 2010

CS 465 Program 5: Ray II

Chapter 5 Managing Graphic Objects

CSE 167: Lecture #17: Procedural Modeling. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2011

3.6: First Person Computer Games

Animations involving numbers

Create an Adorable Hedgehog with Basic Tools in Inkscape Aaron Nieze on Sep 23rd 2013 with 5 Comments

Geometry R. Unit 12 Coordinate Geometry. Day Classwork Day Homework Wednesday 3/7 Thursday 3/8 Friday 3/9

SketchUp. SketchUp. Google SketchUp. Using SketchUp. The Tool Set

Problem Set 4: Streams and Lazy Evaluation

Parametric Modeling. With. Autodesk Inventor. Randy H. Shih. Oregon Institute of Technology SDC PUBLICATIONS

Web-Friendly Sites. Planning & Design 1

SOFTWARE SKILLS BUILDERS

Dice in Google SketchUp

Using Microsoft Word. Tables

Paint/Draw Tools. Foreground color. Free-form select. Select. Eraser/Color Eraser. Fill Color. Color Picker. Magnify. Pencil. Brush.

Math 125 Little Book Homework Chapters 7, 10, 11, and 12

Assignment 3 Functions, Graphics, and Decomposition

Create a unit using United Streaming and PowerPoint. Materials: Microsoft PowerPoint, Internet access, United Streaming account

Unit 8 Geometry I-1. Teacher s Guide for Workbook 8.1 COPYRIGHT 2010 JUMP MATH: NOT TO BE COPIED

Lesson 5: Definition of Rotation and Basic Properties

CT5510: Computer Graphics. Transformation BOCHANG MOON

ACT Math test Plane Geometry Review

In this lesson, you ll learn how to:

A Brief Introduction to Scheme (II)

Drawing Tools. Drawing a Rectangle

ME009 Engineering Graphics and Design CAD 1. 1 Create a new part. Click. New Bar. 2 Click the Tutorial tab. 3 Select the Part icon. 4 Click OK.

CHEM /12/01 INSTRUCTIONS FOR AB-INITIO CALCULATIONS Prepared by: Dr. Cynthia HARWOOD, Ahu AKIN, Prof. Tim KEIDERLING

ACT SparkNotes Test Prep: Plane Geometry

Learning Microsoft Word By Greg Bowden. Chapter 10. Drawing Tools. Guided Computer Tutorials

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

STANDARDS OF LEARNING CONTENT REVIEW NOTES GEOMETRY. 3 rd Nine Weeks,

SOLIDWORKS: Lesson 1 - Basics and Modeling. Introduction to Robotics

Unit 5: Transformations in the Coordinate Plane

Part 1: Basics. Page Sorter:

The Rectangular Problem

CECOS University Department of Electrical Engineering. Wave Propagation and Antennas LAB # 1

University of Virginia, Department of Computer Science cs150: Computer Science Spring 2007 Out: 5 February 2007 Problem Set 3: L-System Fractals

Happy Haunting Halloween

CSE 167: Lecture #15: Procedural Modeling. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

Copyright and License

Shape & Space Part C: Transformations

Design and Print Instruction Manual

GEOMETRY REVIEW PACKET

Expression Design Lab Exercises

On the Web sun.com/aboutsun/comm_invest STAROFFICE 8 DRAW

Let s use a more formal definition. An angle is the union of two rays with a common end point.

Intro. Scheme Basics. scm> 5 5. scm>

GIMP TEXT EFFECTS. Text Effects: Outline Completed Project

Discussion 4. Data Abstraction and Sequences

MET 107 Drawing Tool (Shapes) Notes Day 3

Principles and Standards for School Mathematics. Content Standards. Process Standards. Emphasis across the Grades. Principles

LAB # 2 3D Modeling, Properties Commands & Attributes

SCRATCH MODULE 3: NUMBER CONVERSIONS

2. Decide which wall template you are going to use, the default or the tall wall, and load that template into your art program.

Topic. Section 4.1 (3, 4)

Clip Art and Graphics. Inserting Clip Art. Inserting Other Graphics. Creating Your Own Shapes. Formatting the Shape

FrontPage 98 Quick Guide. Copyright 2000 Peter Pappas. edteck press All rights reserved.

To build shapes from scratch, use the tools are the far right of the top tool bar. These

Honors Computer Science Python Mr. Clausen Program 7A, 7B

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

Lab1: Introductory and Setup Activities

Late Penalty: Late assignments will not be accepted.

Adjust model for 3D Printing. Direct modeling tools 13,0600,1489,1616(SP6)

CSE 167: Introduction to Computer Graphics Lecture #9: Visibility. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2018

Getting Started LESSON 1. Objectives: In this exercise you will perform the following tasks. Access MSC PATRAN and run a Session File.

GEOMETRY. slide #3. 6th Grade Math Unit 7. 6th Grade Unit 7: GEOMETRY. Name: Table of Contents. Area of Rectangles

We can use square dot paper to draw each view (top, front, and sides) of the three dimensional objects:

Geometry SEMESTER 1 SEMESTER 2

Junior Circle Meeting 9 Commutativity and Inverses. May 30, We are going to examine different ways to transform the square below:

Step 1: Create A New Photoshop Document

Adobe PageMaker Tutorial

Transcription:

University of Massachusetts Lowell 91.301: Organization of Programming Languages 91.301 Spring 2004 Problem Set 4 Henderson Picture Language Issued: Thursday, 4 March 2004 Due: Thursday, 11 March 2004; Late assignments only accepted until 4:30pm on Friday, 12 March 2004 Reading: Text (SICP 2nd Edition by Abelson & Sussman): Sections 2.2.4 In this assignment, you will work with the Peter Henderson s square-limit graphics design language, which is described in Section 2.2.4 of the textbook. Before beginning work on this programming assignment, you should review that section. The goal of this problem set is to reinforce ideas about data abstraction and higher-order procedures, and to emphasize the expressive power that derives from appropriate primitives, means of combination, and means of abstraction. 1 Section 1 of this handout reviews the language, as presented in the text (mostly). You will need to study this in order to complete the exercises. Section 2 gives the lab assignment, which includes an optional design contest. Compatibility This problem set must be done in DrScheme (my apologies to the MIT Scheme users). Use the Textual (MzScheme, includes R5RS) language in DrScheme. 1. The Square-Limit Language Remember that the key idea in the square-limit language is to use painter procedures that take frames as inputs and paint images that are scaled to fit the frames. Basic data structures Vectors are represented as pairs of numbers. (define make-vect cons) (define vector-xcor car) (define vector-ycor cdr) Here are the operations of vector addition, subtraction, and scaling a vector by a number: 1 This problem set was developed by Hal Abelson, based upon work by Peter Henderson ( Functional Geometry, in Proc. ACM Conference on Lisp and Functional Programming, 1982). The image display code was designed and implemented by Daniel Coore. The adaption of the image display code to DrScheme was done by Michael Sperber, with small modifications and repackaging by Allyn Dimock. The problem set was downloaded from the MIT Press web site and modified by Holly Yanco and Allyn Dimock.

2 91.301 (define (vector-add v1 v2) (make-vect (+ (vector-xcor v1) (vector-xcor v2)) (+ (vector-ycor v1) (vector-ycor v2)))) (define (vector-sub v1 v2) (vector-add v1 (vector-scale -1 v2))) (define (vector-scale x v) (make-vect (* x (vector-xcor v)) (* x (vector-ycor v)))) A pair of vectors determines a directed line segment the segment running from the endpoint of the first vector to the endpoint of the second vector: (define make-segment cons) (define segment-start car) (define segment-end cdr) Frames A frame is represented by three vectors: an origin and two edge vectors. (define (make-frame origin edge1 edge2) (list frame origin edge1 edge2)) (define frame-origin cadr) (define frame-edge1 caddr) (define frame-edge2 cadddr) The frame s origin is given as a vector with respect to the origin of the graphics-window coordinate system. The edge vectors specify the offsets of the corners of the frame from the origin of the frame. If the edges are perpendicular, the frame will be a rectangle; otherwise it will be a more general parallelogram. Figure 2.15 on page 135 of the textbook shows a frame and its associated vectors. Each frame determines a system of frame coordinates (x, y) where(0, 0) is the origin of the frame, x represents the displacement along the first edge (as a fraction of the length of the edge) and y is the displacement along the second edge. For example, the origin of the frame has frame coordinates (0, 0) and the vertex diagonally opposite the origin has frame coordinates (1, 1). Another way to express this idea is to say that each frame has an associated frame coordinate map that transforms the frame coordinates of a point into the Cartesian plane coordinates of the point. That is, (x, y) gets mapped onto the Cartesian coordinates of the point given by the vector sum Origin(Frame) + x Edge 1 (Frame) + y Edge 2 (Frame) We can represent the frame coordinate map by the following procedure: (define (frame-coord-map frame) (lambda (point-in-frame-coords) (vector-add (frame-origin frame) (vector-add (vector-scale (vector-xcor point-in-frame-coords) (frame-edge1 frame)) (vector-scale (vector-ycor point-in-frame-coords) (frame-edge2 frame)))))) For example, ((frame-coord-map a-frame) (make-vect 0 0)) will return the same value as (frame-origin a-frame). The procedure make-relative-frame provides a convenient way to transform frames. Given a frame and three points origin, corner1, andcorner2 (expressed in frame coordinates), it returns a new frame with those corners: Problem Set 4 91.301 Spring 2004

91.301 3 (define (make-relative-frame origin corner1 corner2) (lambda (frame) (let ((m (frame-coord-map frame))) (let ((new-origin (m origin))) (make-frame new-origin (vector-sub (m corner1) new-origin) (vector-sub (m corner2) new-origin)))))) For example, (make-relative-frame (make-vect 0.5 0.5) (make-vect 1 0.5) (make-vect 0.5 1)) returns the procedure that transforms a frame into the upper-right quarter of the frame. 2 Painters As described in Section 2.2.4 of the textbook, a painter is a procedure that, given a frame as argument, paints a picture in the frame. That is to say, if p is a painter and f is a frame, then evaluating (p f) will cause an image to appear in the frame. The image will be scaled and stretched to fit the frame. 3 The language you will be working with includes four ways to create primitive painters. The simplest painters are created with number->painter, which takes a number as argument. These painters fill a frame with a solid shade of gray. The number specifies a gray level: 0 is black, 255 is white, and numbers in between are increasingly lighter shades of gray. Here are some examples: (define black (number->painter 0)) (define white (number->painter 255)) (define gray (number->painter 150)) You can also specify a painter using procedure->painter, which takes a procedure as argument. procedure determines a gray level (0 to 255) as a function of (x, y) position, for example: The (define diagonal-shading (procedure->painter (lambda (x y) (* 100 (+ x y))))) The x and y coordinates run from 0 to 1 and specify the fraction that each point is offset from the frame s origin along the frame s edges. (See Figure 2.15 in the textbook.) Thus, the frame is filled out by the set of points (x, y) such that 0 x 1and0 y 1. (The DrScheme version has some rounding error that causes range of displayable points (x, y) to be in the range 0 x<1and0 y<1 in the outermost frame. Any point that is mapped to within a pixel of 1 in the outermost frame is not displayed.) A third kind of painter is created by segments->painter, which takes a list of line segments as argument. This paints the line drawing specified by the list segments. The (x, y) coordinates of the line segments are specified as above. For example, you can make the Z shape shown in Figure 1 as (define mark-of-zorro (let ((v1 (make-vect.1.9)) (v2 (make-vect.8.9)) (v3 (make-vect.1.2)) (v4 (make-vect.9.3))) (segments->painter (list (make-segment v1 v2) (make-segment v2 v3) (make-segment v3 v4))))) The final way to create a primitive painter is from a stored image. The procedure load-painter uses an image from the OPL image collection to create a painter. Images are stored in 128x128 grayscale.gif format. (define einstein (load-painter "einstein"))

4 91.301 Figure 1: Examples of primitive painters: mark-of-zorro and einstein. will paint an image of Albert Einstein. Transforming and combining painters We can transform a painter to produce a new painter which, when given a frame, calls the original painter on the transformed frame. For example, if p is a painter and f is a frame, then (p ((make-frame-relative (make-vect 0.5 0.5) (make-vect 1 0.5) (make-vect 0.5 1)) f)) will paint in the corner of the frame farthest from the origin. We can abstract this idea with the following procedure: (define (transform-painter origin corner1 corner2) (lambda (painter) (compose painter (make-relative-frame origin corner1 corner2)))) Note: The definition of transform-painter differs from the one in the book on page 138. Look at the definition of the procedure in the book and the one listed above. Convince yourself that they do the same thing. Calling transform-painter with an origin and two corners, returns a procedure that transforms a painter into one that paints relative to a new frame with the specified origin and corners. For example, we could define: (define (shrink-to-upper-right painter) ((transform-painter (make-vect 0.5 0.5) (make-vect 1 0.5) (make-vect 0.5 1)) painter)) Note that this can be written equivalently as (define shrink-to-upper-right (transform-painter (make-vect 0.5 0.5) (make-vect 1 0.5) (make-vect 0.5 1))) Other transformed frames will flip images horizontally: (define flip-horiz (transform-painter (make-vect 1 0) (make-vect 0 0) (make-vect 1 1))) 2 Actually into the corner of a frame farthest from the frame s origin: thus to the upper-right if the frame s origin is in the lower-left. 3 Actually, painting a picture requires some extra administrative work. Always use paint to paint a picture. Problem Set 4 91.301 Spring 2004

91.301 5 or rotate images counterclockwise by 90 degrees: (define rotate90 (transform-painter (make-vect 1 0) (make-vect 1 1) (make-vect 0 0))) By repeating rotations, we can create painters whose images are rotated through 180 or 270 degrees: (define rotate180 (repeated rotate90 2)) (define rotate270 (repeated rotate90 3)) We can combine the results of two painters a single frame by calling each painter on the frame: (define (superpose painter1 painter2) (lambda (frame) (painter1 frame) (painter2 frame))) The effect is only interesting if the second painter has some transparant areas where the first painter can show through. To draw one image beside another, we combine one in the left half of the frame with one in the right half of the frame: (define (beside painter1 painter2) (let ((split-point (make-vect.5 0))) (superpose ((transform-painter zero-vector split-point (make-vect 0 1)) painter1) ((transform-painter split-point (make-vect 1 0) (make-vect.5 1)) painter2)))) We can also define painters that combine painters vertically, by using rotate together with beside. The painter produced by below shows the image for painter1 below the image for painter2: (define (below painter1 painter2) (rotate270 (beside (rotate90 painter2) (rotate90 painter1)))) 2. Exercises In DrScheme, set the language to Textual(MzScheme, includes R5RS). Copy all files for the assignment into a single directory. Typing drscheme ps4 ans.ss in linux or executing ps4 ans.ss in Dr. Scheme on another platform loads files containing the procedures described in Section 1. You will not need to modify any of these. Fill out your code and (commented out) written answers in ps4 ans.ss. To paint a picture, use the procedure paint. For example, to paint a graphic image, (paint einstein) will show a picture of Albert Einstein in the interaction window. To print a picture: You can print out the entire contents of your interaction window using File -> Print Interactions. What seems to work best for printing is to use paint-lo-res rather then paint for painting and have as little as possible other material in the interactions buffer. When selecting print parameters you can then print to a file with Scales=3.0, Translations=0.0.

6 91.301 Exercise 1 Answer the following exercise on paper. Do not actually make these changes to the system. In the square-limit language, a frame is represented as a list of four things the symbol frame followed by the origin and the two edge vectors. 1. Pick some values for the coordinates of origin and edge vectors and draw the box-and-pointer structure for the resulting frame. 2. Suppose we change the representation of frames and represent them instead as a list of three vectors the origin and the two edges without including the symbol frame. Give the new definitions of make-frame, frame-origin, frame-edge1, andframe-edge2 for this representation. In addition to changing these constructors and selectors, what other changes to the implementation of the square-limit language are required in order to use this new representation? 3. Why might it be useful to include the symbol frame as part of the representation of frames? Exercise 2 Describe the patterns drawn by (procedure->painter (lambda (x y) (* x y))) (procedure->painter (lambda (x y) (* 255 x y))) (procedure->painter (lambda (x y) (* 255 x))) Try answering this problem without typing in the code. To check your answer, you could type (paint (procedure->painter (lambda (x y) (* x y)))) Exercise 3 Describe the effect of (transform-painter (make-vect.1.9) (make-vect 1.5 1) (make-vect.2 0)) Try this on paper first, before checking what the system does. Exercise 4 Make a collection of primitive painters to use in the rest of this lab. In addition to the ones predefined for you (and listed in section 1), define at least one new painter of each of the four primitive types: a uniform grey level made with number->painter, something defined with procedure->painter, a line-drawing made with segments->painter, and an image of your choice that is either loaded from the provided images, or created by you. Turn in a list of your definitions: my-number-painter, my-procedure-painter, my-segments-painter, my-image-painter. If you are creating your own image, name it my-pic.gif and include it in your electronic submission. Creating and loading your own images We have a rather small collection of images available. You may want to create your own images to use in this problem set. First, find an image. This can be an image that you have taken with a digital camera, something that you have scanned in (if you have access to a scanner), or a picture downloaded from a web site. 4 Next, you need to convert the image to the proper format for our system. Use an image manipulation tool that allows cropping, resizing, and format conversion. (For Linux, we recommend xv, available at 4 To save an image from a web site, click on the right mouse button when your cursor is over the image. Select the Save image as option from the menu bar, then specify a name and location for where you d like to save the image. Problem Set 4 91.301 Spring 2004

91.301 7 Figure 2: Painting created by (diamond einstein). http://linux.tucows.com/preview/10306.html). Crop to get a (reasonably) square region. (In Linux: Mouse1 and drag in xv, then right-click to open the xv controls window.) Reset image size to 128x128 pixels (In Linux: Image Size > Set Size, enter 128x128 and click Ok in xv.) Save as a greyscale.gif file as my-pic.gif (color.gifs will not work with this problem set, dithered b/w.gifs have not been tested with this problem set). If you saved your picture as my-pic.gif you should be able, in DrScheme with the problem set loaded, type (paint (load-painter "my-pic")) in the interaction window and have the picture displayed. Exercise 5 Experiment with some combinations of your primitive painters, using beside, below, superpose, flips, and rotations, to get a feel for how these means of combination work. You needn t turn in anything for this exercise. Exercise 6 The diamond of a frame is defined to be the smaller frame created by joining the midpoints of the original frame s sides. Define a procedure diamond that transforms a painter into one that paints its image in the diamond of the specified frame, as shown in Figure 2. Try some examples, and turn in a listing of your procedure. Exercise 7 The diamond transformation has the property that, if you start with a square frame, the diamond frame is still square (although rotated). Define a procedure make-non-square-diamond which takes 3 parameters: the first is the y coordinate of the origin (the x coordinate of the origin will always be 0); the second is the x coordinate of edge1 (y will always be 0); and the third is the x coordinate of edge2 (y will always be 1). Use your procedure make-non-square-diamond to define non-square-diamond-1 with the first parameter equal to 0.2, the second equal to 0.1 and the third equal to 0.9. Write at least one other transform that uses make-non-square-diamond to define it. Try your transformation(s) on some images to get some nice effects. Turn in a listing of your procedures. Exercise 8 The following recursive right-split procedure was demonstrated in lecture: (define (right-split painter n) (if (= n 0) painter (let ((smaller (right-split painter (- n 1)))) (beside painter (below smaller smaller)))))

8 91.301 (up-split mark-of-zorro 4) (up-split einstein 4) Figure 3: The up-split procedure places a picture below two (recursively) up-split copies of itself. Try this with some of the painters you ve previously defined, both primitives and combined ones. Now define an analogous up-split procedure as shown in Figure 3. Make sure to test it on a variety of painters. Turn in a listing of your procedure. (In defining your procedure, remember that (below painter1 painter2) produces painter1 below painter2.) Exercise 9 Right-split and up-split are both examples of a common pattern that begins with a means of combining two painters and applies this over and over in a recursive pattern. We can capture this idea in a procedure called keep-combining, which takes as argument a combiner (which combines two painters). For instance, we should be able to give an alternative definition of right-split as (define new-right-split (keep-combining (lambda (p1 p2) (beside p1 (below p2 p2))))) Complete the following definition of keep-combining: (define (keep-combining combine-2) ;; combine-2 = (lambda (painter1 painter2)...) (lambda (painter n) ((repeated fill in missing expression n) painter))) Show that you can indeed define right-split using your procedure, and give an analogous definition of up-split. Name your new procedures right-split2 and up-split2. Exercise 10 Once you have keep-combining, you can use it to define lots of recursive means of combination. Here are three examples: (define nest-diamonds (keep-combining (lambda (p1 p2) (superpose p1 (diamond p2))))) (nest-diamonds diagonal-shading 4) Problem Set 4 91.301 Spring 2004

91.301 9 (define new-comb (keep-combining (lambda (p1 p2) (square-limit (below p1 p2) 2)))) (new-comb mark-of-zorro 1) (define mix-with-einstein (keep-combining (lambda (p1 p2) (below (beside p1 einstein) (beside p2 p2))))) (mix-with-einstein logo 3) Invent some variations of your own. Turn in the code: procedure painter-12a, optionally painter-12b,... Exercise 11 The procedures you have implemented give you a wide choice of things to experiment with. Invent some new means of combination, both simple ones like beside and complex higher-order ones like keep-combining and square-limit, and see what kinds of interesting images you can create. Turn in the code of painters for one or two images: procedure painter-13a, optionally painter-13b,... Contest (Optional) Hopefully, you generated some interesting designs in doing this assignment. If you wish, you can enter printouts of your best designs in the 91.301 design contest. Turn in your design collection together with your homework, but stapled separately, and make sure your name is on the work. For each design, show the expression you used to generate it. Designs will be judged by the OPL staff and other paragons of design expertise, and fabulous prizes will be awarded in lecture. There is a limit of five entries per student. Make sure to turn in not only the pictures, but also the procedure(s) that generated them.