(use chicken.string chicken.format srfi-1) (define height 900) (define width 900) (define escape-value 700) (define (hsb->rgb h s b) (let* ((c (* b s)) (x (* c (- 1 (abs (sub1 (modulo (floor (/ h 60)) 2)))))) (m (- b c)) (rgb* (cond ((and (<= 0 h) (< h 60)) (list c x 0)) ((and (<= 60 h) (< h 120)) (list x c 0)) ((and (<= 120 h) (< h 180)) (list 0 c x)) ((and (<= 180 h) (< h 240)) (list 0 x c)) ((and (<= 240 h) (< h 300)) (list x 0 c)) ((and (<= 300 h) (< h 360)) (list c 0 x))))) (map (lambda (c) (floor (* 255 (+ m c)))) rgb*))) (define (color-mapping i) (hsb->rgb (- 359 (modulo i 360)) 1 (/ i (+ 8 i)))) (define colors (map color-mapping (iota (add1 escape-value)))) (define (mandelbrod/real real img) (define (x* x y) (assume ((x float) (y float) (real float)) (+ (- (* x x) (* y y)) real))) (define (y* x y) (assume ((x float) (y float) (img float)) ;; NOTE: Using (* 2.0 x y) is _slow_ (+ (* (* 2.0 x) y) img))) (let iterate ((x (the float 0.0)) (y (the float 0.0)) (count 0)) (if (or (>= (+ (* x x) (* y y)) 4) (>= count escape-value)) count (iterate (x* x y) (y* x y) (add1 count))))) (define (infinity-trap/instant-draw acorner bcorner side #!key (formula mandelbrod/real)) (let ((h-gap (/ side height)) (w-gap (/ side width))) (fprintf (current-error-port) "Gap: ~a, ~a~%" h-gap w-gap) (let loop ((i 0)) (if (>= i (* height width)) 'done (begin (let* ((c (formula (+ acorner (* w-gap (remainder i width))) (+ bcorner (* h-gap (quotient i width))))) (colors (if (>= c escape-value) '(0 0 0) (list-ref colors (floor c))))) (printf "~a ~a ~a~%" (car colors) (cadr colors) (caddr colors))) (loop (add1 i))))))) (print "P3") (printf "~a ~a~%" width height) (print 255) (time (infinity-trap/instant-draw -2 -1.25 2.5 formula: mandelbrod/real))