You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

55 lines
1.2 KiB
C++

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include "complex.h"
using namespace std;
struct Color {
unsigned char r, g, b;
};
Complex func(Complex z, Complex c) {
return z * z + c;
}
int main(int argc, char** argv) {
int n = 20;
Complex z = {0, 0};
int width = 800, height = 800;
float x0 = -2.0f, x1 = 2.0f;
float y0 = -2.0f, y1 = 2.0f;
Color* data = (Color*)malloc(sizeof(Color) * width * height);
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
z = {0, 0};
Complex c = {x0 + (x1-x0) / width * i, y0 + (y1-y0) / width * j};
for (int k = 0; k < n; ++k) {
z = func(z, c);
}
float r = abs(z.re);
float b = abs(z.im);
if (r > 255) {
r = 255;
}
if (b > 255) {
b = 255;
}
data[i + width * j].r = 255 - r;
data[i + width * j].g = 0;
data[i + width * j].b = 255 - b;
}
}
FILE* file = fopen("mandelbrot.ppm", "wb");
fprintf(file, "P6\n%d %d\n255\n", width, height);
fwrite(data, sizeof(Color), height * width, file);
fclose(file);
free(data);
}