You should be automatically redirected . If not, visit
and update your bookmarks.


Quickie PostScript graphics

If you want to generate some quick graphics from a newLISP script, here's a simple technique that involves just a small bit of PostScript code. (If you're using Linux or Windows, you'll need a PostScript interpreter and PDF converter somewhere on your system - I don't know whether these are installed by default, like they are on MacOS X.)

This little script draws a bar graph in a PostScript file, given a list of height values. The open command automatically converts the PostScript file to PDF format and displays the image in Preview.

(set 'ps-prolog "%!PS-Adobe-2.0
%%Title: newLISP test
%%Creator: newLISP
0.5 setlinewidth
30 30 translate
/str {10 string} def
/Helvetica findfont 8 scalefont setfont
(set 'ps-epilog " showpage
(define (bar-chart point-list)
    " bar-chart '(y1 y2 y3) ..." 
    (println ps-prolog)
    (set 'x-coord 10)  ; start x
    (set 'rect-width 15) ; width of bar
    (println (format "%d %d moveto " x-coord (first point-list)))
    (dolist (y point-list)
        ; draw bar
            (format "%d %d %d %f rectstroke " x-coord 0 rect-width y ))
        ; draw label
            (format "%d %f moveto %d str cvs show " x-coord (+ y 5)  (floor y)))
        (inc 'x-coord rect-width))
    (println "stroke") 
    (println ps-epilog))
(device (open "/tmp/" "write"))  
(bar-chart (random 0 500 30)) ; example 
(close (device))
(exec "open /tmp/") ; convert to PDF

A useful newLISP facility here is the device command. This can direct the output of the various println functions to the named device, a file in /tmp. But, you can comment out the device calls while you're developing the script, then, when the statements look OK, remove the comments and run the file again, printing to the file instead.

(And before you all tell me that there are much better graphing tools in newLISP's Tcl/Tk Graphical Frontend - I just haven't had the time to look into this. Also, I used to like hacking PostScript files, so this was fun!)


Post a Comment

Links to this post:

Create a Link

<< Home