UiPath Platform: The Backend Engine Powering Your Automation - Session 1
09 Jo P Sep 07
1. The Joy of
Programming
Writing Beautiful Programs: S.G. GANESH
ASCII Arts and Fractals—Part III
In this column, we’ll look at how to draw the Mandelbrot set fractal using ASCII characters.
I
n most of the cases, writing fractal programs requires a
z.r = temp.r;
good knowledge of mathematics. The Mandelbrot set is z.i = temp.i;
based on the theory and use of complex numbers. The
Mandelbrot set is a collection of complex numbers, which // if square of z.r and z.i is more than 4,
// then the point is outside the image
have two parts: real and imaginary. Usually, a complex if((z.r * z.r + z.i * z.i) > 4) {
number is represented in the form, 'a + bi', where 'a' is the outside = 1; break;
real part and 'b' is the imaginary part (the suffix 'i' }
}
indicates that). A complex plane has real numbers in the x- // if the point is inside image, print ‘*’
axis and imaginary numbers in the y-axis. printf(“%c”, outside ? ‘ ‘ : ‘*’);
Given the complex numbers Z and C, the Mandelbrot }
printf(“n”); // move to next row
set is formed by the formula: Z = Z ** 2 + C. Here ** stands }
for square. C is the number we test to see if the number is }
inside or outside the set. We start Z from zero and keep
reassigning it as we change C, using this formula. When you run this program, you’ll get this output:
Given a complex number a + bi, the formula for finding
the square of a complex number is: a**2 – b**2 + 2abi. So, *
the real part is a**2 – b**2 and the imaginary part is 2*a*b.
*
With this background, here is the program to print the ****
****
Mandelbrot set. The comments in the program explain the ****
** *
programming steps required to find if a given number Z is * **********
******************
inside or outside the set. *****************
* *****************
*******************
struct complex { double r, i; } origin, z, c; ***********************
*********************
* *** **********************
int main() { ******* **********************
********* **********************
// the incr value for c ********* **********************
double incr = 0.05; ** ********* *********************
************************************
// image dimension is size * size characters ** ********* *********************
const int size = 60; ********* **********************
********* **********************
origin.r = -1.5; origin.i = -1.5; ******* **********************
* *** **********************
*********************
for(int rows = 0; rows < size; rows++) { ***********************
// for each rwo, increase the value of c.i *******************
* *****************
c.i = origin.i + rows * incr; *****************
for(int cols = 0; cols < size; cols++) { ******************
* **********
// for each row, increase the value for c.r ** *
c.r = origin.r + cols * incr; ****
****
****
// initialize z to 0 and start again *
z.r = 0; z.i = 0;
// to check if the point is “outside” *
int outside = 0;
Quite interesting, isn’t it!
// loop to check and set “outside”
for(int i = 0; i < 100; i++) {
// calc z**2 + c
// z**2 = z.r**2 - z.i**2 + 2*z.r*z.i By: S.G. Ganesh is a research engineer in Siemens
complex temp; (Corporate Technology). He has authored a book “Deep C”
temp.r = z.r * z.r - z.i * z.i + c.r; (ISBN 81-7656-501-6). You can reach him at
temp.i = 2 * z.r * z.i + c.i; sgganesh@gmail.com.
88 AUGUST 2007 | LINUX FOR YOU | www.linuxforu.com
CMYK