--- title: "INTERPOLATE" id: 54531 type: "computer_media" slug: "interpolate" url: "http://localhost/computer_media/interpolate/" markdown_url: "http://localhost/computer_media/interpolate.md" published_at: "2024-06-02T16:31:54+00:00" modified_at: "2026-03-30T21:43:19+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2024/06/Interpolated-Graph-1.png" excerpt: "A line-by-line interpolated graph builder that scales and plots any twelve data points with smooth linear interpolation between each pair." category: - name: "Archived Media" slug: "archived-media" taxonomy: "category" url: "http://localhost/category/archived-media/" post_tag: - name: "Downloadable" slug: "downloadable" taxonomy: "post_tag" url: "http://localhost/tag/downloadable/" - name: "TS 2068" slug: "ts2068" taxonomy: "post_tag" url: "http://localhost/tag/ts2068/" model: - name: "Timex/Sinclair 2068" slug: "ts-2068" taxonomy: "model" url: "http://localhost/model/ts-2068/" indiv: - name: "Nick Hampshire" slug: "nick-hampshire" taxonomy: "indiv" url: "http://localhost/indiv/nick-hampshire/" genre: - name: "Graphics" slug: "graphics" taxonomy: "genre" url: "http://localhost/type/graphics/" media_contents: - id: 51213 title: "CATS Library Tape 3" type: "computer_media" url: "http://localhost/computer_media/cats-library-tape-3/" media_type: "Program" programmers: - name: "Nick Hampshire" slug: "nick-hampshire" taxonomy: "indiv" url: "http://localhost/indiv/nick-hampshire/" mediadate: "198x" images: - url: "http://localhost/wp-content/uploads/2024/06/Interpolated-Graph-1.png" - url: "http://localhost/wp-content/uploads/2024/06/Interpolated-Graph.png" related_content: - id: 20953 title: "Timex Sinclair Color Graphics" type: "book" url: "http://localhost/book/timex-sinclair-color-graphics/" media_type_tags: "Graphics" --- # INTERPOLATE 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: | Variable | Value | Meaning | | --- | --- | --- | | `xr` | 15 | Right edge of left (x-axis origin side) | | `xl` | 240 | Left edge of right boundary | | `yt` | 100 | Top of graph area | | `yb` | 30 | Bottom 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. ## 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 y1y(i) THEN LET y2=y(i) 470 IF x1x(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 ```