Polygons

Date: 198x
Type: Program
Platform(s): TS 2068
Tags: Demo

This program draws regular polygons from 4 to 20 sides and renders every possible diagonal connecting non-adjacent vertices. For each polygon, it computes vertex coordinates using trigonometric functions with a fixed radius of 70 units centered at screen position (128, 90). The number of diagonals displayed matches the combinatorial formula n*(n-3)/2, which is printed alongside the side count at the bottom of the screen. The outer loop iterates over polygon sizes, and nested FOR loops over vertex index pairs use PLOT/DRAW to connect every vertex combination, naturally including both sides and diagonals.


Program Analysis

Program Structure

The program is organized as a straightforward nested loop sequence with no subroutines or branching. The outer loop at line 50 iterates n from 4 to 20, driving the entire animation. Within each iteration, a vertex-generation loop (lines 70–100) populates coordinate arrays, followed by a CLS and display phase, and then a double-nested drawing loop (lines 130–160).

  1. Initialization (lines 10–40): Sets display colors, prints a title, defines the angular constant i, and dimensions two arrays.
  2. Outer polygon loop (lines 50–170): Iterates over polygon sizes from 4 to 20 sides.
  3. Vertex computation (lines 60–100): Calculates Cartesian screen coordinates for each vertex using SIN/COS.
  4. Drawing phase (lines 110–160): Clears the screen, prints the side/diagonal count, then draws all vertex-to-vertex line segments.

Coordinate Generation

Vertex positions are computed in lines 80–90 using the expressions 128+70*SIN(i*a/n) and 90+70*COS(i*a/n). The constant i=6.3 (line 30) approximates 2π (actual value ≈ 6.2832), causing the polygon to complete very nearly a full revolution. Using a slightly imprecise value of 2π means the first and last computed vertices do not perfectly coincide; however, since the vertex at index 1 is set explicitly at line 60 and the drawing loop runs from t=1 TO n+1, the polygon closes adequately in practice. Note that SIN drives the X axis and COS drives the Y axis, consistent with a clockwise vertex ordering.

Drawing All Diagonals (and Sides)

The nested loops at lines 130–160 iterate over all pairs (t, q) where q >= t, connecting every vertex to every other vertex. This draws not only the diagonals but also the polygon’s sides themselves. The total number of line segments drawn equals the triangular number C(n+1, 2), which for n vertices is n*(n+1)/2. The program correctly displays only the diagonal count using the formula n*(n-3)/2 at line 120, distinguishing diagonals from sides in the printed label while the graphics show all connections.

Key BASIC Idioms and Techniques

  • Array pre-allocation: DIM x(25): DIM y(25) at line 40 allocates enough space for polygons up to 20 sides (requiring 21 entries including the repeated first vertex).
  • PLOT/DRAW for lines: Line 150 uses PLOT x(t),y(t): DRAW x(q)-x(t),y(q)-y(t), the standard idiom for drawing a line segment between two absolute coordinates by converting the destination to a relative offset.
  • Inline status display: Line 120 uses PRINT AT 21,0 to place the polygon statistics on the bottom screen row, keeping the graphics area unobstructed.
  • Padding spaces in PRINT: The string " " between the side count and diagonal count in line 120 provides manual column separation rather than using TAB.

Notable Observations and Anomalies

The angular constant i=6.3 is a minor inaccuracy: 2π ≈ 6.28318. This means each computed polygon is very slightly open — the last computed vertex at index n+1 is not exactly the same as vertex 1. For small n the gap is visually imperceptible, but it technically means the closing segment drawn between vertex n+1 and vertex 1 is a hair shorter than if 2π were used precisely.

There is no PAUSE or INKEY$ between polygon iterations, so the program advances through all 17 polygons as fast as the drawing completes — the display of complex polygons (near n=20) will be slow enough to observe, while simpler ones may flash by quickly. The program halts naturally when the outer FOR loop at line 50 finishes.

Diagonal Count Formula

Sides (n)Diagonals n*(n-3)/2Total segments drawn
4210
5515
6921
103555
20170210

Content

Appears On

Library tape of the Indiana Sinclair Timex User’s Group.

Related Products

Related Articles

Related Content

Image Gallery

Polygons

Source Code

   10 INK 7: PAPER 0: BORDER 0: CLS 
   20 PRINT "diagonals of a polygon"
   30 LET i=6.3
   40 DIM x(25): DIM y(25)
   50 FOR n=4 TO 20
   60 LET x(1)=128: LET y(1)=90
   70 FOR a=1 TO n
   80 LET x(a+1)=128+70*SIN (i*a/n)
   90 LET y(a+1)=90+70*COS (i*a/n)
  100 NEXT a
  110 CLS 
  120 PRINT AT 21,0;n;" Sides";"      ";n*(n-3)/2;" diagonals"
  130 FOR t=1 TO n+1
  140 FOR q=t TO n+1
  150 PLOT x(t),y(t): DRAW x(q)-x(t),y(q)-y(t)
  160 NEXT q: NEXT t
  170 NEXT n

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

People

No people associated with this content.

Scroll to Top