Sphere 3D

Developer(s): Real Gagnon
Date: 1985
Type: Program
Platform(s): TS 2068
Tags: Demo

This program plots a Lissajous-style parametric curve on the screen using the equations x = 125 + g·sin(a) and y = 87 + g·sin(0.95a)·cos(a), where g is a user-supplied size value and a steps from 0 to 125.7 in increments of 0.03. The resulting shape is a closed or near-closed figure-eight or knot pattern depending on the irrational frequency ratio (0.95). After drawing, all plotted points are stored in the string array b$, with each coordinate packed as a single CHR$ character, allowing the curve to be redrawn from memory without recalculating. The program uses a 4200-element two-character-wide string array to buffer coordinates, exploiting the fact that screen coordinates fit within the 0–255 range of a single byte stored as a character code. Redrawing from the buffer on the second pass demonstrates an efficient coordinate caching technique using CODE to recover the numeric values from stored characters.


Program Analysis

Program Structure

The program divides into three logical phases:

  1. Initialization (line 5): Sets display attributes, clears the screen, and dimensions the coordinate buffer array.
  2. Plotting loop (lines 20–50): Prompts for a size parameter, iterates through the parametric angle, computes and plots each point, and stores coordinates in the buffer.
  3. Redraw phase (lines 100–110): Waits for a keypress, clears the screen, and replots the entire curve from the buffered data.

Parametric Equations

The curve is defined by:

  • x = 125 + g * SIN(a)
  • y = 87 + g * SIN(0.95 * a) * COS(a)

The parameter a runs from 0 to 125.7 in steps of 0.03, giving approximately 4190 points. The irrational frequency ratio of 0.95 (= 19/20) means the x and y oscillations have a near but not exactly integer relationship, producing a dense, slowly precessing Lissajous-like figure. The center is offset to approximately the middle of the 256×176 pixel display area.

Coordinate Buffer Technique

The array b$(4200,2) is declared as a two-column string array, where each element holds a single character. The x coordinate is stored as CHR$(x) in column 1, and the y coordinate as CHR$(y) in column 2. During the redraw phase, CODE b$(a,1) and CODE b$(a,2) recover the numeric values. This is an efficient packing scheme that exploits the 0–255 range of a character code matching the screen coordinate space exactly, avoiding any floating-point recalculation on the second pass.

Key BASIC Idioms

LineIdiomPurpose
5DIM b$(4200,2)Allocates a fixed-size 2-column string array as a coordinate store
40CHR$(x) / CHR$(y)Packs integer coordinates into single characters for compact storage
100FLASH 1 / PAUSE 0Attracts attention then waits for any keypress before redrawing
110CODE b$(a,1)Unpacks stored character back to its numeric coordinate value

Notable Techniques

The use of PRINT #0; on line 100 directs the flashing prompt to the lower status area (stream 0) rather than the main screen, preserving the drawn curve while awaiting user input. The counter variable c tracks how many points were actually plotted, so the redraw loop on line 110 iterates exactly c-1 times rather than assuming all 4200 slots are populated.

Potential Issues

With a step of 0.03 over a range of 125.7, the loop generates approximately 4190 iterations. The buffer is sized at 4200, leaving a small safety margin of about 10 elements. However, if g is large enough to push computed x or y values outside the 0–255 range, CHR$ would produce an error. No bounds checking is performed on the coordinate values before storing them, which could cause an out-of-range error for very large size inputs.

Content

Appears On

Related Products

Related Articles

Short program to plot a 3D sphere.

Related Content

Image Gallery

Source Code

    5 BORDER 0: PAPER 0: INK 5: CLS : DIM b$(4200,2)
   20 LET c=1: INPUT "Size:";g
   30 FOR a=0 TO 125.7 STEP .03: LET y=87+g*SIN (a*.95)*COS a: LET x=125+g*SIN a
   40 PLOT x,y: LET b$(c,1)=CHR$ (x): LET b$(c,2)=CHR$ (y)
   50 LET c=c+1: NEXT a
  100 PRINT #0; FLASH 1;"Press a key...": PAUSE 0: CLS 
  110 FOR a=1 TO c-1: PLOT CODE b$(a,1),b$(a,2): NEXT a

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

Scroll to Top