home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 168.img / ACAD3.ZIP / FPLOT.LSP < prev    next >
Lisp/Scheme  |  1988-09-23  |  4KB  |  125 lines

  1. ;
  2. ;       Plot function of two variables
  3. ;
  4. ;       Designed and implemented by Kelvin R. Throop in June of 1988
  5. ;
  6. ;       To make a three dimensional polygon mesh representing the
  7. ;       values of a function in two variables across a specified range
  8. ;       of values for the two variables, with a defined resolution
  9. ;       (specified as the number of subdivisions within the range), call:
  10. ;
  11. ;           (fplot function xrange yrange resolution)
  12. ;
  13. ;       where:
  14. ;
  15. ;          function    The function to be evaluated.  This will
  16. ;                      usually be the quoted name of a previously-
  17. ;                      defined function, or a quoted lambda-definition
  18. ;                      of a function.
  19. ;
  20. ;          xrange      The range of X values, specified as a list
  21. ;                      with the first element the lower bound for X
  22. ;                      and the second element the upper bound.
  23. ;
  24. ;          yrange      The range of Y values, specified as a list
  25. ;                      with the first element the lower bound for Y
  26. ;                      and the second element the upper bound.
  27. ;
  28. ;          resolution  An integer specifying the granularity of the
  29. ;                      mesh approximating the surface defined by the
  30. ;                      function's values for arguments in the specified
  31. ;                      range.
  32. ;
  33. ;       For example, to plot (e**(-(X**2 + Y**2))) over the range from
  34. ;       -1 to 1 in both the X and Y axes, use:
  35. ;
  36. ;               (fplot '(lambda (x y) (exp (- (+ (* x x) (* y y)))))
  37. ;                       '(-2 2)
  38. ;                       '(-2 2)
  39. ;                        20
  40. ;               )
  41. ;
  42. ;       (This will look like a tennis ball under the rug, when viewed
  43. ;       from, say, VPOINT 1,1,1.)
  44. ;
  45. ;       Or, you can plot a predefined function.  For example:
  46. ;
  47. ;                (defun cs (x y)
  48. ;                   (cos (sqrt (+ (* x x 2) (* y y))))
  49. ;                )
  50. ;                (fplot 'cs '(-20 20) '(-20 20) 40)
  51. ;
  52. ;       This makes a series of elliptical ripples, like a pond after
  53. ;       you've just dropped in a cinder block.
  54. ;
  55. ;       This file contains a complex predefined test case.  If you
  56. ;       enter the command:
  57. ;
  58. ;                DEMO
  59. ;
  60. ;       you'll get the interference pattern from two exponentially
  61. ;       damped cosine waves.  This example illustrates the amazing
  62. ;       surfaces you can generate with a simple definition using
  63. ;       fplot.
  64. ;
  65. ;
  66. (defun fplot (fcn xrange yrange res / ce stepx stepy i j x y)
  67.  
  68.         (setq x (car xrange)
  69.               stepx (/ (- (cadr xrange) x) (float res))
  70.               stepy (/ (- (cadr yrange) (car yrange)) (float res))
  71.               i 0
  72.         )
  73.  
  74.         (setq ce (getvar "cmdecho"))
  75.         (setvar "cmdecho" 0)
  76.         (command "3Dmesh" res res)
  77.         (while (< i res)
  78.            (setq j 0
  79.                  y (car yrange)
  80.            )
  81.            (while (< j res)
  82.               (setq j (1+ j)
  83.                     y (+ y stepy)
  84.               )
  85.               (command (list x y (apply fcn (list x y))))
  86.            )
  87.            (setq i (1+ i)
  88.                  x (+ x stepx)
  89.            )
  90.         )
  91.         (setvar "cmdecho" ce)
  92. )
  93.  
  94. ;       Demo program
  95.  
  96. ;       Generate exponentially damped cosine wave
  97.  
  98. (defun dampcos (x y / dist omag sfreq decfr)
  99.         (setq
  100.               omag  2.0      ; Overall magnitude scale factor
  101.               sfreq 8.0      ; Spatial frequency factor
  102.               decfr 1.5      ; Exponential decay spatial frequency
  103.         )
  104.         (setq dist (sqrt (+ (* x x) (* y y))))
  105.         (* omag
  106.            (cos (* dist sfreq))
  107.            (exp (- (* decfr dist)))
  108.         )
  109. )
  110.  
  111. ;       Calculate interference of two damped cosine waves
  112.  
  113. (defun interf (x y / offset)
  114.         (setq offset 0.9)          ; Offset of centres from origin
  115.         (+ (dampcos (- x offset) y) (dampcos (+ x offset) y))
  116. )
  117.  
  118. ;       Demo run of function plot, type DEMO at command prompt
  119.  
  120. (defun C:demo ()
  121.         (fplot 'interf
  122.                 '(-3 3) '(-3 3) 50)
  123.         (princ)                    ; Suppress printing function result
  124. )
  125.