# Chapter 9 – Further Programs (Ellipses)

**Posted:**April 20, 2013

**Filed under:**Further Programs, ZX Spectrum Astronomy |

**Tags:**Astronomy, Ellipse, Ellipse Routines, Further Programs, Maurice Gavin, Programming, Sinclair Basic, Trigonometry, ZX Spectrum Astronomy Leave a comment

The Messier List, check out the fake comets

Telescope, the facts about telescopes, binoculars, cameras and astronomy

Star Tester, a quiz

Ellipses, various ellipses

Spectrum World Map, a map of the world in CHR$ CODE.

## Ellipses

For astronomical presentations an ‘ellipse’ command is sadly missed from the Spectrum (and most other micros for that matter) but the following short routines will suffice, using the machine’s trigonometry functions of SIN and COS. These routines prove much less cumbersome than some published examples using complex algebraic formulae. Also keeping the routine brief ensures that the results are executed quickly, so speeding up the PLOTting process.

Effectively an ellipse is a circle seen at an angle so that its form is compressed. With minor changes, each routine can be made to PLOT in a clockwise or anticlockwise direction with the long or major axis either vertical or horizontal.

*The Ellipse routines*

Key in and RUN the first routine called Ellipse (see Figure 9.6). You will notice that in most cases the variable names are sufficiently long to describe their function in the program:

xaxis = x (horizontal) coordinate position

yaxis = y (vertical) coordinate position

rad = maximum radius of ellipse called semimajor axis

tilt = INPUT angle of tilt to a circle

angle = compression factor to produce an ellipse

The formula in the variable ‘angle’ contains a small make-weight value of 0.1 added to a variable ’tilt’. This is done to stop the program crashing if an INPUT of 0° is made (a circle shown edge-on). The value returned by 0° would be a truly infinite number exceeding the Spectrum’s numeric capacity which is limited to a range from 3*10^{-39} to 7*10^{38}.

The short routine called Ellipse Angle Values (Figure 9.7) shows what values are returned by the formula called angle between 0° and 90° in whole degree steps.

In reality, 0° is made to return a value as if 0.10, ie 1/10° or 6′ arc, and 0.1 is added throughout the sequence. The Spectrum’s PLOTting accuracy is well below this small angle and the results (as PLOTted) are indistinguishable from whole degrees.

It should now be evident that if the somewhat improbable INPUT angle for tilt of -0.1° is made, the Ellipse program will crash because:

– 0.1 + 0.1 = 0°

**Figure 9.6**

An ellipse from full circle to edge-on is produced via this routine.

**10 PRINT “Ellipse @ tilt=”;
30 LET xaxis=132: LET yaxis=91
40 LET radius=70
50 INPUT “0CHR$130 to 90CHR$130 “,tilt
60 PRINT tilt;”CHR$130 ”
70 PRINT AT 10,16;”+”
80 LET angle=1/SIN ((.1+tilt)/180*PI)
90 INPUT “step (1 to 10)”,s
100 FOR f=0 TO PI*2 STEP 1/s/10
110 PLOT xaxis+SIN f*radius,yaxis+COS f*radius/angle
120 NEXT f**

**Figure 9.7**

**1 REM Ellipse angle values
5 FOR x=0 TO 90
7 LET tilt=x
10 LET angle=1/SIN ((.1+tilt)/180*PI)
20 PRINT angle,tilt;CHR$ 130
30 NEXT x**

*Ellipse 4*

This routine is precisely the same as the above routine but is used to demonstrate the simultaneous PLOTting of four ellipses. Againthe tu.o all four ellipses and the PLOTting frequency via variables can be selected by he two INPUT commands. Figure 9.8 shows two example COPYs from the screen.

**Figure 9.8**

Samples of ellipses PLOTted in different directions and long (major) axes.

The ellipses are made to PLOT in a clockwise or anticlockwise direction by the exchange of the SIN and COS expressions in each half of the PLOT routine. If the routine commences with the SIN function, PLOTting starts at the 12 o’clock position: if it commences with COS, PLOTting starts at the 3 o’clock position. If the variable angle is in the first half of the PLOT routine, ellipse major axis is vertical and, if it is in the second half, the major axis is horizontal.

Line 100 contains the FOR/NEXT f loop used for PLOTting the ellipse in the form:

FOR f = 0 TO PI*2 STEP l/s*3

where 0 to PI*2 produces a full ellipse. If the expression were to read:

FOR f = PI TO PI*3 STEP…

then a full ellipse would still be PLOTted, but commencing at 180°, or, on the opposite side, at the 6 o’clock or 9 o’clock locations respectively.

Try changing the values in the FOR/NEXT f loop to test this.

**10 PRINT “Ellipse 4 @ tilt=”;
30 LET x=132: LET y=76
31 LET x1=x*.6: LET y1=y*1.5
32 LET x2=x*1.2: LET y2=y*1.5
33 LET x3=x*.6: LET y3=y*.5
34 LET x4=x*1.2: LET y4=y*.5
40 LET r=35
50 INPUT “0CHR$130 to 90CHR$130 “,tilt
60 PRINT tilt;”CHR$130 “: PAPER 5
70 PRINT “clockwise”,”anticlockwise”
75 PRINT “axis 1″,”2″
77 PRINT AT 12,0;”^axis 3″,”4”
79 PAPER 7
80 LET angle=1/SIN ((.1+tilt)/180*PI)
90 INPUT “step (1 to 10)”,s
100 FOR f=0 TO PI*2 STEP 1/s/3
111 PLOT x1+SIN f*r,y1+COS f*r/angle
112 PLOT x2+COS f*r,y2+SIN f*r/angle
113 PLOT x3+SIN f*r/angle,y3+COS f*r
114 PLOT x4+COS f*r/angle,y4+SIN f*r
120 NEXT f**

