home *** CD-ROM | disk | FTP | other *** search
/ Dream 44 / Amiga_Dream_44.iso / RiscPc / programmation / scm4e2.arc / !Scm / scm / arc_demos next >
Text File  |  1994-06-19  |  5KB  |  218 lines

  1. ;
  2. ; Scheme demo code for new archi routines - Al Slater 9/6/94->
  3. ; Note - if you come up with any really nice routines - I'll include em
  4. ; with credits ...
  5. ;
  6.  
  7. ;
  8. ; Utility functions
  9. ;
  10. (define Xscreen 1280)          ;; physical size
  11. (define Yscreen 1024)          ;; ditto
  12. (define Left   -10)
  13. (define Right  10)
  14. (define Top    10)
  15. (define Bottom -10)
  16.  
  17. (define i->e inexact->exact)  ;; saves typing.
  18.  
  19. ; Set-universal-point => given two floating point values plot them.
  20. ;
  21. (define (sup xw yw)
  22.       (point
  23.           (i->e (round (/ (* (- xw Left) Xscreen) (- Right Left))))
  24.           (i->e (round (/ (* (- yw Bottom) Yscreen) (- Top Bottom))))
  25.       )
  26. )
  27.  
  28. ; ---------------------------------------------------------------------------
  29. ;; martin maps - run by (martin-map) - hit escape when you are bored.
  30. ;; note it'll run out of memory eventually if you leave it long enough,
  31. ;; but by that time you'll have seen more than enough.
  32.  
  33.  
  34. (define a 3.1)                          ;; something around Pi works ok..
  35.  
  36. (define (martin-iter x y)
  37. (let ((newx (- y (sin x)))(newy (- a x)))
  38.       (begin
  39.       (sup newx newy)
  40.       `(,newx ,newy))))    ;;; note use of backquote...
  41.  
  42. ;
  43. ; Prize for doing this more tidily - overwriting your own arguments is 
  44. ; absolutely bletcherous...(as well as being the most dire example of how not
  45. ; to do functional programming)
  46. ;
  47. (define (mm x y)
  48. (begin
  49.       (define res (martin-iter x y))
  50.       (set! x (car res))
  51.       (set! y (cadr res))
  52.       (mm x y)
  53. ))
  54.  
  55. (define (martin-map)
  56. (begin
  57.       (mode 12)
  58.       (gcol 0 1)
  59.       ;
  60.       ; ALWAYS define these four somewhere in your gfx routines if using
  61.       ; set-universal-point (and use set! NOT define..) confused yet?
  62.       ;
  63.       (set! Left          -10)
  64.       (set! Right          10)
  65.       (set! Top          10)
  66.       (set! Bottom      -10)
  67.       (mm 0 0)
  68. ))
  69.  
  70. ; ---------------------------------------------------------------------------
  71. ; A curve or two
  72.  
  73. (define pi 3.14159)
  74.  
  75. ; degrees -> radians
  76. (define (d->r a)(* a (/ pi 180)))
  77.  
  78. (define (iterate f from to expr)
  79. (if (> from to)
  80.       'done
  81.       (begin
  82.               (expr f from to)
  83.               (iterate f (+ from 1) to expr))))
  84.  
  85. (define (p-f f x y)
  86. (let ((fx (f (d->r x))))
  87.       (sup x fx)))
  88.  
  89. (define (f-curve f)
  90.       (begin
  91.               (mode 12)
  92.               (iterate f Left Right p-f)
  93.               (get)))
  94.  
  95. (define (trig-curve f)
  96. (begin 
  97.       (set! Left          0)
  98.       (set! Right          720)
  99.       (set! Top          2)
  100.       (set! Bottom      -2)
  101.       (f-curve f)
  102.       (get)))
  103.  
  104. (define (sin-curve)(trig-curve sin))
  105. (define (cos-curve)(trig-curve cos))
  106.  
  107. (define (tan-curve)
  108. (begin
  109.       (set! Left          0)
  110.       (set! Right          360)
  111.       (set! Top          10)
  112.       (set! Bottom        -10)
  113.       (f-curve tan)
  114.       (get)
  115. ))
  116.  
  117. ; ---------------------------------------------------------------------------
  118. ; Henon attractor
  119. ; try (henon) and hit escape when you've seen enough.
  120.  
  121. (define h_a 1.4)
  122. (define h_b 0.3)
  123.  
  124. (define (h_s i) (i->e (+ 400 (* 300 i))))
  125.  
  126. (define (henon-attractor x y)
  127. (begin
  128.       (point (h_s y)(h_s x))  ;; i prefer it this way around...
  129.       (define xn x)
  130.       (define newx (+ (- y (+ (* h_a x x))) 1))
  131.       (define newy (* h_b xn))    
  132.       (henon-attractor newx newy)))
  133.  
  134. (define (henon)
  135. (begin
  136.       (mode 12)
  137.       (gcol 0 1)
  138.       (henon-attractor 0 0)))
  139.      
  140. ; ---------------------------------------------------------------------------
  141. ; Ikeda attractor (from Pickover)
  142. ; Horrendously space inefficient no doubt....
  143.  
  144. (define ia_c1 0.4)
  145. (define ia_c2 0.9)
  146. (define ia_c3 6.0)
  147. (define ia_rho 1.0)
  148. (define ia_scale 200)
  149. (define ia_xoff  600)
  150. (define ia_yoff  500)
  151.  
  152. (define (ikeda-iter i x y)
  153. (if (> i 3000)
  154.     'done
  155.     (begin
  156.           (define temp (/ (- ia_c1 ia_c3) (+ 1 (* x x) (* y y))))
  157.           (define sin_temp (sin temp))
  158.           (define cos_temp (cos temp))
  159.           (define xt (+ ia_rho (* ia_c2 (- (* x cos_temp) (* y sin_temp)))))
  160.           (define newy (* ia_c2 (+ (* x sin_temp)(* y cos_temp))))
  161.           (define newx xt)
  162.           (point (i->e (+ (* x ia_scale) ia_xoff))
  163.                  (i->e (+ (* y ia_scale) ia_yoff)))
  164.           (ikeda-iter (+ i 1) newx newy))))
  165.  
  166. (define (ikeda)
  167. (begin
  168.       (mode 12)
  169.       (ikeda-iter 1 0.1 0.1)))
  170.       
  171. ; ---------------------------------------------------------------------------
  172.  
  173.           
  174. ; ---------------------------------------------------------------------------
  175. ; Miscellaneous FP related definitions - see suggested things to try with
  176. ; definitions.
  177.  
  178. ;; like a 'for' loop
  179. ; try with
  180. ; (define (f n w)(+ 1 w))
  181. ; (itn 1 10 0 f)
  182. ; function 'f' is always defined in terms of 'n' and 'w' generally..
  183.  
  184. (define (itn n b w f)
  185.     (if (> n b)
  186.         w
  187.         (itn (+ n 1) b (f n w) f)
  188. ))
  189.  
  190. ;; itl (like foldl in miranda / itl in KRC)
  191. (define (itl l w f)
  192.     (if (null? l)
  193.         w
  194.         (itl (cdr l) (f (car l) w) f)
  195.     ))
  196.  
  197. ;; lit (like foldr in miranda / lit in KRC (this isn't strictly correct but
  198. ;; it'll do.)
  199. (define (lit f w l)
  200.     (if (null? l)
  201.         w
  202.         (f (car l) (lit f w (cdr l)))
  203.     ))
  204.  
  205. ;
  206. ; things to try with itl and lit :
  207. ; (itl '(1 2 3 4) '() cons)
  208. ; (lit cons '() '(1 2 3 4))
  209.  
  210. ; stuff thats nice to have defined..
  211. (define (go)
  212. (begin
  213.       (mode 12)
  214.       (graphics-origin! 640 512)
  215. ))
  216.  
  217. (display "Loaded Arc demos")(newline)