PasteRack.org
Paste # 11302
2021-08-02 18:54:00

Fork as a new paste.

Paste viewed 4420 times.


Embed:

issue #61

  1. #lang racket
  2.  
  3. (require 2htdp/image
  4.          lang/posn)
  5.  
  6. (define COLOR-ON "white")
  7. (define COLOR-OFF "black")
  8.  
  9. (define hex-on (regular-polygon 60 6 "solid" COLOR-ON))
  10. (define hex-off (regular-polygon 60 6 "solid" COLOR-OFF))
  11.  
  12. (define half-on (place-image/align (rotate 30 hex-on)
  13.                    0 60 "center" "center"
  14.                    (rectangle 52 120 "outline" "transparent")))
  15.  
  16. (define half-off (place-image/align (rotate 30 hex-off)
  17.                    0 60 "center" "center"
  18.                    (rectangle 52 120 "outline" "transparent")))
  19.  
  20. (define split-hex (beside (rotate 180 half-on) half-off))
  21.  
  22. (define hex-options
  23.   (append
  24.    (list (rotate 90 hex-on))
  25.    (for/list ([i (in-range 6)])
  26.     (rotate (* i 60) split-hex))))
  27.  
  28. (define (make-row n)
  29.   (for/fold ([scene (rectangle (* 104 n) 120 "outline" "transparent")])
  30.             ([i (in-range n)])
  31.     (place-image (list-ref hex-options (random (length hex-options)))
  32.                  (+ (* i 104) (/ 104 2)) 60
  33.                  scene)))
  34.  
  35. (define (make-rows w n)
  36.   (for/list ([i (in-range n)])
  37.     (make-row w)))
  38.  
  39.  
  40. (define (scene-width rows)
  41.   (- (image-width (car rows)) (image-width split-hex)))
  42.  
  43. (define (scene-height rows)
  44.   (* (length rows) (/ (image-height split-hex) 2)))
  45.  
  46. (define (stack-rows rows)
  47.   (define-values (sum image)
  48.     (for/fold ([sum 0]
  49.              [scene (rectangle (scene-width rows)
  50.                                (scene-height rows)
  51.                                "outline" "transparent")])
  52.             ([row rows])
  53.     (values
  54.      (add1 sum)
  55.      (place-image row
  56.                   (- (/ (image-width scene) 2) (if (even? sum) 0 (image-width half-on)))
  57.                   (* sum (/ (image-height split-hex) 1.33333))
  58.                   scene))))
  59.   image)
  60.  
  61.  
  62. (stack-rows (make-rows 10 20))

=>

image