
;; mandelbrot set

(define mandel
  (lambda (rmin rmax imin imax res)
    (letrec
	((rdelta (- rmax rmin)) (idelta (- imax imin))
	 (rdelta1 (/ rdelta res)) (idelta1 (/ idelta res))
	 (pcolor
	  (lambda (re im cre cim n)
	    (if (or (> n 200) (> (+ (* re re) (* im im)) 4.0))
		(let
		  ((c (* 8 (remainder n 8)))
		   (x (* res (/ (- cre rmin) rdelta)))
		   (y (* res (/ (- cim imin) idelta))))
		  (draw-color
		   (* 255 (remainder n 2))
		   (* 255 (remainder (quotient n 2) 2))
		   (* 255 (remainder (quotient n 4) 2)))
		  (draw-move x y)
		  (draw-line (+ x 1) (+ y 1)))
		(pcolor
		 (+ (- (* re re) (* im im)) cre)
		 (+ (* 2 re im) cim)
		 cre cim (+ 1 n)))))
	 (iter
	  (lambda (rep imp)
	    (if (> rep rmax)
		(if (> imp imax) '()
		    (iter rmin (+ imp idelta1)))
		(begin
		  (pcolor 0 0 rep imp 0)
		  (iter (+ rep rdelta1) imp))))))
      (iter rmin imin))))

(mandel -1.5 0.5 -1.0 1.0 25)

