06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Mathematics 217<br />

In 1975, Benoit Mandelbrot coined the term fractal <strong>to</strong> describe self-similar shapes found in nature.<br />

Much of the stuff we encounter in our physical world can be described by idealized geometrical forms—a<br />

postcard has a rectangular shape, a ping-pong ball is spherical, and so on. However, many naturally<br />

occurring structures cannot be described by such simple means. Some examples are snowfl akes, trees,<br />

coastlines, and mountains. Fractals provide a geometry for describing and simulating these types of<br />

self-similar shapes (by “ self-similar ” we mean no matter how “ zoomed out ” or “ zoomed in, ” the shape<br />

ultimately appears the same). One process for generating these shapes is known as recursion.<br />

We know that a function can call another function. We do this whenever we call any function inside<br />

of the draw( ) function. But can a function call itself? Can draw( ) call draw( ) ? In fact, it can (although<br />

calling draw( ) from within draw( ) is a terrible example, since it would result in an infi nite loop).<br />

Functions that call themselves are recursive and are appropriate for solving diff erent types of problems.<br />

Th is occurs in mathematical calculations; the most common example of this is “ fac<strong>to</strong>rial. ”<br />

Th e fac<strong>to</strong>rial of any number n , usually written as n !, is defi ned as:<br />

n ! � n * n – 1 * . . . . * 3 * 2 * 1<br />

0! � 1<br />

We could write a function <strong>to</strong> calculate fac<strong>to</strong>rial using a for loop in <strong>Processing</strong> :<br />

int fac<strong>to</strong>rial(int n) {<br />

int f = 1;<br />

for (int i = 0; i < n; i + + ) {<br />

f = f * (i + 1);<br />

}<br />

return f;<br />

}<br />

If you look closely at how fac<strong>to</strong>rial works, however, you will notice something interesting. Let’s examine<br />

4! and 3!<br />

4! � 4 * 3 * 2 * 1<br />

3! � 3 * 2 * 1<br />

therefore. . . 4! � 4 * 3!<br />

We can describe this in more general terms. For any positive integer n :<br />

n ! � n * ( n – 1)!<br />

1! � 1<br />

Written in English:<br />

Th e fac<strong>to</strong>rial of N is defi ned as N times the fac<strong>to</strong>rial of N – 1.

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

Saved successfully!

Ooh no, something went wrong!