begin
An array of boolean flags .
Integer variables and .
Variables and comprising a point in space.
Another array of flags .
Boolean flags , , and .
The variables , , , and array are used to keep track of the best fit as the algorithm proceeds.
Set all the to .
For each point :
begin
if set to , otherwise set it to .
if set to , otherwise set it to .
if set to , otherwise set it to .
if set to , otherwise set it to .
If the 's are either all true or all false, the line misses the root rectangle, otherwise they intersect. If they intersect, set to , to and to .
Call the procedure with arguments as follows:
Procedure ( array of flags , arrays of intercepts and , variables and , subdivision level , accumulator value , line of descent list ):
begin
Array of intercepts with elements.
Accumulators , , and , initialised to zero.
Variable .
Nine boolean flags , , , , , , , , .
Boolean flag , initialised to .
Sum the votes for the child rectangles: For each such that :
begin
Set to .
If then:
begin
If then:
begin
If then set to .
Repeat for intercept and flags , and .
Repeat for intercept and flags , and .
If , , and are not all either or :
begin
Set flag to .
Repeat for flags , , and , accumulator and flag .
Repeat for flags , , and , accumulator and flag .
Repeat for flags , , and , accumulator and flag .
Subdivide child rectangles receiving greater than or equal to votes: If :
begin
begin
Call with arguments , , , , , , and extended line of descent.
Set flag to .
Repeat with accumulator , index pair [-1,1], flag list , intercept arrays and , and values and .
Repeat with accumulator , index pair [1,-1], flag list , intercept arrays and , and values and .
Repeat with accumulator , index pair [1,1], flag list , intercept arrays and , and values and .
If is still (i.e. no further subdivision of this rectangle), test for best fit so far:
begin
Set to .
Calculate centre of rectangle in direction by calling procedure with the line of descent as argument. The result is copied into .
Set to .
Copy array of flags into array .
Procedure ( line of descent list ): begin
Set to zero.
While line of descent not traversed (i.e. root rectangle not yet reached):
begin
Replace old value of
:
Add centre -coordinate of root cube to
:
At the end of the algorithm is the highest level of subdivision reached, the highest accumulator value at level , while and hold the best fit line parameters.
Note that the points are never used by the procedure . All the information about the points is contained in the intercept arrays. The index part of the line of descent is actually superflous since the positions of rectangles are passed from parent to child via and .