1
www.linuxforu.com | LINUX FOR YOU | AUGUST 2007 C M Y K 91 T T T he term ‘fractal’ was coined in 1975 by a mathematician named Mandelbrot. Fractals are created 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, can reproduce a larger portion of the fractal! It is easy to write computer programs to create fractals, since fractals are produced with simple mathematical rules and are 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 graphical representation of fractals is not possible because the standard C language does not define any functions for graphics programming. It is possible to write graphics programs that use the facilities provided by the underlying operating system; however, for now, we’ll limit ourselves to creating fractals with ASCII 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 following picture: @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ @ @ . . @ @ . . @ @ . . @ @ . . @ @ . . @ @ . . @ @ . . @ @ @ . . . @ . . . @ . . . @ . . . @ . . . @ . . . @ . . . @ @ @ @ @ . . . . @ @ @ @ . . . . @ @ @ @ . . . . @ @ @ @ @ . @ . . . . . @ . @ . . . . . @ . @ . . . . . @ . @ @ @ . . . . . . @ @ . . . . . . @ @ . . . . . . @ @ @ . . . . . . . @ . . . . . . . @ . . . . . . . @ @ @ @ @ @ @ @ @ . . . . . . . . @ @ @ @ @ @ @ @ @ . @ . @ . @ . . . . . . . . . @ . @ . @ . @ @ @ . . @ @ . . . . . . . . . . @ @ . . @ @ @ . . . @ . . . . . . . . . . . @ . . . @ @ @ @ @ . . . . . . . . . . . . @ @ @ @ @ . @ . . . . . . . . . . . . . @ . @ @ @ . . . . . . . . . . . . . . @ @ @ . . . . . . . . . . . . . . . @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ . @ . @ . @ . @ . @ . @ . @ @ @ . . @ @ . . @ @ . . @ @ @ . . . @ . . . @ . . . @ @ @ @ @ . . . . @ @ @ @ @ . @ . . . . . @ . @ @ @ . . . . . . @ @ @ . . . . . . . @ @ @ @ @ @ @ @ @ @ . @ . @ . @ @ @ . . @ @ @ . . . @ @ @ @ @ @ . @ @ @ @ 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 and columns for printing the characters. After printing MAX characters for each row, we reset the col count to zero to start afresh in the next row; so we increment the row count. To get a new row in the picture, we print a newline character. The crux of the program is the expression (~col & row)”—if it becomes true, we print the ‘.’ character, else we print the ‘@’ character. You can use any two different looking characters for output (say, the ‘`’ and ‘#’ characters). To understand how the expression “(~col & row)” works, print the values of ~col, row, and (~col & row) for each iteration and check the results (or mentally do the calculation for given values of col and row). Note that fractal programs typically use the formal mathematical model and/or use recursion to create the fractal. In this column, to introduce fractals, we use the power of C bitwise operators to get the desired result. However, not all fractals can be written this way. S.G. GANESH Computer programs can be written for aesthetic purposes also. This column gives an introduction to the basics of computer art using C, and is meant for students and novice programmers. The Joy of Programming Writing Beautiful Programs: ASCII Arts 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 [email protected]

08 Jo P Aug 07

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: 08 Jo P Aug 07

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

[email protected]