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 104<br />

main procedure determines the initial point of the curve, i.e., the initial coordinates of the pen denoted by<br />

x0 <strong>and</strong> y0, <strong>and</strong> the unit increment u. The square in which the curves are drawn is placed into the middle of<br />

the page with given width <strong>and</strong> height. These parameters as well as the drawing procedure line are taken<br />

from a module Draw. Note that this module retains the current position of the pen.<br />

DEFINITION Draw; (* ADenS33_Draw *)<br />

CONST width = 1024; height = 800;<br />

PROCEDURE Clear; (*clear drawing plane*)<br />

PROCEDURE SetPen(x, y: INTEGER);<br />

PROCEDURE line(dir, len: INTEGER);<br />

(*draw line of length len in direction dir*45 degrees; move pen accordingly*)<br />

END Draw.<br />

Procedure Hilbert draws the n Hilbert curves H 1 ... H n . It uses the four auxiliary procedures A, B, C<br />

<strong>and</strong> D recursively:<br />

VAR u: INTEGER; (* ADenS33_Hilbert *)<br />

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

BEGIN<br />

IF i > 0 THEN<br />

D(i-1); Draw.line(4, u); A(i-1); Draw.line(6, u); A(i-1); Draw.line(0, u); B(i-1)<br />

END<br />

END A;<br />

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

BEGIN<br />

IF i > 0 THEN<br />

C(i-1); Draw.line(2, u); B(i-1); Draw.line(0, u); B(i-1); Draw.line(6, u); A(i-1)<br />

END<br />

END B;<br />

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

BEGIN<br />

IF i > 0 THEN<br />

B(i-1); Draw.line(0, u); C(i-1); Draw.line(2, u); C(i-1); Draw.line(4, u); D(i-1)<br />

END<br />

END C;<br />

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

BEGIN<br />

IF i > 0 THEN<br />

A(i-1); Draw.line(6, u); D(i-1); Draw.line(4, u); D(i-1); Draw.line(2, u); C(i-1)<br />

END<br />

END D;<br />

PROCEDURE Hilbert (n: INTEGER);<br />

CONST SquareSize = 512;<br />

VAR i, x0, y0: INTEGER;<br />

BEGIN<br />

Draw.Clear;<br />

x0 := Draw.width DIV 2; y0 := Draw.height DIV 2;<br />

u := SquareSize; i := 0;<br />

REPEAT<br />

INC(i); u := u DIV 2;

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

Saved successfully!

Ooh no, something went wrong!