;; 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 09local) (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: Programming at level 2 (place-image (text "GO" 36 "red") 450 450 (place-image (text "Just Visiting" 36 "red") 50 450 (place-image (text "Free Parking" 36 "red") 50 50 (place-image (text "Go to JAIL" 36 "red") 450 50 (empty-scene 500 500))))) ;; Let's fit text in corners. ;; Need height and width (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)) #| +---------------------------------- | | [This is the * centered text] | ^ |---------------| what is the x coordinate of the center? |# (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) (place-image (text name 36 "red") (- (image-width board) (+ border (quotient (image-width (text name 36 "red")) 2))) (+ border (quotient (image-height (text name 36 "red")) 2)) board)) (check-expect (place-ur-corner "Go to JAIL" blank-board) (place-image (text "Go to JAIL" 36 "red") 395 43 (empty-scene 500 500))) (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 ; half : integer -> integer ; to return the nearest integer to half the given integer (define (half n) (quotient n 2))] (place-image txt (- bw (+ border (half tw))) (+ border (half th)) board))) (check-expect (+place-ur-corner "Go to JAIL" blank-board) (place-ur-corner "Go to JAIL" blank-board)) ;; What are the rules for UL, LL, LR? ;;;;; let's do another (define (dog-to-food dog food) (place-image (circle 5 "outline" "black") (- (posn-x dog) (* .10 (- (posn-x dog) (posn-x food)))) (- (posn-y dog) (* .10 (- (posn-y dog) (posn-y food)))) (place-image (circle 4 "solid" "orange") (posn-x food) (posn-y food) (place-image (circle 9 "solid" "black") (posn-x dog) (posn-y dog) (scene+line (empty-scene 300 300) (posn-x dog) (posn-y dog) (posn-x food) (posn-y food) "green"))))) (check-expect (dog-to-food (make-posn 200 50) (make-posn 30 286)) (place-image (circle 5 "outline" "black") (- 200 (* .10 (- 200 30))) (- 50 (* .10 (- 50 286))) (place-image (circle 4 "solid" "orange") 30 286 (place-image (circle 9 "solid" "black") 200 50 (scene+line (empty-scene 300 300) 200 50 30 286 "green"))))) ;;;;;;;; More than just clean laundry ;; 1LOS (non-empty list of strings) is one of: ;; -- (cons string empty) ;; -- (cons string 1LON) ;; longest : 1LOS -> string ;; find the longest string in the list (define (longest ss) (cond [(empty? (rest ss)) (first ss)] [(cons? (rest ss)) (cond [(< (string-length (first ss)) (string-length (longest (rest ss)))) (longest (rest ss))] [else (first ss)])])) (check-expect (longest '("Call" "me" "Ishmael")) "Ishmael") #| (time (longest (list "If" "you" "like" "you" "can" "call" "me" "Ishmael"))) (time (longest (list "My" "mother" "named" "me" "Sam" "and" "my" "father" "calls" "me" "Junior" "but" "if" "you" "like" "you" "can" "call" "me" "Ishmael"))) |#