Listing 2

Triangular database code

var
  DB: array[0..8384] of TTriple;
  {Triangular array: Vertices(MaxPlys+1) entries}

  NumberOfVertices, TopRow: word;

function Vertices(N: word): word;
{ Vertices in an equilateral triangle with EdgeLength = N-1 }
begin
  Vertices := (Sqr(N) + N) shr 1;
end;

{*
   TopRow = EdgeLength + 1
     It is the number of vertices on each edge; coordinate values
     range from 0..TopRow-1.

   NumberOfVertices = Vertices(TopRow)
     Total vertices in use.
*}

function Loc(const V: TVertex): word;
begin
  Loc := NumberOfVertices - Vertices(TopRow - V.AB) + V.BC;
  {      ^^^^^^^^^^^^^^^^^^ This is actually NOT necessary and just
                            wastes cycles, but I have retained it
                            for compatability with FL2 .FL files. }
end;

procedure SetTriple(var V: TVertex; var T: Triple);  { DB[V] := T }
begin
  DB[Loc(V)] := T;
end;

function GetTriple(const V: TVertex): TTriple;        { DB[V] }
begin
  Result := DB[Loc(V)];
end;