Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
08 Jo P Aug 07
1. The Joy of
Programming
Writing Beautiful Programs: ASCII
Arts and Fractals—Part II 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.
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) For such a small program, the result is quite interesting,
printf(“ %c”, (~col & row) ? ‘.’ : ‘@’); isn’t it? Let’s see how it works.
col++; The two variables, row and col, are to track the rows and
if (col >= MAX) { columns for printing the characters. After printing MAX
col = 0; // reset col to start again characters for each row, we reset the col count to zero to start
row++; // go to next line afresh in the next row; so we increment the row count. To get a
printf(“n”); new row in the picture, we print a newline character.
} The crux of the program is the expression “(~col & row)”—if
} while (row != MAX); 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
When you execute this program, you’ll get the following expression “(~col & row)” works, print the values of ~col, row,
picture: 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.
@ . @ . . . . . @ . @ . . . . . @ . @ . . . . . @ . @
@ @ . . . . . . @ @ . . . . . . @ @ . . . . . . @ @ By: S.G. Ganesh is a research engineer at Siemens
@ . . . . . . . @ . . . . . . . @ . . . . . . . @ (corporate technology) in Bangalore. You can reach him at
@ @ @ @ @ @ @ @ . . . . . . . . @ @ @ @ @ @ @ @ sgganesh@gmail.com
www.linuxforu.com | LINUX FOR YOU | AUGUST 2007 91
CMYK