25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Fig. 3.5. Sierpinski curves S 1 <strong>and</strong> S 2 .<br />

The base pattern of the Sierpinski curves is<br />

S: A B C D <br />

<strong>and</strong> the recursion patterns are (horizontal <strong>and</strong> vertical arrows denote lines of double length.)<br />

A: A B → D A<br />

B: B C ↓ A B<br />

C: C D ← B C<br />

D: D A ↑ C D<br />

If we use the same primitives for drawing as in the Hilbert curve example, the above recursion scheme is<br />

transformed without difficulties into a (directly <strong>and</strong> indirectly) recursive algorithm.<br />

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

BEGIN<br />

IF k > 0 THEN<br />

A(k-1); Draw.line(7, h); B(k-1); Draw.line(0, 2*h);<br />

D(k-1); Draw.line(1, h); A(k-1)<br />

END<br />

END A<br />

This procedure is derived from the first line of the recursion scheme. Procedures corresponding to the<br />

patterns B, C <strong>and</strong> D are derived analogously. The main program is composed according to the base<br />

pattern. Its task is to set the initial values for the drawing coordinates <strong>and</strong> to determine the unit line length h<br />

according to the size of the plane. The result of executing this program with n = 4 is shown in Fig. 3.6.<br />

VAR h: INTEGER; (* ADenS33_Sierpinski *)<br />

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

BEGIN<br />

IF k > 0 THEN<br />

A(k-1); Draw.line(7, h); B(k-1); Draw.line(0, 2*h);<br />

D(k-1); Draw.line(1, h); A(k-1)<br />

END<br />

END A;

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

Saved successfully!

Ooh no, something went wrong!