home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Tools⁄Additions / MacScheme20 / Graphics Examples / trifractal.sch < prev   
Encoding:
Text File  |  1986-08-16  |  2.2 KB  |  57 lines  |  [TEXT/EDIT]

  1. ; (koch n) draws a fractal of degree n.
  2. ; A graphics window must be open when koch is called.
  3. ;
  4. ; This program was translated from a Pascal program of the same name
  5. ; that appeared in
  6. ;
  7. ;     Matthew Zeidenberg, "Snowflakes and Dragons: Drawing fractals
  8. ;     with Macintosh Pascal", MacWorld Volume 2, Number 8, August
  9. ;     1985, pages 124-131.
  10. ;
  11. ; It is interesting that the program below contains no assignment
  12. ; statements.  The original Pascal program was full of assignment
  13. ; statements, but the Scheme version uses local bindings instead.
  14.  
  15. (define koch
  16.   (lambda (n)
  17.     (fractal1-for-half-window 235 100 60 n)))
  18.  
  19. (define fractal1-for-half-window
  20.   (lambda (xorig yorig scaling n)
  21.     (letrec ((sin60 .8660254)
  22.              (side
  23.               (lambda (x1 y1 x2 y2 n)
  24.                 (if (= n 1)
  25.                     (draw-line (+ x1 xorig)
  26.                                (+ y1 yorig)
  27.                                (+ x2 xorig)
  28.                                (+ y2 yorig))
  29.                     (let ((xdiff (* 1.0 (- x2 x1)))
  30.                           (ydiff (* 1.0 (- y2 y1))))
  31.                       (let ((x3 (+ x1 (round (/ xdiff 3))))
  32.                             (y3 (+ y1 (round (/ ydiff 3))))
  33.                             (x4 (+ x1 (round (- (/ xdiff 2)
  34.                                                 (/ (* ydiff sin60)
  35.                                                    3)))))
  36.                             (y4 (+ y1 (round (+ (/ ydiff 2)
  37.                                                 (/ (* xdiff sin60)
  38.                                                    3)))))
  39.                             (x5 (+ x1 (round (/ (* xdiff 2) 3))))
  40.                             (y5 (+ y1 (round (/ (* ydiff 2) 3)))))
  41.                         (begin
  42.                          (side x1 y1 x3 y3 (- n 1))
  43.                          (side x3 y3 x4 y4 (- n 1))
  44.                          (side x4 y4 x5 y5 (- n 1))
  45.                          (side x5 y5 x2 y2 (- n 1)))))))))
  46.       (let
  47.         ((x1 0)
  48.          (y1 (round (* scaling sin60)))
  49.          (x2 (round scaling))
  50.          (y2 (- (round (* scaling sin60))))
  51.          (x3 (- (round scaling)))
  52.          (y3 (- (round (* scaling sin60)))))
  53.         (begin
  54.          (side x1 y1 x2 y2 n)
  55.          (side x2 y2 x3 y3 n)
  56.          (side x3 y3 x1 y1 n))))))
  57.