You are on page 1of 2

; file DrawSpirograph.

txt
; This RPN calculator program draws a spirograph picture.
;
;
;
;
;
;
;
;

A Spirograph is formed by rolling a first circle (called


the moving circle) inside or outside of another circle
(called the stationary circle). The pen is attached to
the moving circle at some arbitrary distance from that
circle's center. If the radius of fixed circle is R,
the radius of moving circle is r, and the offset of the
pen from the center of the moving circle is p, then the
equation of the resulting curve is defined by:

; x = (R+r)*cos(t) - p*cos(((R+r)/r)*t)
; y = (R+r)*sin(t) - p*sin(((R+r)/r)*t)
; where t is the angle theta which starts at 0 degrees
; and continues incrementing forever.
;
;
;
;
;

Note that the spirograph will be distorted unless you


manually drag the cGraph window to some size where the
aspect ratio is unity (that is, one unit of the x axis
covers the same number of screen pixels as one unit of
the y axis).

;
;
;
;
;

Because this program continues to generate data points


forever it will eventually overflow the number of data
points which the cGraph window can accept (nothing bad
happens, the cGraph window just forces the RPN calculator
program to halt).

; This program requires 3 passed parameters. To initiate


; this program use the keystroke sequence:
;
R ENTER r ENTER p GSB 0
;
;
;
;
;
;

The three passed parameters can be most any value. If


r > 0 then the moving circle rides along the outside
of the fixed circle. If r < 0 then the moving circle
rides along the inside of the fixed circle.
DON'T REQUEST r=0 as the program will then generate
a divide-by-zero error !!

; Here's some "settings" that will show you the variety


; of pictures that can be produced:
;
R = 75, r = -30, p = 60
;
R = 75, r = -32, p = 60
;
R = 60, r = -16, p = -15
;
R = 90, r = 1, p = 105
;
R = 90, r = 2, p = 105
;
R = 89, r = 1, p = 105
;
R = 100, r = 48, p = 60
; This program stores t in Reg[0], R in Reg[1], r in Reg[2],
; p in Reg[3], and the intermediate term (R+r) in Reg[4].
LBL 0
ClearG
DEG
STO 3

; empty any prior plot


; sets calculator into degrees mode for trig functions
; Reg[3] is used to hold p

Rolldown
STO 2
; Reg[2] is used to hold r
Rolldown
STO 1
; Reg[1] is used to hold R
RCL 1
RCL 2
+
STO 4

;
;
;
;

X = R
X = r, Y = R
X = R+r
Reg[4] is used to hold (R+r)

+0.
STO 0

; initial theta = t value


; Reg[0] is used to hold theta

LBL 1

; we return here for each new data point

; We first compute the y axis value.


RCL
RCL
sin
*
RCL
RCL
/
RCL
*
sin
RCL
*
-

4
0
4
2
0
3

;
;
;
;
;
;
;
;
;
;
;
;
;

X
X
X
X
X
X
X
X
X
X
X
X
X

=
=
=
=
=
=
=
=
=
=
=
=
=

R+r
t, Y = R+r
sin(t), Y = R+r
(R+r)*sin(t)
R+r, Y = (R+r)*sin(t)
r, Y = R+r, Z = (R+r)*sin(t)
(R+r)/r, Y = (R+r)*sin(t)
t, Y = (R+r)/r, Z = (R+r)*sin(t)
((R+r)/r)*t, Y = (R+r)*sin(t)
sin(((R+r)/r)*t), Y = (R+r)*sin(t)
p, Y = sin(((R+r)/r)*t), Z = (R+r)*sin(t)
p*sin(((R+r)/r)*t), Y = (R+r)*sin(t)
(R+r)*sin(t) - p*sin(((R+r)/r)*t)

; We now leave the y axis value on the operand stack


; as we compute the x axis value.
RCL
RCL
cos
*
RCL
RCL
/
RCL
*
cos
RCL
*
-

4
0
4
2
0
3

;
;
;
;
;
;
;
;
;
;
;
;
;

X
X
X
X
X
X
X
X
X
X
X
X
X

=
=
=
=
=
=
=
=
=
=
=
=
=

R+r
t, Y = R+r
cos(t), Y = R+r
(R+r)*cos(t)
R+r, Y = (R+r)*cos(t)
r, Y = R+r, Z = (R+r)*cos(t)
(R+r)/r, Y = (R+r)*cos(t)
t, Y = (R+r)/r, Z = (R+r)*cos(t)
((R+r)/r)*t, Y = (R+r)*cos(t)
cos(((R+r)/r)*t), Y = (R+r)*cos(t)
p, Y = cos(((R+r)/r)*t), Z = (R+r)*cos(t)
p*cos(((R+r)/r)*t), Y = (R+r)*cos(t)
(R+r)*cos(t) - p*cos(((R+r)/r)*t)

; At this point the X stack location holds the x axis value


; and the Y stack location holds the y axis value so we
; are ready for the SumG keystroke.
SumG
; add the new x,y point to the graph
+1.
STO+ 0 ; increment theta
GTO 1

You might also like