*Solid Ellipse*

This routine uses the DRAW command to produce solid (filled in) ellipses. It actually executes the results about four times quicker than the previous ellipse routines, where the STEP intervals are sufficiently frequent to produce a continuous unbroken outline. This is because it is only necessary to compute one quadrant of an ellipse and to mirror the results in the remaining three quadrants sequentially. Different variable names have been assigned to this routine but the principles are precisely as before. You will notice that, as it is only necessary to compute one quadrant, the FOR/NEXT loop reads:

FOR f = 0 TO PI/2 STEP .012

only one quarter the length of… 0 TO PI*2 … used previously. The STEP interval is fixed at 0.012 as this was found, with this routine, to give the solid form required. If the ellipse is not to fill the Spectrum screen then a larger value can be tested.

The variables a and b are the x and y coordinate positions for the ellipse, where variable e provides the necessary compression to the vertical or minor axis. Line 130 PLOTs the left hand upper outline of the ellipse and Line 140 does the same for the right hand side. The DRAW routines that follow the PLOT commands in the form:

DRAW 0, – b*2

DRAW a vertical line equal to twice the value of b in a downwards direction, so completing the lower portion of the ellipse. Line 200 completes the routine by marking the major (horizontal) and minor (vertical) axes of the ellipse using the OVER command.

The ellipses are made to PLOT in a clockwise or anticlockwise direction by the exchange of the SIN and COS expressions in each half of the PLOT routine. If the routine commences with the SIN function, PLOTting starts at the 12 o’clock position: if it commences with COS, PLOTting starts at the 3 o’clock position. If the variable angle is in the first half of the PLOT routine, ellipse major axis is vertical and, if it is in the second half, the major axis is horizontal.

Line 100 contains the FOR/NEXT f loop used for PLOTting the ellipse in the form:

FOR f = 0 TO PI*2 STEP l/s*3

where 0 to PI*2 produces a full ellipse. If the expression were to read:

FOR f = PI TO PI*3 STEP…

then a full ellipse would still be PLOTted, but commencing at 180°, or, on the opposite side, at the 6 o’clock or 9 o’clock locations respectively. Try changing the values in the FOR/NEXT f loop to test this.

**10 PRINT “Solid Ellipse”
20 INPUT “tilt “;z: PRINT “tilt=”;z;”CHR$130 ”
30 LET e=SIN ((.1+z)/180*PI)
40 LET x=255/2: LET y=175/2
100 FOR f=0 TO PI/2 STEP .012
110 LET a=INT (SIN f*y)
120 LET b=INT (COS f*y*e)
130 PLOT x-a,y+b: DRAW 0,-b*2
140 PLOT x+a,y+b: DRAW 0,-b*2
150 NEXT f
200 OVER 1: PLOT 0,y: DRAW 255,0: PLOT x,0: DRAW 0,175: OVER 0**

*Inclined Ellipse*

There are times when an ellipse is required with the major axis, neither vertical nor horizontal but in an intermediate position. This final routine ‘indicates a method of achieving this aim to a modest extent.

Figure 9.9 shows a screen COPY from the program and typical INPUTs and ellipses as PLOTted. The INPUT values for tilt and inclination (rotation of the image) may be from -90° to 90° in each case. Line 90 does the actual work of computing both these factors in the final part of the expression:

…/t + a/z

where variables t (for tilt) and z (for rotation) are the reciprocal SIN values as INPUT. The PLOTting interval (STEP 0.05) and maximum horizontal displacement (value 60 in Lines 80 and 90) are fixed but can be amended to suit the requirements.

The Ellipse routine is used frequently, sometimes in a modified form, for various programs in this book, including Saturn’s Rings, Galaxy, Binary-star Orbits. The expression ‘ellipse’ is in common use to describe the shape of planetary and particular cometary orbits and the shape of optical (lens and mirror) surfaces. Ellipse is one of a family of forms in the conic section which includes the circle, parabola and hyperbola.

**Figure 9.9**

Typical effects of inclination (rotation of major axis) of an ellipse via this routine.

**10 PRINT “Inclined ellipse”
20 INPUT “tilt “;t,”incline “;z
30 PRINT “tilt “;t;”CHR$130 ”
40 PRINT “incl “;z;”CHR$130 “”
50 LET z=1/SIN ((.1+z)/180*PI)
60 LET t=1/SIN ((.1+t)/180*PI)
70 FOR n=0 TO PI*2 STEP .05
75 PLOT OVER 1;140,80
80 LET a=SIN n*60
90 LET b=COS n*60/t+a/z
100 PLOT INT (140+a),INT (80+b)
110 NEXT n: GO TO 20**

*Ellipses and Kepler’s Orbits*

One cautionary note should be observed in using the Ellipse routines to simulate a planet in true elliptical orbit about the Sun, perhaps — where the Sun cannot be in the centre of the ellipse but at one of the foci. Here Kepler’s 2nd Law of Planetary Motion applies and the planet will ‘speed-up’ as it approaches the Sun and ‘slow-down’ as it recedes. The Ellipse program will not in this case give a perfect account of itself but on many occasions the effect can be reasonably convincing. For a correct simulation of Kepler’s 2nd Law, see the Kepler’s Orbits program in Chapter 5, and the sample screen COPYs.