Article View: comp.lang.postscript
Article #966Re: Building A PostScript Toolset
From: Roy Smith
Date: Wed, 30 Aug 1989 17:20
Date: Wed, 30 Aug 1989 17:20
100 lines
3221 bytes
3221 bytes
In <2251@optilink.UUCP> cramer@optilink.UUCP (Clayton Cramer) writes:
> 3. To prevent creating write-only code, set variable names to stack
> items at the start of the function [...] rather than using lots of dups,
> exchs, and rolls
Well, here is a counter example. I wrote this a while ago, and
have used it several times. It is a bunch of routines to let you work with
vectors in 2-space (i.e. x,y coordinates). It's all dups, exchs, and
rolls. Given the triviality of these, I think it's pretty easy to follow
the thread of them, and it's all pretty well documented anyway. But, yes,
in general I agree -- code which does lots of fancy stack manipulation is
hard to grok, even for somebody like me who grew up on HP (i.e. stack)
machines. I did a lot of up,up,rolldown,exchange stuff when I was hacking
9810's. Obviously, the following can be extended, dot and cross products,
and dimensions higher than 2, but that's all I needed so that's all I did.
-------------------
%
% Vectorops.ps -- vector operators for PostScript.
%
% Copyright 1987 Roy Smith
% Permission is hereby granted for any use of this software other
% than selling it or including it in a commercial product.
%
% $Header: vectorops.ps,v 1.1 87/09/16 20:31:59 roy Exp $
%
% vadd -- add two vectors.
% x1 y1 x2 y2 vadd ==> x1+x2 y1+y2
/vadd
{
3 -1 roll add % ==> x1 x2 y2+y1
3 1 roll add % ==> y2+y1 x1+x2
exch % ==> x1+x2 y2+y1
} def
% vsub -- subtract two vectors.
% x1 y1 x2 y2 vsub ==> x1-x2 y1-y2
/vsub
{
3 -1 roll sub neg % ==> x1 x2 -(y2-y1)
3 1 roll sub % ==> -(y2-y1) x1-x2
exch % ==> x1-x2 -(y2-y1)
} def
% vneg -- take negative of vector (scalar multiply by -1).
% x y vneg ==> -x -y
/vneg
{
exch neg % ==> y -x
exch neg % ==> -x -y
} def
% vdiv -- divide vector by scalar.
% x y s vdiv ==> x/s y/s
/vdiv
{
dup % ==> x y s s
3 1 roll div % ==> x s y/s
3 1 roll div % ==> y/s x/s
exch % ==> x/s y/s
} def
% vmul -- multiply vector by scalar.
% x y s vmul ==> x*s y*s
/vmul
{
dup % ==> x y s s
3 1 roll mul % ==> x s y*s
3 1 roll mul % ==> y*s x*s
exch % ==> x*s y*s
} def
% vexch -- exchange two vectors on stack.
% x1 y1 x2 y2 vexch => x2 y2 x1 y1
/vexch
{
4 2 roll % ==> x2 y2 x1 y1
} def
% vpop -- pop a vector off the stack.
% x1 y1 vpop => ---
/vpop
{
pop pop
} def
% vdup -- duplicate a vector on the stack.
% x1 y1 vdup => x1 y1 x1 y1
/vdup
{
dup % ==> x1 y1 y1
3 -1 roll dup % ==> y1 y1 x1 x1
4 1 roll exch % ==> x1 y1 x1 y1
} def
--
Roy Smith, Public Health Research Institute
455 First Avenue, New York, NY 10016
{att,philabs,cmcl2,rutgers,hombre}!phri!roy -or- roy@alanine.phri.nyu.edu
"The connector is the network"
Technical Details
Message-ID:
<3965@phri.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!watmath!att!dptg!rutgers!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!ginosko!rex!uflorida!haven!adm!cmcl2!phri!royReferences:
<2251@optilink.UUCP>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!watmath!att!dptg!rutgers!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!ginosko!rex!uflorida!haven!adm!cmcl2!phri!roy From: roy@phri.UUCP (Roy Smith) Newsgroups: comp.lang.postscript Subject: Re: Building A PostScript Toolset Message-ID: <3965@phri.UUCP> Date: 30 Aug 89 17:20:01 GMT References: <2251@optilink.UUCP> Reply-To: roy@phri.UUCP (Roy Smith) Organization: Public Health Research Inst. (NY, NY) Lines: 100