2.6 KiB
2.6 KiB
Mandelbrot Set
TODO
{ Pretty amazing ASCII rendering of the Mandelbrot set can be found at http://www.mrob.com/pub/muency/asciigraphics.html. ~drummyfish }
___________________________________________________________
|[-2,1] . |
| .:. |
| ::::: |
| ...:::.. . |
| :..:::::::::::::.... |
| .:::::::::::::::::::' |
| :::::::::::::::::::::::: |
| :::::::::::::::::::::::::' |
| :..:::. .:::::::::::::::::::::::::: |
| .:::::::::. :::::::::::::::::::::::::: |
| .. ::::::::::: :::::::::::::::::::::::::' |
| ' ' '::':::::::::::'::::::::::::::::::::::::. |
| ::::::::::: :::::::::::::::::::::::::: |
| ':::::::' ::::::::::::::::::::::::::. |
| ' ''' :::::::::::::::::::::::::' |
| '::::::::::::::::::::::::' |
| ''::::::::::::::::::::'' |
| :::::::::::::::::::: |
| ' ''::::::::'': |
| .:::. |
| ':::' |
| : |
|___________________________________________________[0.5,-1]|
Code
The following code is a simple C program that renders the Mandelbrot set into terminal (for demonstrative purposes, it isn't efficient or do any antialiasing).
#include <stdio.h>
#define ROWS 30
#define COLS 60
#define FROM_X -2.0
#define FROM_Y 1.0
#define STEP (2.5 / ((double) COLS))
unsigned int mandelbrot(double x, double y)
{
double cx = x, cy = y, tmp;
for (int i = 0; i < 1000; ++i)
{
tmp = cx * cx - cy * cy + x;
cy = 2 * cx * cy + y;
cx = tmp;
if (cx * cx * cy * cy > 1000000000)
return 0;
}
return 1;
}
int main(void)
{
double cx, cy = FROM_Y;
for (int y = 0; y < ROWS; ++y)
{
cx = FROM_X;
for (int x = 0; x < COLS; ++x)
{
unsigned int point =
mandelbrot(cx,cy) + (mandelbrot(cx,cy + STEP) * 2);
putchar(point == 3 ? ':' : (point == 2 ? '\'' :
(point == 1 ? '.' : ' ')));
cx += STEP;
}
putchar('\n');
cy -= 2 * STEP;
}
return 0;
}