Navigation:

5.1. Render grid as image
    5.1.1. List to image
    5.1.2. Rectangle to image
    5.1.3. Matrix to image
5.2. Color a path
5.3. Display a path
5.4. Display path with score
CMSC 15100: Lab 4 Go back to the main lab

You will need to use the image.ss teachpack for this section.

5.1. Render the grid as an image

The built-in function text takes a string, a font size, and a color, and renders the string as an image in the given font size and color. For example, (text "K" 23 'red) gives

5.1.1. Convert a list of strings to an image

Write a function list->image: list list -> image which takes a list of strings and list of colors. It produces an image consisting of the strings on the same row, separated by 30 points, with the ith string being the ith color in the list of colors. Recall the overlay/xy function from the first lab.

(list->image (list "A" "E" "I" "O" "U") (list 'red 'green 'blue 'yellow 'purple)) should produce

5.1.2. Convert a rectangle of strings to an image

A "rectangle" of size mxn is an informal term for a list of m lists, each of length n. Define a function rectangle->image: rectangle rectangle -> image which takes a rectangle of strings and a rectangle of colors. It renders an image by creating an image from the first list in the rectangle (using list->image), and then an image from the second list in the rectangle which it places 30 points below the first, and so on. For example, (rectangle->image (list (list "T" "I" "C") (list "T" "A" "C") (list "T" "O" "E")) (list (list 'red 'red 'red) (list 'blue 'blue 'blue) (list 'green 'green 'green))) should give

5.1.3. Convert a matrix of strings (aka, the character grid) to an image

A rectangle can be converted to a matrix and vice-versa using the in-built functions matrix->rectangle and rectangle->matrix. These functions are very handy since rectangles and matrices are convenient for different purposes. Since the former is a list, it is great for things like recursion, and the latter is useful when we need to index an element, like extracting or setting the value at position (i, j).

Copy and paste the following code into your definitions window. This defines a 4x4 matrix of colors, all of which are 'red.

(define (make-list k v)
    (cond [(= k 0) '()]
          [else (append (list v) (make-list (- k 1) v))]))

(define colormatrix
    (make-matrix 4 4 (make-list 16 'red)))
Some installations of DrScheme already have the make-list function defined. In that case, only copy the colormatrix definition.

Now write a function matrix->image: matrix matrix -> image which renders an image given a matrix of strings and a matrix of colors. You will have to use rectangle->image and matrix->rectangle. Calling (matrix->image grid colormatrix) should render our puzzle character grid as below:

5.2. Set colors according to a path

Define a function colorpath: list matrix -> matrix which takes a path (list of cells) and a matrix of colors. It returns the same matrix with all the positions in the path set to 'green. You will have to use matrix-set. Calling (colorpath (list (make-cell 1 3) (make-cell 1 2) (make-cell 0 2)) colormatrix) should give

5.3. Display input path

Define a function colorgrid: path matrix matrix -> image which takes a path, a matrix of strings, and a matrix of colors. It alters the matrix of colors according to the path, using colorpath, and returns an image of the grid colored by the altered matrix. This is what (colorgrid (list (make-cell 1 3) (make-cell 1 2) (make-cell 0 2)) grid colormatrix) should look like:

5.4. Display input path with score

Use evalpath and pathscore to define a function colorgridandscore: path matrix matrix -> image. it takes the same input types as the previous function, and renders an image which consists of the colored grid as above, as well a display of the string found by the path and the score alloted to it. You will need to make use of the in-built number->string. Example:

> (colorgridandscore (list (make-cell 1 3) (make-cell 1 2) (make-cell 0 2)) grid colormatrix)



> (colorgridandscore (list (make-cell 1 2) (make-cell 1 1) (make-cell 2 0) (make-cell 1 0) (make-cell 0 1)) grid colormatrix)



> (colorgridandscore (list (make-cell 3 1) (make-cell 1 1) (make-cell 1 2)) grid colormatrix)