23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

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.

Each multiple of 1 <strong>in</strong>ch also has a numeric label. The longest tick length is called<br />

the major tick length. We will not worry about actual distances, however, <strong>and</strong> just<br />

pr<strong>in</strong>t one tick per l<strong>in</strong>e.<br />

A Recursive Approach to Ruler Draw<strong>in</strong>g<br />

Our approach to draw<strong>in</strong>g such a ruler consists of three functions. The ma<strong>in</strong> function<br />

drawRuler() draws the entire ruler. Its arguments are the total number of <strong>in</strong>ches<br />

<strong>in</strong> the ruler, nInches, <strong>and</strong> the major tick length, majorLength. The utility<br />

function drawOneTick() draws a s<strong>in</strong>gle tick of the given length. It can also be<br />

given an optional <strong>in</strong>teger label, which is pr<strong>in</strong>ted if it is nonnegative.<br />

The <strong>in</strong>terest<strong>in</strong>g work is done by the recursive function drawTicks(), which<br />

draws the sequence of ticks with<strong>in</strong> some <strong>in</strong>terval. Its only argument is the tick<br />

length associated with the <strong>in</strong>terval's central tick. Consider the 1-<strong>in</strong>ch ruler with<br />

major tick length 5 shown <strong>in</strong> Figure 3.22(b). Ignor<strong>in</strong>g the l<strong>in</strong>es conta<strong>in</strong><strong>in</strong>g 0 <strong>and</strong> 1,<br />

let us consider how to draw the sequence of ticks ly<strong>in</strong>g between these l<strong>in</strong>es. The<br />

central tick (at 1/2 <strong>in</strong>ch) has length 4. Observe that the two patterns of ticks above<br />

<strong>and</strong> below this central tick are identical, <strong>and</strong> each has a central tick of length 3. In<br />

general, an <strong>in</strong>terval with a central tick length L ≥ 1 is composed of the follow<strong>in</strong>g:<br />

• An <strong>in</strong>terval with a central tick length L − 1<br />

191

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

Saved successfully!

Ooh no, something went wrong!