Lab 3
This lab should be considered practice for hw8. We will grade it on completion. If you submit a file "lab3/lab3.rkt" to your repository by the end of lab today (6 pm) you will get full credit. If these seem easy and you want to jump right into the homework, go ahead. If you have trouble on the homework, do these first. They may help.
To run all your functions today, the following image is your building block.
The function that draws this square figure is called design. Here is the source code for design:![]()
(: bbt (-> Integer Image-Color Image)) ;; bbt means "black-backed triangle" ;; The resulting image is an nxn square. (define (bbt n c) (if (< n 0) (error "negative n") (overlay (triangle (quotient (* 2 n) 3) "solid" c) (square n "solid" "black")))) (: gbc (-> Integer Image-Color Image)) ;; gbc means "gray-backed circle" ;; The resulting image is an nxn square. (define (gbc n c) (if (< n 0) (error "negative n") (overlay (circle (quotient n 3) "solid" c) (square n "solid" "darkgray")))) (: design (-> Integer Image)) ;; Draws an asymmetrical design in an nxn square. (define (design n) (if (< n 0) (error "negative") (frame (above (beside (gbc (quotient n 2) "black") (bbt (quotient n 2) "ivory")) (beside (gbc (quotient n 2) "red") (gbc (quotient n 2) "darkred"))))))
Please copy and paste this code into your lab3 file. When you evaluate (design 64), you should see the square figure (containing three colored circles and one triangle) above. This pattern is designed to be asymmetrical so that you can easily identify how it has been flipped or rotated.
Here is a short list of some relevant operations that come with typed/2htdp/image, along with their types. You may need these in the exercises that follow.
empty-image : Image image-width : Image -> Integer image-height : Image -> Integer above : Image Image -> Image beside : Image Image -> Image beside/align : String Image Image -> Image ;; more about this function in shrinking-row below flip-vertical : Image -> Image flip-horizontal : Image -> Image scale : Positive-Real Image -> Image
Proceed by writing the functions specified in this section. In doing so, you will build the skills -- and a toolkit -- to reproduce the recursively constructed figure in homework 8 problem 1.
For all of the operations we ask you to write, you may assume the image arguments are squares (i.e., have equal width and height). This principle does not need to hold for whatever helper functions you write, of course.
Note that these functions are not strictly cumulative. That is, you won't necessarily need to call functions you've already written in later functions.
(: quarter (-> Image Image))
The function quarter
should consume a square image and
produce one whose side length is half as long. (The image produced has
one quarter the area of the image consumed.)
(: row (-> Integer Image Image))
The function row
should produce a row of n
images. For example, (row 4 (design 64))
If the number of images is 0, you should produce the empty-image.![]()
(: col (-> Integer Image Image))
The function col
should produce a column of n
images. For example, (col 3 (design 64))
(: grid (-> Integer Integer Image Image))
The function grid
should produce a grid of images of the
given dimensions. The first argument is the grid width, the second is
the height, and the third input is the image use.
For example, (grid 7 2 (design 64))
Where each square is (design 64).![]()
(: shrinking-row (-> Image Image))
The function shrinking-row
should produce a row of images
with the following property: the original square image (the one given
as the input) should be scaled down to 1/2 its original size, with
respect to side length (not area), at every step. Also, the total
width of the final image should equal twice the width of the
first design. For
example, (shrinking-row (design 64))
With the first square being 64 pixels across, the second 32, etc.![]()
Comments:
-
Use beside/align with first argument "bottom" to align
all the images along their bottom edges. Try this to see how this
works:
(beside/align "bottom" (square 50 "solid" "red") (square 30 "solid" "blue"))
- You should terminate your recursion according to the width of the input image. When the image gets too small to be seen (for example, when the width less or equal to one pixel), you should stop the recursion. In order to make the final image the correct width, you will have to include two copies of the last image. In the above example, 64 + 32 + 16 + 8 + 4 + 2 + 1 = 127. To make the final size correct, you must add another copy of design with size 1.
- If you begin with something that is not a multiple of two, the sizes should still work out correctly, as long as the initial input is an integer. For example: 30 + 15 + 7.5 + 3.75 + 1.875 + .9375 + .9375 = 60. The image-width function rounds to the nearest integer, so if you try (image-width (shrinkking-row (design 30))) you will get 60, but if you try (image-width (shrinking-row (design 8.3))) you will get 17.
(: bsr (-> Image Image))
bsr
stands for "bidirectional shrinking row" (for lack of
a better name). It's better explained as a picture than in
words. (bsr (design 64))
In this case, the image's sides are scaled 3/4 at every step. For this question, we won't check the exact width of the output image.![]()