;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-reader.ss" "lang")((modname 10local) (read-case-sensitive #t) (teachpacks ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp"))))) ; DEMO: Abstracting with functions #| ;; Let's fit text in corners. ------------------------------------------+ | [This is the * centered text] | ... | ^ | |<---------------->| | | ... what is the x coordinate of the center? |# (define txt-go (text "GO" 36 "red")) (define txt-jv (text "Just Visiting" 36 "red")) (define txt-fp (text "Free Parking" 36 "red")) (define txt-gj (text "Go to JAIL" 36 "red")) (define blank-board (empty-scene 500 500)) (define border 20) ;; place-ur-corner : string image -> image ;; return the given image with the given string overlaid in ;; the upper-right corner with a border, 36 pixels and red (define (place-ur-corner name board) (local [(define txt (text name 36 "red")) (define tw (image-width txt)) ; text width (define th (image-height txt)) ; text height (define bw (image-width board)) ; board width (define bh (image-height board))] ; board height (place-image txt (- bw (+ border (half tw))) (+ border (half th)) board))) ; half : integer -> integer ; to return the nearest integer to half the given integer (define (half n) (quotient n 2)) (define (near-edge board-size text-size) (+ border (half text-size))) (define (far-edge board-size text-size) (- board-size (+ border (half text-size)))) (define (middle-coordinate board-size text-size) (half board-size)) ;; place-on-rectangle : string image (number number -> number) (number number -> number) -> image ;; return the given image with the given string overlaid in ;; as placed by the `the-x` and `the-y` functions, 36 pixels and red (define (place-on-rectangle name board the-x the-y) (local [(define txt (text name 36 "red")) (define tw (image-width txt)) ; text width (define th (image-height txt)) ; text height (define bw (image-width board)) ; board width (define bh (image-height board))] ; board height (place-image txt (the-x bw tw) (the-y bh th) board))) (check-expect (place-on-rectangle "JAIL" blank-board far-edge near-edge) (place-ur-corner "JAIL" blank-board)) ;; by using `place-on-rectangle` with all combinations ;; of `near-edge`, `far-edge`, and `middle-coordinate`, ;; we get *nine* placements from the same function: ;; top left, top middle, top right, middle left, ;; center, middle right, bottom left, bottom middle, ;; bottom right.