60 lines
1.5 KiB
C++
60 lines
1.5 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 width = 800, height = 800;
|
|
float x0 = -2.0f, x1 = 2.0f;
|
|
float y0 = -2.0f, y1 = 2.0f;
|
|
int n = 20;
|
|
|
|
Color* data = (Color*)malloc(sizeof(Color) * width * height);
|
|
|
|
int max_time_steps = 500;
|
|
for (int time = 0; time < max_time_steps; time++)
|
|
{
|
|
Complex c = {-1.5 + (1.5 / max_time_steps) * time, -1.5 + (1.5 / max_time_steps) * time + 1};
|
|
for (int j = 0; j < height; j++)
|
|
{
|
|
for (int i = 0; i < width; i++)
|
|
{
|
|
Complex z = {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;
|
|
}
|
|
}
|
|
|
|
char filename[100];
|
|
sprintf(filename, "animation/complex_%03d.ppm", time);
|
|
|
|
FILE* file = fopen(filename, "wb");
|
|
fprintf(file, "P6\n%d %d\n255\n", width, height);
|
|
fwrite(data, sizeof(Color), height * width, file);
|
|
fclose(file);
|
|
}
|
|
free(data);
|
|
}
|