[comp.lang.postscript] Postscript Spirograph

skdutta@cs.tamu.edu (Saumen K Dutta) (06/16/90)

----------X CUT HERE X---------X CUT HERE X------------
%!
%
% Postscript Spirograph: Written by S. K. Dutta
%
%
%
% Here is a  postscript spirograph program that simulates a real spirograph.
% A spirograph is formed by rotating a vector on an axis which itself
% rotates on some other axis. This gives rise to interesting figures.
% 
% Description:
%
% Following are the description of postscript control variables
%
% r1 - This is the radius of the outer circle and is kept at 0.5 so that
%      the picture totally covers the area of the paper.
% r2 - Changable radius of the wheel which rotates on r1. The value is
%      less than or equal to r1
% r3 - The radius where the hole exist on the circle r2. In real spirograph
%      a pen is moved through this hole and the circle r2 is rotated
%      on r1. r3 should be less than or equal to r2.
%
% The values of r2 and r3 are kept in the operand stack before the
% program starts. For multiple spirals on the same page you are
% required to push multiple values of r2 and r3 into the stack in
% the same order.  
%
/r1 0.5 def		% The value of r1 is fixed (don't change it ! )
%
/thetha 10 def		% thetha may be varied to control smoothness
%
% We now put the values of r2 and r3 into the stack. The following
% points must be noted while selecting the values.
%
% 1.	r2 and r3 must be within the second decimal places.
% 2.	(100 * r2) must be prime for better results.
% 3.	the operand stack should have even number of entries.
% 4.	The stack entries are put as : r2 r3 r2 r3 r2 r3 .....
%
0.31 0.29 0.47 0.1		% in stack
%
% Given below are some experimental values which the user can put
% into stack before running the program. For this, comment the 
% previous line of stack entry and uncomment any of the line given 
% below. You can also try other combinations( refer to the restr-
% ictions on the values of r2 and r3 as given above).
%
%0.43 0.05 0.43 0.19 0.43 0.31 0.43 0.42 
%0.33 0.09 0.17 0.05 0.47 0.25
%0.41 0.3
%
% BUGS & Limitations: 
%
% 1. The algorithm used makes the spirograph by drawing many straight
%    lines. Hence the smoothness of the lines are affected.
% 2. The speed of the rotating vector is not controlled.
% 3. Since the number of vectors printed are very large, the printing
%    is slow.
%

/alpha {
	r2 thetha mul r1 div
} def

/findhcf {
	{dup 3 1 roll rmod dup 0 eq {exit}if}loop
	pop
}def

/hcf {
	r1 1000 mul r2 1000 mul round findhcf r1 1000 mul exch div
}def

/hlim {
	hcf 360 mul thetha div 1 add
	} def

/rmod {
	2 copy div cvi mul sub cvi
	} def

/SpiroGraph {
1 1 hlim{
	gsave
	0 r1 r2 sub moveto
	dup alpha mul 360 rmod rotate
	6 array currentmatrix exch
	currentpoint translate
	thetha mul 360 rmod -1 mul rotate
	0 r3 moveto
	setmatrix
	currentpoint
	grestore
	2 {1 index} repeat
	6 -2 roll
	moveto lineto stroke
	}for
} def

/MainSpiral {
    0 setgray
	500 500 scale
	{
	count 0 eq {exit} if
    gsave
	newpath
	0.6 0.7 translate
	/r3 exch def
	/r2 exch def
	0 r1 r2 sub r3 add
	SpiroGraph
	pop pop
	grestore
	} loop 
} def

0 setlinewidth		% otherwise does'nt work on NeXT!
MainSpiral
showpage


----------X CUT HERE X---------X CUT HERE X------------



--
     _                                   ||Internet: skdutta@cssun.tamu.edu  
    (   /_     _ /   --/-/- _            ||Bitnet : skd8107@tamvenus.bitnet 
   __)_/(_____(_/_(_/_(_(__(_/_______    ||Uucp : uunet!cssun.tamu.edu!skdutta
                                 ..      ||Yellnet: (409) 846-8803