08 Jo P Aug 07

Preview:

DESCRIPTION

 

Citation preview

www.linuxforu.com | L INUX FOR YOU | AUGUST 2007

C M Y K

91

TTTTT he term ‘fractal’ was coined in 1975 by amathematician named Mandelbrot. Fractals arecreated based on some simple patterns and simple

rules. An important characteristic of fractals is that they are‘self-similar’—a small portion of a fractal, when magnified, canreproduce a larger portion of the fractal!

It is easy to write computer programs to create fractals,since fractals are produced with simple mathematical rules andare based on recursion.

In this column, we’ll create a variation of a simple, well-known fractal called the ‘Sierpinski triangle fractal’.

Writing portable C programs to create graphicalrepresentation of fractals is not possible because the standardC language does not define any functions for graphicsprogramming. It is possible to write graphics programs that usethe facilities provided by the underlying operating system;however, for now, we’ll limit ourselves to creating fractals withASCII characters for portability.

int main(){

/* the image is of 2 * MAX * MAX characters */

const int MAX = 32;

int col = 0, row = 0;

do {

if(col >= row)

printf(“ %c”, (~col & row) ? ‘.’ : ‘@’);

col++;

if (col >= MAX) {

col = 0; // reset col to start again

row++; // go to next line

printf(“\n”);

}

} while (row != MAX);

}

When you execute this program, you’ll get the followingpicture:

@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @

@ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @

@ @ . . @ @ . . @ @ . . @ @ . . @ @ . . @ @ . . @ @ . . @ @

@ . . . @ . . . @ . . . @ . . . @ . . . @ . . . @ . . . @

@ @ @ @ . . . . @ @ @ @ . . . . @ @ @ @ . . . . @ @ @ @

@ . @ . . . . . @ . @ . . . . . @ . @ . . . . . @ . @

@ @ . . . . . . @ @ . . . . . . @ @ . . . . . . @ @

@ . . . . . . . @ . . . . . . . @ . . . . . . . @

@ @ @ @ @ @ @ @ . . . . . . . . @ @ @ @ @ @ @ @

@ . @ . @ . @ . . . . . . . . . @ . @ . @ . @

@ @ . . @ @ . . . . . . . . . . @ @ . . @ @

@ . . . @ . . . . . . . . . . . @ . . . @

@ @ @ @ . . . . . . . . . . . . @ @ @ @

@ . @ . . . . . . . . . . . . . @ . @

@ @ . . . . . . . . . . . . . . @ @

@ . . . . . . . . . . . . . . . @

@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @

@ . @ . @ . @ . @ . @ . @ . @

@ @ . . @ @ . . @ @ . . @ @

@ . . . @ . . . @ . . . @

@ @ @ @ . . . . @ @ @ @

@ . @ . . . . . @ . @

@ @ . . . . . . @ @

@ . . . . . . . @

@ @ @ @ @ @ @ @

@ . @ . @ . @

@ @ . . @ @

@ . . . @

@ @ @ @

@ . @

@ @

@

For such a small program, the result is quite interesting,isn’t it? Let’s see how it works.

The two variables, row and col, are to track the rows andcolumns for printing the characters. After printing MAXcharacters for each row, we reset the col count to zero to startafresh in the next row; so we increment the row count. To get anew row in the picture, we print a newline character.

The crux of the program is the expression “(~col & row)”—ifit becomes true, we print the ‘.’ character, else we print the ‘@’character. You can use any two different looking characters foroutput (say, the ‘`’ and ‘#’ characters). To understand how theexpression “(~col & row)” works, print the values of ~col, row,

and (~col & row) for each iteration and check the results (ormentally do the calculation for given values of col and row).

Note that fractal programs typically use the formalmathematical model and/or use recursion to create the fractal.In this column, to introduce fractals, we use the power of Cbitwise operators to get the desired result. However, not allfractals can be written this way.

S.G. GANESH

Computer programs can be written for aesthetic purposes also. This column gives an introduction tothe basics of computer art using C, and is meant for students and novice programmers.

The Joy ofProgrammingWriting Beautiful Programs: ASCIIArts and Fractals—Part II

By: S.G. Ganesh By: S.G. Ganesh By: S.G. Ganesh By: S.G. Ganesh By: S.G. Ganesh is a research engineer at Siemens

(corporate technology) in Bangalore. You can reach him at

sgganesh@gmail.com