[geeks] Postscript question

Jonathan C Patschke jp at celestrion.net
Wed Nov 20 20:47:38 CST 2002


On Tuesday, November 19, 2002, at 04:40 PM, Joshua D Boyd wrote:

>> I did a similar sort of iteration to construct a generic "function
>> grapher" in PostScript.  I can post the (unoptimized, I'm sure) code, 
>> if
>> you want to see how something like that would be done.
>
> Yeah, it would be really cool to see that.  I don't know much about
> postscript, but I'm interested in learning more.

Okay, the code appears below, and I'm sure it's thoroughly unoptimized, 
since I wrote it about 2 hours after sitting down and deciding "I wanna 
learn PostScript".  The "sinewave" routine is hard-coded to calculate 
y=sin(x), but replacing "sin" with your function of choice will make it 
plot just about anything that's bounded [-1,1].

The line in the for-loop looks complicated, but all it really does is 
get X+dX, compute the sine, stretch the result to the height of the 
bounding box, and translate it relative to the lower-left corner of the 
bounding box.

----- snip here -----

%!PS
%
% My second PostScript document: a sine-wave
%
/inch {72 mul} bind def
/baseX {3.5 inch add} bind def
/baseY {5.0 inch add} bind def
/sinewave {
   % sinewave: Draws a sinewave.
   %
   % Takes 7 stack parameters:
   %   beginX:    The left of the bounding box, in points
   %   beginY:    The bottom of the bounding box, in points
   %   endX:      The right of the bounding box, in points
   %   endY:      The top of the bounding box, in points
   %   beginT:    The initial value of theta, in degrees
   %   endT:      The final value of theta, in degrees
   %   interval:  The step size for the solver, in degrees
   %
   /interval exch def
   /endT exch def
   /beginT exch def
   /endY exch def
   /endX exch def
   /beginY exch def
   /beginX exch def

   % draw the bounding box
   newpath
     beginX beginY moveto
     beginX endY   lineto
     endX   endY   lineto
     endX   beginY lineto
     closepath
   stroke

   % draw the sine-curve
   newpath
     % calculate constants
     /scaleY endY beginY sub 2 div def
     /scaleX endX beginX sub endT beginT sub div def
     % move to the center of the left-side of the bounding box
     beginX beginY endY add 2 div moveto
     beginT interval endT {
       dup beginT sub scaleX mul beginX add exch sin scaleY mul beginY 
add scaleY add lineto
     } for
   stroke
} def

% Set the default font
/Helvetica-Roman findfont
12 scalefont
setfont

newpath
0.0 inch baseX 1.0 inch baseY moveto
(Sine Box!) show
stroke

0.0 inch baseX 0.0 inch baseY 1.5 inch baseX 1.0 inch baseY 0 360 1 
sinewave

/baseY {3.75 inch add} bind def

newpath
0.0 inch baseX 1.0 inch baseY moveto
(Multisine Box!) show
stroke

0.0 inch baseX 0.0 inch baseY 1.5 inch baseX 1.0 inch baseY 0 360 1 
sinewave
0.0 inch baseX 0.0 inch baseY 1.5 inch baseX 1.0 inch baseY 90 450 1 
sinewave
0.0 inch baseX 0.0 inch baseY 1.5 inch baseX 1.0 inch baseY 180 540 1 
sinewave
0.0 inch baseX 0.0 inch baseY 1.5 inch baseX 1.0 inch baseY 270 630 1 
sinewave

/baseY {2.5 inch add} bind def

newpath
0.0 inch baseX 1.0 inch baseY moveto
(Stretch Box!) show
stroke

0.0 inch baseX 0.0 inch baseY 1.5 inch baseX 1.0 inch baseY 0 360 1 
sinewave
0.0 inch baseX 0.0 inch baseY 1.5 inch baseX 1.0 inch baseY 0 270 1 
sinewave
0.0 inch baseX 0.0 inch baseY 1.5 inch baseX 1.0 inch baseY 0 180 1 
sinewave
0.0 inch baseX 0.0 inch baseY 1.5 inch baseX 1.0 inch baseY 0 90 1 
sinewave

showpage

----- end of code -----

--
Jonathan C. Patschke
Celestrion Information Systems
Thorndale, TX



More information about the geeks mailing list