;;;; WeScheme Starter Code: Chapters 3, 7, and 15.

#|
;; Chapter 1 Images - commented out because they load slowly.

(define pic:bloch
  (bitmap/url "http://picturingprograms.com/pictures/bloch.jpg"))
(define pic:hieroglyphics
  (bitmap/url "http://picturingprograms.com/pictures/hieroglyphics.png"))
(define pic:hacker
  (bitmap/url "http://picturingprograms.com/pictures/mad_hacker.png"))
(define pic:book
  (bitmap/url "http://picturingprograms.com/pictures/qbook.png"))
(define pic:stick-figure
  (bitmap/url "http://picturingprograms.com/pictures/stick-figure.png"))
(define pic:scheme-logo
  (bitmap/url "http://picturingprograms.com/pictures/schemelogo.png"))
(define pic:calendar
  (bitmap/url "http://picturingprograms.com/pictures/calendar.png"))
(define pic:train-engine
  (bitmap/url "http://picturingprograms.com/pictures/train_engine.png"))
(define pic:train-car
  (bitmap/url "http://picturingprograms.com/pictures/train_car.png"))
|#

;; Chapter 1 Functions: rotate-cw rotate-ccw rotate-180
;; This section defines simple functions used in Chapter 1
;; rotate-cw
;; rotate-ccw
;; rotate-180

(define (rotate-cw x)
  (rotate -90 x))
(define (rotate-ccw x)
  (rotate 90 x))
(define (rotate-180 x)
  (rotate 180 x))

;; Chapter 3 Functions for WeScheme

;; Chapter 3: crop-left crop-right crop-top crop-bottom
(define (crop-left img x)
  (crop x 0 (- (image-width img) x) (image-height img) img))
(define (crop-right img x)
  (crop 0 0 (- (image-width img) x) (image-height img) img))
(define (crop-top img y)
  (crop 0 y (image-width img) (- (image-height img) y) img))
(define (crop-bottom img y)
  (crop 0 0 (image-width img) (- (image-height img) y) img))

;; Chapter 3: colorize

(define (colorize the-image-color)
  (if (color? the-image-color)
      the-image-color
      (name->color the-image-color)))

;; Chapter 3: overlay/offset

(define (overlay/offset img x y bg)
  (overlay/xy img
              (+ x (- (image-width img)
                      (image-width bg)))
              (+ y (- (image-height img)
                      (image-height bg)))
              bg))

;; Chapter 7: real->int

(define (real->int x)
  (exact->inexact (round x)))

;; Chapter 7 and 15: build-image

#|
This file contains
    build-image-wy : number(width) number(height) function (x y -> color)
                     -> image

Give it a width, height, and color-determining function, and it
makes a pixel-art canvas of colored dots.

|#

(define DOT-RADIUS 3)

(define (grid width height gap)
  (local [(define delta (add1 DOT-RADIUS))
          (define (row-make y)
            (map (lambda (x) (make-posn x y))
                 (range delta
                        (- width delta)
                        gap)))]
    (apply append (map row-make (range delta
                                       (- height delta)
                                       gap)))))

(define (color-image radius color-f pts bg)
  (if (empty? pts)
      bg
      (local [(define p (first pts))
              (define x (posn-x p))
              (define y (posn-y p))]
        (color-image radius color-f (rest pts)
                     (place-image (circle radius "solid" (color-f x y))
                                  x y
                                  bg)))))

(define (build-image-wy width height color-f)
  (color-image DOT-RADIUS
               color-f
               (grid width height (* 2 DOT-RADIUS))
               (empty-scene width height)))

#|
;; Example 1: A random mix of different shades of red.

(define (random-col-f x y)
  (local [(define v (random 5))]
    (cond [(= 0 v) (make-color 250 0 0)]
          [(= 1 v) (make-color 200 0 0)]
          [(= 2 v) (make-color 150 0 0)]
          [(= 3 v) (make-color 100 0 0)]
          [else "black"])))

(build-image-wy 300 200 random-col-f)

;; Example 2: Three pixel art circles.

(define (dist x0 y0 x1 y1)
  (sqrt
   (+
    (sqr (- x0 x1))
    (sqr (- y0 y1)))))

(define (circ-color-f x y)
  (cond [(< (dist x y 110 120) 60) "purple"]
        [(< (dist x y 40 130) 20) "blue"]
        [(< (dist x y 220 90) 40) "orange"]
        [else "white"]))

(build-image-wy 300 200 circ-color-f)
|#        

(define build-image build-image-wy)

#| ;; Images - get rid of this line to show images.
pic:bloch
pic:hieroglyphics
pic:hacker
pic:book
pic:stick-figure
pic:scheme-logo
pic:calendar
pic:train-engine  ; not a variable in DrRacket
pic:train-car     ; not a variable in DrRacket
|# ;; Get rid of this line to show images.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Last modified December 5, 2025: WeScheme starter: build-image. (38f5f55)