The first program below generates and displays the Mandelbrot Set. It also stores the image data in a file called mandel.dat. It could have been made to run faster. However, it's only for amusement!
//TO GENERATE THE MANDELBROT SET
#include < stdio.h>
#include < conio.h>
#include < graph.h>
#define XBIAS 395
#define YBIAS 240
main() {
int c, e = 0, i;
float cx, cy, x, y, z;
FILE *h;
_setvideomode(_VRES16COLOR);
h = fopen("mandel.dat","w");
for(cy = -2; cy < 2 && e == 0; cy += .01) {
for(cx = -2.5; cx < 1.5 && e == 0; cx += .01) {
for(x = .01, y = .01, c = 0, i = 0; i < 128; i++) {
if(kbhit()) {e = 1; break;}
z = x * y;
x = x * x - y * y + cx;
y = z + z + cy;
if(x < -2.5 || x > 1.5 || y < -2 || y > 2) {
if(i < 2) c = 1; // dark blue
else if(i < 3) c = 2; // dark green
else if(i < 4) c = 3; // dark cyan
else if(i < 5) c = 4; // dark red
else if(i < 6) c = 5; // dark magenta
else if(i < 7) c = 6; // dark yellow
else if(i < 8) c = 7; // dark yellow
else if(i < 9) c = 9; // light blue
else if(i < 10) c = 10; // light green
else if(i < 11) c = 11; // light cyan
else if(i < 12) c = 12; // light red
else if(i < 13) c = 13; // light yellow
else if(i < 14) c = 14; // light yellow
else c = 15; // white
_setcolor(c);
_setpixel(
XBIAS + (int)(cx * 100), YBIAS - (int)(cy * 100)
);
break; // break the i-loop
}
}
if(h != NULL) putc(c, h); // save pixel colour
}
}
fclose(h);
_settextposition(1, 1); printf("THE MANDELBROT SET");
c = getchar();
_setvideomode(_DEFAULTMODE);
}
To download a compiled version to run on a PC, right-click on mandel1.exe and save it to your local disk.
The second program here simply re-displays the Mandelbrot Set from the data previously stored in mandel.dat by the first program.
//TO RE-DISPLAY THE MANDELBROT SET [Compiled as Mandel2.exe]
#include < stdio.h>
#include < conio.h>
#include < graph.h>
#define XBIAS 370
#define YBIAS 240
main() {
int c, e = 0, x, y;
FILE *h;
_setvideomode(_VRES16COLOR);
_settextposition( 1,32); printf("THE MANDELBROT SET");
_settextposition(30,31); printf("Hit C/R key to quit.");
_setcolor(7);
yscale( -260 ); yscale( 160 );
xscale( -210 ); xscale( 210 );
if(h = fopen("mandel.dat","r")) {
for(y = -200; y < 201 && e == 0; y++)
for(x = -250; x < 151; x++) {
if(kbhit()) {e = 1; break;}
_setcolor( getc(h) );
_setpixel(XBIAS + x, YBIAS - y);
}
fclose(h);
}
else printf("Could not open data file.");
c = getchar();
_setvideomode(_DEFAULTMODE);
}
yscale(int a) { //a = -260 or +160
int y;
_moveto (XBIAS + a, YBIAS - 200);
_lineto (XBIAS + a, YBIAS + 200);
for(y = -200; y < 201; y += 50) {
_moveto (XBIAS + a - 5, YBIAS + y);
_lineto (XBIAS + a + 5, YBIAS + y);
}
}
xscale(int b) { //b = -210 or +210
int x;
_moveto (XBIAS - 250, YBIAS + b);
_lineto (XBIAS + 150, YBIAS + b);
for(x = -250; x < 151; x += 50) {
_moveto (XBIAS + x, YBIAS + b - 5);
_lineto (XBIAS + x, YBIAS + b + 5);
}
}
The compiled version of this program is mandel.exe, which you can download by right-clicking on its name and saving it to your local disk.
1. Square a Complex Number
(x + iy)²
= (x + iy)(x + iy)
= x² + i² y² + i2xy
= x² - y² + i2xy
SquareComplex(struct complex c) {
double x = c -> r, y = c -> i, z;
c -> r = x * x - y * y; //Real part of square: x² - y²
z = r * i; c -> i = z + z; //Imaginary part: 2xy
}
2. Display a complex quantity as a pixel on the complex plane
#define REALSCALE 128 //Screen pixel scaling factor
#define IMAGINERYSCALE 128 //Screen pixel scaling factor
DisplayComplex(struct complex c) {
static int initialised = NO;
if(initialised == NO) {
_setvideomode(_VRES16COLOR); //Set to VGA 640 by 480 pixels
_setlogorg(320,240); //Set origin to screen centre
_setcolor(15); //Set pixel colour bright white
initialised = YES; //Set the initialisation flag
}
_setpixel( //display the pixel
(int)((c -> r) *= REALSCALE),
(int)((c -> i) *= IMAGINERYSCALE)
);
}
/* I, Robert J Morton, author of these programs, am a poor
but right honourable fellow of the Ancient and Noble
Order of the Long-Term Unemployed.
Please send any gratefully accepted offers of
programming work to robmorton@clara.net. */