[ create a new paste ] login | about

Link: http://codepad.org/rk6S6he9    [ raw code | output | fork ]

aaronla - Scheme, pasted on May 2:
; mb.ss
;
; mandelbrot set

(define (1+ r)
  (+ r 1))

(define (abs-complex c)
  (let ((r (real-part c)) (i (imag-part c)))
    (sqrt (+ (* r r) 
             (* i i)))))

(define (mandelbrot c)
  (let iterate ((x 0) (acc c))
    (if (and (< (abs-complex acc) 2) (< x 200))
        (iterate (1+ x) (+ (* acc acc) c))
      x)))

(define chars
  '( (1    ".")
     (3    ":")
     (8    "/")
     (16   "$")
     (64   "%")
     (1000 "#") ))

(define (itercount->string cnt)
  (let find ((chars chars))
    (cond 
      ((null? (cdr chars)) 
       (cadar chars))
      ((<= cnt (caar chars))
       (cadar chars))
      (else (find (cdr chars))))))

(define run 
  (let ((rows 30) (cols 50))
    (lambda (rmin rmax imin imax)
      (do ((ii 0 (1+ ii))) ((= ii rows) #f)
        (do ((rr 0 (1+ rr))) ((= rr cols) #f)
          (let* ((rx (+ (/ (* (- rmax rmin) rr) (- cols 1)) rmin))
                 (ix (+ (/ (* (- imax imin) ii) (- rows 1)) imin))
                 (x (+ rx (* ix 0+1i)))
                 (y (mandelbrot x))
                 (str (itercount->string y)))
            (display str)))
        (newline))
      (void))))

;; example plot:
(run -2.0 1.0 -1.5 1.5)


Output:
..................................................
..................................................
....................::::::::::....................
..............::::::::::::::::::::::..............
...........::::::::::::::::://$//::::::...........
........:::::::::::::::::::////$%/::::::::........
.......::::::::::::::::::////$$#$///:::::::.......
.....:::::::::::::::::://////####$/////::::::.....
....::::::::::::::::///$%/$$$$##%$$$////::::::....
...:::::::::::::///////$############$##%/::::::...
..:::::::::://///////$%%##############$///::::::..
.::::::::///$///$////%%#################$/:::::::.
.:::::://////$#%%#%$$%##################//:::::::.
.::::://////$#######%##################%//:::::::.
.////////$%#%#########################%///:::::::.
.////////$%#%#########################%///:::::::.
.::::://////$#######%##################%//:::::::.
.:::::://////$#%%#%$$%##################//:::::::.
.::::::::///$///$////%%#################$/:::::::.
..:::::::::://///////$%%##############$///::::::..
...:::::::::::::///////$############$##%/::::::...
....::::::::::::::::///$%/$$$$##%$$$////::::::....
.....:::::::::::::::::://////####$/////::::::.....
.......::::::::::::::::::////$$#$///:::::::.......
........:::::::::::::::::::////$%/::::::::........
...........::::::::::::::::://$//::::::...........
..............::::::::::::::::::::::..............
....................::::::::::....................
..................................................
..................................................


Create a new paste based on this one


Comments: