(module tiles mzscheme (require (lib "../lang/image.ss")) (provide image-above image-beside reflect-vert reflect-horiz reflect-main-diag reflect-other-diag rotate-cw rotate-ccw rotate-180 ) ; image-union-background : image image => image ; Returns a white rectangle big enough to hold both images (define (image-union-background image1 image2) (filled-rect (max (image-width image1) (image-width image2)) (max (image-height image1) (image-height image2)) 'white)) ; image-union : image image => image ; Returns the union of the two images, WITHOUT clipping; second overrides first (define (image-union bottom top) (image+ (image+ (image-union-background bottom top) bottom) top)) ; image-translate : image int int => image (define (image-translate image dx dy) (offset-image+ (filled-rect (+ (image-width image) dx) (+ (image-height image) dy) 'white) dx dy image)) ; offset-image-union : image int int image => image (define (offset-image-union bottom dx dy top) (image-union bottom (image-translate top dx dy))) ; above : image image => image (define (image-above over under) (offset-image-union over 0 (image-height over) under)) ; beside : image image => image (define (image-beside left right) (offset-image-union left (image-width left) 0 right)) ; first-n : list n => list of length n (or less) (define (first-n L n) (cond [(null? L) ()] [(<= n 0) ()] [else (cons (car L) (first-n (cdr L) (- n 1)))])) ;"Examples of first-n:" ;(first-n empty 2) "should be" empty ;(first-n (list 'a) 2) "should be" (list 'a) ;(first-n (list 'a 'b 'c) 2) "should be" (list 'a 'b) ; rest-n : list n => list of length n smaller (or 0) (define (rest-n L n) (cond [(null? L) ()] [(<= n 0) L] [else (rest-n (cdr L) (- n 1))])) ;"Examples of rest-n:" ;(rest-n empty 2) "should be" empty ;(rest-n (list 'a) 2) "should be" empty ;(rest-n (list 'a 'b 'c) 2) "should be" (list 'c) ; slice : list n => list-of-lists ; Assumes n divides the length of the list (define (slice L width) (cond [(null? L) ()] [else (cons (first-n L width) (slice (rest-n L width) width))])) ;"Examples of slice:" ;(slice (list 1 2 3 4 5 6) 2) "should be" (list (list 1 2) (list 3 4) (list 5 6)) ; unslice : list-of-lists => list (define (unslice lists) (apply append lists)) ;"Example of unslice:" ;(unslice (list (list 1 2) (list 3 4) (list 5))) "should be" (list 1 2 3 4 5) ; reflect-horiz : image => image (define (reflect-horiz picture) (color-list->image (unslice (map reverse (slice (image->color-list picture) (image-width picture)))) (image-width picture) (image-height picture))) ; reflect-vert : image => image (define (reflect-vert picture) (color-list->image (unslice (reverse (slice (image->color-list picture) (image-width picture)))) (image-width picture) (image-height picture))) ; ncons-each : list => list-of-one-element-lists (define (ncons-each L) (map list L)) ;"Examples of ncons-each:" ;(ncons-each empty) "should be" empty ;(ncons-each (list 'a)) "should be" (list (list 'a)) ;(ncons-each (list 'a 'b)) "should be" (list (list 'a) (list 'b)) ; transpose : list-of-lists => list-of-lists ; Assumes all lists are the same length ; Assumes there's at least one row and at least one column (define (transpose rows) (apply map (cons list rows))) ;"Exampls of transpose:" ;(transpose (list (list 'a))) "should be" (list (list 'a)) ;(transpose (list (list 'a 'b))) "should be" (list (list 'a) (list 'b)) ;(transpose (list (list 'a) (list 'b))) "should be" (list (list 'a 'b)) ;(transpose (list (list 'a 'b) (list 'c 'd))) "should be" (list (list 'a 'c) (list 'b 'd)) ; reflect-main-diag : image => image (define (reflect-main-diag picture) (color-list->image (unslice (transpose (slice (image->color-list picture) (image-width picture)))) (image-height picture) (image-width picture))) ; reflect-other-diag : image => image (define (reflect-other-diag picture) ; (reflect-vert ; (reflect-main-diag ; (reflect-vert picture)))) (color-list->image (unslice (reverse (transpose (reverse (slice (image->color-list picture) (image-width picture)))))) (image-height picture) (image-width picture))) ; rotate-cw : image => image (define (rotate-cw picture) ; (reflect-main-diag (reflect-vert picture))) (color-list->image (unslice (transpose (reverse (slice (image->color-list picture) (image-width picture))))) (image-height picture) (image-width picture))) ; rotate-ccw : image => image (define (rotate-ccw picture) ; (reflect-vert (reflect-main-diag picture))) (color-list->image (unslice (reverse (transpose (slice (image->color-list picture) (image-width picture))))) (image-height picture) (image-width picture))) ; rotate-180 : image => image (define (rotate-180 picture) ; (rotate-cw (rotate-cw picture)) ; (reflect-vert (reflect-horiz picture)) (color-list->image (unslice (reverse (map reverse (slice (image->color-list picture) (image-width picture))))) (image-width picture) (image-height picture))) )