less_retarded_wiki/mandelbrot_set.md

88 lines
2.5 KiB
Markdown
Raw Normal View History

2022-05-12 20:53:59 +02:00
# Mandelbrot Set
TODO
```
___________________________________________________________
|[-2,1] . |
| .:. |
| ::::: |
| ...:::.. . |
| :..:::::::::::::.... |
| .:::::::::::::::::::' |
| :::::::::::::::::::::::: |
| :::::::::::::::::::::::::' |
| :..:::. .:::::::::::::::::::::::::: |
| .:::::::::. :::::::::::::::::::::::::: |
| .. ::::::::::: :::::::::::::::::::::::::' |
| ' ' '::':::::::::::'::::::::::::::::::::::::. |
| ::::::::::: :::::::::::::::::::::::::: |
| ':::::::' ::::::::::::::::::::::::::. |
| ' ''' :::::::::::::::::::::::::' |
| '::::::::::::::::::::::::' |
| ''::::::::::::::::::::'' |
| :::::::::::::::::::: |
| ' ''::::::::'': |
| .:::. |
| ':::' |
| : |
|___________________________________________________[0.5,-1]|
```
## Code
The following code is a simple [C](c.md) program that renders the Mandelbrot set into terminal (for demonstrative purposes, it isn't efficient or do any [antialiasing](antialiasing.md)).
```
#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;
}
```