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
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
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
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)Some installations of DrScheme already have the make-list function defined. In that case, only copy the colormatrix definition.
(cond [(= k 0) '()]
[else (append (list v) (make-list (- k 1) v))]))
(define colormatrix
(make-matrix 4 4 (make-list 16 'red)))
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:
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
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:
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)