Index / 2018-09-17-carpet.rkt

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#lang racket
(require 2htdp/image)

(define (->color c)
  (cond [(= c 0) 'indigo]
        [(= c 1) 'palevioletred]
        [(= c 2) 'moccasin]
        [(= c 3) 'lemonchiffon]
        [else 'white]))

(define (next-c c) (modulo (+ 1 c) 5))

(define (i c s)
  (if (< s 5)
      (square s 'solid (->color c))
      (overlay (rotate 45 (i (next-c c) (* 0.707 s)))
               (square s 'solid (->color c)))))

(define (sq2x2 i1 i2)
  (above (beside i1 i2)
         (beside i2 i1)))

(define (step c s)
  (if (< s 10) (i (next-c c) (* 1.2 s))
      (sq2x2 (step (next-c c) (/ s 2)) (i c (* 0.707 s)))))

(provide main)
(define (main . args)
  (save-image
   (step 0 800)
   (car args)))