Functional Problem Solving (CSC 151 2015S) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Taking Notes] [Rubric] - [Calendar]
Current: [Assignment] [EBoard am] [EBoard pm] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards am] [EBoards pm] [Labs] [Outlines] [Readings] - [Examples] [Handouts]
Reference: [Setup] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Davis (2013F)] [Rebelsky (2014F)] [Weinman (2014F)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Book Office Hours] - [Issue Tracker (Course)]
Overview
Admin
Do hshift and vshift work for images?
No.
Are there ways to find the width and height of an image?
Let's see ... we'll check the reference page. http://www.cs.grinnell.edu/~rebelsky/Courses/CSC151/2015S/reference/image-reference.html. The answer is yes.
How would you write (center-thing source target), which makes a copy
of source that is centered on target?
I'd start with the "get the center of a drawing" procedures we saw on the latest lab.
;;; Procedure:
;;; drawing-center-x
;;; Parameters:
;;; drawing, a drawing
;;; Purpose:
;;; Get the x coordinate of the center of a drawing
;;; Produces:
;;; x, a real number
;;; Preconditions:
;;; drawing has positive width and height.
;;; Postconditions:
;;; (- (drawing-right drawing) x) is approximately (- x (drawing-left drawing))
(define drawing-center-x
(lambda (drawing)
(+ (drawing-left drawing) (* 1/2 (drawing-width drawing)))))
;;; Procedure:
;;; drawing-center-y
;;; Parameters:
;;; drawing, a drawing
;;; Purpose:
;;; Get the y coordinate of the center of a drawing
;;; Produces:
;;; y, a real number
;;; Preconditions:
;;; drawing has positive width and height.
;;; Postconditions:
;;; (- (drawing-bottom drawing) y) is approximately (- y (drawing-top drawing))
(define drawing-center-y
(lambda (drawing)
(+ (drawing-top drawing) (* 1/2 (drawing-height drawing)))))
Next, I'd write a working version of
center-at. (I haven't documented it because the lab requires you to do so.)
(define center-at
(lambda (drawing x y)
(hshift-drawing
(- x (drawing-center-x drawing))
(vshift-drawing
(- y (drawing-center-y drawing))
drawing))))
Finally, I'd write
center-thingyusing these two procedures
(define center-thingy
(lambda (source target)
(center-at source
(drawing-center-x target)
(drawing-center-y target))))
What if you didn't want to use helper procedures?
It's essentially the same computation, just a bit harder to read.
(define center-thingy
(lambda (source target)
(drawing-hshift
(- (+ (drawing-left target)
(* 1/2 (drawing-width target)))
(+ (drawing-left source)
(* 1/2 (drawing-width source))))
(drawing-vshift
(- (+ (drawing-top target)
(* 1/2 (drawing-height target)))
(+ (drawing-top source)
(* 1/2 (drawing-height source))))
source))))
No, I haven't tested it yet.
On the last problem, there's the expression (max color ...). Can I
really compute maximum using a color like "red" rather than a number?
No.
How do we choose what to test?
You want enough that you are confident that the answer is correct.
Can we test abstract values?
No, you generally need to pick a particular input value (or set of input values) and make sure that it works (they work).
In the
center-at, I'd do some simple examples, but then I'd progress to asking whether it works when we shift left or right, up or down, to a point that is integral or rational, exact or inexact, using both simple and compound shapes, drawings with different colors, etc. ....We want a wide variety of tests to catch all the likely possible ways in which the procedure is used legally.
So how would we do these?
Example: Checking a case in which a compound shape shifts left and then up, to a point that is specified by two exact rational numbers.
First: Sketch on the board so that I can think about things
Next, make the shape
(define compound-shape
(drawing-hshift
2
(drawing-vshift
20
(drawing-scale
50
(drawing-group drawing-unit-circle
(drawing-scale 1/2 drawing unit-square))))))
I probably should have used my "make a square" and "make a rectangle" procedures.
Shifting it to the target point would be
(define example3 (center-at compound-shape 20/3 -7/2))
Now I'm ready to write the tests
(test-case "center of example 3 is correct"
(check-= (drawing-center-x example3) 20/3 1)
(check-= (drawing-center-y example3) -7/2 1))
(test-case "dimensions of example 3 are correct"
(check-= (drawing-width example3)
(drawing-width compound-shape)
1)
(check-= (drawing-height example3)
(drawing-height compound-shape)
1))
(test-case "characteristics of example 3 are correct"
(check-equal? (drawing-color example3)
(drawing-color compound-shape))
(check-equal? (drawing-type example3)
(drawing-type compound-shape)))
What does drawing-type do?
Gets the type of a drawing (ellipse or rectangle or group, right now)
What color is a compound drawing?
It appears that it's always identified as "black" (0)
Usually one "What does this procedure do?"
Example
(define proc
(lambda (drawing)
(* 1/2 (+ (drawing-left drawing)
(drawing-right drawing)))))
Usually one "Finish writing this procedure."
Example. Suppose
drawing-rightis not defined. How would you define it based on our other procedures?
(define drawing-right
(lambda (drawing)
Answer
(define drawing-right
(lambda (drawing)
(+ (drawing-left drawing)
(drawing-width drawing))))