[plt-scheme] Fractals
Hi, I decided to make a (toy) fractals program. MzScheme (or PLT-Scheme in
general) has a reasonable advantage in being able to handle complex numbers
so well. However, I unfortunately could not get the code running fast
enough. I understand Scheme's more of a scripting language and is probably
not the best decision when it comes to number crushing, but i would
appreciate any way to get this running faster!
Here's the code:
;;;; General fractal generator, Mandelbrot Set example
(require (lib "etc.ss"))
;; This function takes a function trans which it uses to create
;; another function which, given a point in the complex plane,
;; returns the number of iterations until the point diverges
(define (pix-gen trans upper max-it)
(define max-iterations max-it)
(lambda (point)
(define (strength)
(let next ((new-point point) (iter 0))
(cond ((>= (magnitude new-point) upper) iter)
((>= iter max-it) iter)
(else (next (trans new-point) (+ iter 1))))))
;; if the value of (strength) belongs to the set,
;; return black (zero)
(let ((val (strength)))
(cond ((= val max-iterations) 0)
(else val)))))
(define (gen-fract gen x-start x-end y-start y-end x-steps y-steps)
; this part calculates the step on each iteration
(define x-int (/ (- x-end x-start) (sub1 x-steps)))
(define y-int (/ (- y-end y-start) (sub1 y-steps)))
; build a vector of number of iterations to diverge
(build-vector x-steps
(lambda (m)
(build-vector y-steps
(lambda (n)
(gen (make-rectangular (* m x-int)
(* n y-int))))))))
;; here's a sample that creates a mandelbrot function,
;; which a maximum depth of 255, and a critical value of 2
;; note: I eventually will want the depth > 1000
(define mand (pix-gen (lambda (x) (* x x)) 2 255))
;; test this out!
(time (printf "~a~n" (gen-fract mand 0.0 1.0 0.0 1.0 30 30)))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20060920/5c880872/attachment.html>