1 About This Teachpack
2 Installation
3 Functions from image.rkt and universe.rkt
4 New image functions
rotate-cw
rotate-ccw
rotate-180
crop-top
crop-bottom
crop-left
crop-right
show-it
reflect-vert
reflect-horiz
5 Input and Output
with-input-from-string
with-output-to-string
with-input-from-file
with-output-to-file
with-input-from-url
with-io-strings
Version: 5.0.0.1

Picturing Programs Teachpack

Stephen Bloch

 (require (planet sbloch/picturing-programs/picturing-programs))

1 About This Teachpack

Provides a variety of functions for combining and manipulating images and running interactive animations. It’s intended to be used with the textbook Picturing Programs.

2 Installation

If you’re reading this, you’ve probably already installed the teachpack successfully, but if you need to install it on a different machine, ...
  1. start DrScheme

  2. switch languages to “Use the language declared in the source” and click “Run”

  3. in the Interactions pane, type
      (require (planet sbloch/picturing-programs:2))

  4. after a few seconds, you should see the message

    Wrote file “picturing-programs.rkt” to installed-teachpacks directory.

  5. switch languages back to one of the HtDP languages, like Beginning Student

  6. either
    • in the Definitions pane, type
        (require installed-teachpacks/picturing-programs)
      or

    • from the Language menu, choose "Add Teachpack..." and select "picturing-programs.rkt"

  7. click "Run"

3 Functions from image.rkt and universe.rkt

This package includes all of the image teachpack and and the universe teachpack, so if you’re using this teachpack, don’t also load either of those. See the above links for how to use those teachpacks.

It also supersedes the older tiles and sb-world teachpacks, so if you have those, don’t load them either; use this instead.

This package also provides the following additional functions:

4 New image functions

(rotate-cw img)  image?
  img : image?
Rotates an image 90 degrees clockwise.

(rotate-ccw img)  image?
  img : image?
Rotates an image 90 degrees counterclockwise.

(rotate-180 img)  image?
  img : image?
Rotates an image 180 degrees around its center.

(crop-top img pixels)  image?
  img : image?
  pixels : natural-number/c
Chops off the specified number of pixels from the top of the image.

(crop-bottom img pixels)  image?
  img : image?
  pixels : natural-number/c
Chops off the specified number of pixels from the bottom of the image.

(crop-left img pixels)  image?
  img : image?
  pixels : natural-number/c
Chops off the specified number of pixels from the left side of the image.

(crop-right img pixels)  image?
  img : image?
  pixels : natural-number/c
Chops off the specified number of pixels from the right side of the image.

(show-it img)  image?
  img : image?
Returns the given image unaltered. Useful as a draw handler for animations whose model is an image.

(reflect-vert img)  image?
  img : image?
The same as flip-vertical; retained for compatibility.

(reflect-horiz img)  image?
  img : image?
The same as flip-horizontal; retained for compatibility.

5 Input and Output

This teachpack also provides five functions to help in testing I/O functions (in Advanced Student language; ignore this section if you’re in a Beginner or Intermediate language):

(with-input-from-string input thunk)  any/c
  input : string?
  thunk : (-> any/c)
Calls thunk, which presumably uses read, in such a way that read reads from input rather than from the keyboard.

(with-output-to-string thunk)  string?
  thunk : (-> any/c)
Calls thunk, which presumably uses display, print, write, and/or printf, in such a way that its output is accumlated into a string, which is then returned.

(with-input-from-file filename thunk)  any/c
  filename : string?
  thunk : (-> any/c)
Calls thunk, which presumably uses read, in such a way that read reads from the specified file rather than from the keyboard.

(with-output-to-file filename thunk)  any/c
  filename : string?
  thunk : (-> any/c)
Calls thunk, which presumably uses display, print, write, and/or printf, in such a way that its output is redirected into the specified file.

(with-input-from-url url thunk)  any/c
  url : string?
  thunk : (-> any/c)
{Calls thunk, which presumably uses read, in such a way that read reads from the HTML source of the Web page at the specified URL rather than from the keyboard.}

(with-io-strings input thunk)  string?
  input : string?
  thunk : (-> any/c)
Combines with-input-from-string and with-output-to-string: calls thunk with its input coming from input and accumulates its output into a string, which is returned. Especially useful for testing:
  (define (ask question)
    (begin (display question)
           (read)))
  (define (greet)
     (local [(define name (ask "What is your name?"))]
          (printf "Hello, ~a!" name)))
  (check-expect
     (with-io-strings "Steve" greet)
     "What is your name?Hello, Steve!")