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!
<br><br>Here's the code:<br><br><br>;;;; General fractal generator, Mandelbrot Set example<br><br>(require (lib "etc.ss"))<br><br>;; This function takes a function trans which it uses to create<br>;; another function which, given a point in the complex plane,
<br>;; returns the number of iterations until the point diverges<br>(define (pix-gen trans upper max-it)<br> (define max-iterations max-it)<br> (lambda (point)<br> (define (strength)<br> (let next ((new-point point) (iter 0))
<br> (cond ((>= (magnitude new-point) upper) iter)<br> ((>= iter max-it) iter)<br> (else (next (trans new-point) (+ iter 1))))))<br> ;; if the value of (strength) belongs to the set,
<br> ;; return black (zero)<br> (let ((val (strength)))<br> (cond ((= val max-iterations) 0)<br> (else val)))))<br> <br>(define (gen-fract gen x-start x-end y-start y-end x-steps y-steps)
<br> ; this part calculates the step on each iteration<br> (define x-int (/ (- x-end x-start) (sub1 x-steps)))<br> (define y-int (/ (- y-end y-start) (sub1 y-steps)))<br> ; build a vector of number of iterations to diverge
<br> (build-vector x-steps<br> (lambda (m)<br> (build-vector y-steps<br> (lambda (n)<br> (gen (make-rectangular (* m x-int) (* n y-int))))))))
<br> <br>;; here's a sample that creates a mandelbrot function,<br>;; which a maximum depth of 255, and a critical value of 2<br>;; note: I eventually will want the depth > 1000<br>(define mand (pix-gen (lambda (x) (* x x)) 2 255))
<br><br>;; test this out!<br>(time (printf "~a~n" (gen-fract mand 0.0 1.0 0.0 1.0 30 30)))<br><br>