25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

N.Wirth. <strong>Algorithms</strong> <strong>and</strong> <strong>Data</strong> <strong>Structures</strong>. Oberon version 103<br />

3.3 Two Examples of Recursive Programs<br />

The attractive graphic pattern shown in Fig. 3.4 consists of a superposition of five curves. These curves<br />

follow a regular pattern <strong>and</strong> suggest that they might be drawn by a display or a plotter under control of a<br />

computer. Our goal is to discover the recursion schema, according to which the drawing program might be<br />

constructed. Inspection reveals that three of the superimposed curves have the shapes shown in Fig. 3.3;<br />

we denote them by H 1 , H 2 <strong>and</strong> H 3 . The figures show that H i+1 is obtained by the composition of four<br />

instances of H i of half size <strong>and</strong> appropriate rotation, <strong>and</strong> by tying together the four H i by three connecting<br />

lines. Notice that H 1 may be considered as consisting of four instances of an empty H 0 connected by three<br />

straight lines. H i is called the Hilbert curve of order i after its inventor, the mathematician D. Hilbert<br />

(1891).<br />

H 1 H 2<br />

H 3<br />

Fig. 3.3. Hilbert curves of order 1, 2, <strong>and</strong> 3<br />

Since each curve H i consists of four half-sized copies of H i-1 , we express the procedure for drawing H i<br />

as a composition of four calls for drawing H i-1 in half size <strong>and</strong> appropriate rotation. For the purpose of<br />

illustration we denote the four parts by A, B, C <strong>and</strong> D, <strong>and</strong> the routines drawing the interconnecting lines by<br />

arrows pointing in the corresponding direction. Then the following recursion scheme emerges (see Fig.<br />

3.3).<br />

A: D ← A ↓ A → B<br />

B: C ↑ B → B ↓ A<br />

C: B → C ↑ C ← D<br />

D: A ↓ D ← D ↑ C<br />

Let us assume that for drawing line segments we have at our disposal a procedure line which moves a<br />

drawing pen in a given direction by a given distance. For our convenience, we assume that the direction be<br />

indicated by an integer parameter i as 45 × i degrees. If the length of the unit line is denoted by u, the<br />

procedure corresponding to the scheme A is readily expressed by using recursive activations of analogously<br />

designed procedures B <strong>and</strong> D <strong>and</strong> of itself.<br />

PROCEDURE A (i: INTEGER);<br />

BEGIN<br />

IF i > 0 THEN<br />

D(i-1); line(4, u);<br />

A(i-1); line(6, u);<br />

A(i-1); line(0, u);<br />

B(i-1)<br />

END<br />

END A<br />

This procedure is initiated by the main program once for every Hilbert curve to be superimposed. The

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!