import source from 1.3.40
[external/swig.git] / Examples / GIFPlot / Common-Lisp / full / runme.lisp
1 ;;; Plot a 3D function
2
3 ;; Here is the function to plot
4 (defun func (x y)
5   (* 5
6      (cos (* 2 (sqrt (+ (* x x) (* y y)))))
7      (exp (* -0.3 (sqrt (+ (* x x) (* y y)))))))
8
9 ;; Here are some plotting parameters
10 (defvar xmin -5D0)
11 (defvar xmax  5D0)
12 (defvar ymin -5D0)
13 (defvar ymax  5D0)
14 (defvar zmin -5D0)
15 (defvar zmax  5D0)
16
17 ;; Grid resolution
18 (defvar nxpoints  60)
19 (defvar nypoints  60)
20
21 (defun drawsolid (p3)
22   (Plot3D-clear p3 0)
23   (Plot3D-start p3)
24   (let ((dx (/ (- xmax xmin) nxpoints))
25         (dy (/ (- ymax ymin) nypoints))
26         (cscale (/ 240 (- zmax zmin))))
27     (loop for x from xmin by dx
28           repeat nxpoints
29           do (loop for y from ymin by dy
30                    repeat nypoints
31                    do (let* ((z1 (func x y))
32                              (z2 (func (+ x dx) y))
33                              (z3 (func (+ x dx) (+ y dy)))
34                              (z4 (func x (+ y dy)))
35                              (c1 (* cscale (- z1 zmin)))
36                              (c2 (* cscale (- z2 zmin)))
37                              (c3 (* cscale (- z3 zmin)))
38                              (c4 (* cscale (- z4 zmin)))
39                              (cc (/ (+ c1 c2 c3 c4) 4))
40                              (c  (round (max (min cc 239) 0))))
41                         (Plot3D-solidquad p3 x y z1 (+ x dx) y z2 (+ x dx) (+ y dy)
42                                           z3 x (+ y dy) z4 (+ c 16)))))))
43
44 (defun action (cmap-filename)
45   (let ((cmap (new-ColorMap cmap-filename))
46         (frame (new-FrameBuffer 500 500)))
47     (format t "Making a nice 3D plot...~%")
48     (FrameBuffer-clear frame 0)
49     (let ((p3 (new-Plot3D frame xmin ymin zmin xmax ymax zmax)))
50       (Plot3D-lookat p3 (* 2 (- zmax zmin)))
51       (Plot3D-autoperspective p3 40D0)
52       (Plot3D-rotu p3 60D0)
53       (Plot3D-rotr p3 30D0)
54       (Plot3D-rotd p3 10D0)
55       (drawsolid p3))
56     (FrameBuffer-writeGIF frame cmap "/tmp/image.gif")
57     (format t "Wrote image.gif~%")))
58
59