INTERPOLATE

This file is part of and CATS Library Tape 3. Download the collection to get this file.
Developer(s): Nick Hampshire
Date: 198x
Type: Program
Platform(s): TS 2068
Tags: Graphics

This program reads twelve (x, y) data pairs from a DATA statement and renders a linearly interpolated graph on screen. The graph is drawn within a defined viewport bounded by pixel coordinates, with axis tick marks plotted along the baseline and a full-screen border drawn via a subroutine. Scaling is computed by finding the minimum and maximum values across both axes, then deriving scale factors `a` and `b` that map data coordinates to screen pixels. The interpolation step (line 670–710) walks between each pair of adjacent data points in increments of 0.03, computing intermediate y values using linear interpolation and plotting each resulting screen pixel.


Program Analysis

Program Structure

The program is organized into four logical phases: an introduction/setup block (lines 1–4), a graph initialization and data-loading block (lines 60–180), a scaling and drawing block (lines 200–730), and a border-drawing subroutine (lines 1000–1060).

  1. Lines 1–4: Display a description and wait for a keypress using the IF INKEY$="" THEN GO TO 3 polling idiom.
  2. Lines 60–180: Set colors, call the border subroutine, dimension arrays, and read 12 (x, y) pairs from DATA at line 180.
  3. Lines 200–730: Define viewport parameters, draw the bounding box and tick marks, find data extents, compute scale factors, plot data points, and interpolate between them.
  4. Lines 1000–1060: Border subroutine — draws a full-screen rectangle using PLOT/DRAW.

Viewport and Coordinate System

The viewport is defined by four pixel boundaries set at line 220:

VariableValueMeaning
xr15Right edge of left (x-axis origin side)
xl240Left edge of right boundary
yt100Top of graph area
yb30Bottom of graph area

Note that xr and xl are named counterintuitively — xr (value 15) is the leftmost pixel and xl (value 240) is the rightmost. The bounding box is drawn at lines 310–350, inset by 5 pixels on each side. Tick marks along the x-axis are plotted at lines 365–370 at pixel positions separated by xi = (xl-xr)/(dx-dn), i.e., evenly spaced across the 11 data intervals.

Scaling and Data Normalization

Lines 410–520 find the data extents by scanning all points: y1 and x1 accumulate maxima, while y2 and x2 accumulate minima. Scale factors are then computed:

  • a = (x1-x2)/(xl-xr) — maps data x-range onto pixel x-range
  • b = (y1-y2)/(yt-yb) — maps data y-range onto pixel y-range

These scale factors are used in lines 620–630 and again in 690–700 to convert from data space to screen space.

Interpolation Algorithm

The outer loop (lines 610–730) iterates over each data point. For each segment between point i and point q = i + sp, an inner loop (line 670) steps through x-values in the data domain with a fine increment of 0.03. The interpolated y-value at each step is computed by line 680:

y3 = ((y(q)-y(i))/(x(q)-x(i))) * (j-x(i)) + y(i)

This is standard linear (first-order) interpolation. The result is then mapped to pixel coordinates using INT for both x and y (lines 690–700) before plotting. INT is used here rather than rounding, which could introduce a sub-pixel bias of up to −1 in the negative direction, though this is unlikely to be visually significant for this data.

Key BASIC Idioms and Techniques

  • Keypress wait: Line 3 uses IF INKEY$="" THEN GO TO 3 — the standard busy-poll pattern for waiting on user input.
  • Subroutine for border: The border-drawing code is isolated in a subroutine at line 1000, called early at line 90, keeping the main logic clean.
  • Step variable sp: The step value for data iteration is parameterized as sp=1 (line 200), allowing easy modification to skip points.
  • Early termination: Line 660 uses STOP when q > dx to prevent the inner loop from accessing an out-of-bounds index on the last iteration of the outer loop.
  • Data in a dedicated line: All 24 data values are packed into a single DATA statement at line 180, keeping the program compact.

Notable Anomalies

The variable naming for the viewport is somewhat confusing: xr holds the smaller (left) pixel coordinate and xl holds the larger (right) pixel coordinate, which is the reverse of what the suffixes “r” (right) and “l” (left) would conventionally suggest. This does not affect correctness but may puzzle readers trying to follow the geometry.

The last data value for month 12 is y(12) = 0, which is far below the range of all other y-values (228–266). This will drag the y-minimum to 0, compressing the graph considerably. This appears to be intentional data or a quirk of the source book’s demo dataset.

Content

Appears On

Balance your checkbook, decode cryptograms, or trace your BASIC program's execution path — Tape 3 is where serious productivity meets hands-on programming. Highlights include a word processor, a flat-file database, machine code loaders, and a complete darkroom formulary.

Related Products

Related Articles

Related Content

Image Gallery

Source Code

    1 REM interpolate
    2 PRINT "Here is a program that sets up  an interpolated graph of infor- mation that you supply in the   data statement of line 180.     Press any key for a demo.       Taken from Hampshire's ""Color   Graphics""." 
    3 IF INKEY$="" THEN GO TO 3
    4 CLS 
   60 INK 0: PAPER 7: BORDER 3
   90 GO SUB 1000
  110 DIM x(12): DIM y(12)
  120 FOR i=1 TO 12
  130 READ x(i): READ y(i)
  160 NEXT i
  180 DATA 1,266,2,241,3,247,4,245,5,239,6,228,7,225,8,230,9,240,10,246,11,240,12,0
  200 LET dn=1: LET dx=12: LET sp=1
  220 LET xl=240: LET xr=15: LET yt=100: LET yb=30
  310 PLOT xr-5,yb-5
  320 DRAW (xl-xr+10),0
  330 DRAW 0,(yt-yb+10)
  340 DRAW -(xl-xr+10),0
  350 DRAW 0,-(yt-yb+10)
  355 LET xi=(xl-xr)/(dx-dn)
  360 FOR x=xr TO xl STEP xi
  365 PLOT x,yb-6: PLOT x,yb-7
  370 NEXT x
  380 PRINT AT 2,7;"Interpolated Graph"
  410 LET y1=-1000000
  420 LET y2=1000000
  430 LET x1=y1: LET x2=y2
  440 FOR i=dn TO dx STEP sp
  450 IF y1<y(i) THEN LET y1=y(i)
  460 IF y2>y(i) THEN LET y2=y(i)
  470 IF x1<x(i) THEN LET x1=x(i)
  480 IF x2>x(i) THEN LET x2=x(i)
  490 NEXT i
  510 LET a=(x1-x2)/(xl-xr)
  520 LET b=(y1-y2)/(yt-yb)
  610 FOR i=dn TO dx STEP sp
  620 LET x=(xr+(x(i)-x2)/a)
  630 LET y=((y(i)-y2)/b+yb)
  640 PLOT x,y
  650 LET q=i+sp
  660 IF q>dx THEN STOP 
  670 FOR j=x(i) TO x(q) STEP .03
  680 LET y3=((y(q)-y(i))/(x(q)-x(i)))*(j-x(i))+y(i)
  690 LET x=INT (xr+(j-x2)/a)
  700 LET y=INT ((y3-y2)/b+yb)
  710 PLOT x,y
  730 NEXT j: NEXT i
 1000 REM border
 1010 PLOT 0,0
 1020 DRAW 255,0
 1030 DRAW 0,175
 1040 DRAW -255,0
 1050 DRAW 0,-175
 1060 RETURN 

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

Scroll to Top