;;; Scheme Recursive Art Contest Entry ;;; ;;; Please do not include your name or personal info in this file. ;;; ;;; Title: Newton's Method ;;; ;;; Description: ;;; wave crashes on autumn shore ;;; a divide survives ;;; thanks to my english roommate ;;; Util (define (cadr lst) (car (cdr lst))) ;;; Newton Method's ; f and f' (define (f z) (c+ 2 (c* z (c- (c* z z) 2)))) (define (fp z) (c- (c* 3 (c* z z)) 2)) ; the three roots of our particular function f (define r1 -1.76929235423863141520) (define r2 (complex +0.88464617711931570762 -0.58974280502220550165)) (define r3 (complex +0.88464617711931570762 +0.58974280502220550165)) ; max-iterations and epsilon (define max-iterations 10) (define eps 1e-2) (define (newton z) (define (newton-iter z k) (let ((nz (c- z (c/ (f z) (fp z))))) (if (or (>= k max-iterations) (close z nz)) (list nz k) (newton-iter nz (+ k 1))))) (newton-iter z 0)) ; sees if two complex numbers are sufficiently "close" to eachother (define (close z1 z2) (< (abs (c- z1 z2)) eps)) ;;; Turtle Stuff (define sh 1000) (define sw 1000) (setup sw sh) (define quadsizex (/ sw 4)) (define quadsizey (/ sh 4)) ; convert from a pixel coordinate (x, y) to a complex number (define (pixel-to-complex x y) (complex (- (/ x quadsizex) 2) (- 2 (/ y quadsizey)))) ;;; Color Stuff (define (scale-color r g b iters) (let ((scale (- 1 (/ iters max-iterations)))) (rgb (* scale r) (* scale g) (* scale b)))) ; three different colors (define (c1 iters) (scale-color 0.73725490196 0.73725490196 0.73725490196 iters)) (define (c2 iters) (scale-color 0.94901960784 0.55294117647 0.37254901960 iters)) (define (c3 iters) (scale-color 0.99607843137 0.79215686274 0.37254901960 iters)) (define (c4 iters) (rgb 0 0 0)) ; determines color from the result of Newton's Method (define (newton-color res) (let ((z (car res)) (iters (cadr res))) (cond ((close z r1) (c1 iters)) ((close z r2) (c2 iters)) ((close z r3) (c3 iters)) (else (c4 iters))))) ; colors a certain pixel (define (color-pixel x y) (pixel x y (newton-color (newton (pixel-to-complex x y))))) ;;; Drawing (define (draw) (define (iter-draw x y) (cond ((> y sh) nil) ((> x sw) (begin (update) (iter-draw 0 (+ y 1)))) (else (begin (color-pixel x y) (iter-draw (+ x 1) y))))) (iter-draw 0 0) (exitonclick)) (draw)