65 lines
1.5 KiB
C++
65 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 argc, char** argv) {
|
||
|
Complex c = {0, 0};
|
||
|
int n = 20;
|
||
|
int width = 800, height = 800;
|
||
|
float x0 = -2.0f, x1 = 2.0f;
|
||
|
float y0 = -2.0f, y1 = 2.0f;
|
||
|
|
||
|
if (argc < 4) {
|
||
|
cout << "usage: julia [re] [im] [n]" << endl
|
||
|
<< "using default values: 0 + 0i, n = 20 "<< endl;
|
||
|
}
|
||
|
else {
|
||
|
cout << argv[1] << " + " << argv[2] << "i" << endl << "n = " << n << endl;
|
||
|
sscanf(argv[1], "%f", &c.re);
|
||
|
sscanf(argv[2], "%f", &c.im);
|
||
|
sscanf(argv[3], "%d", &n);
|
||
|
}
|
||
|
|
||
|
Color* data = (Color*)malloc(sizeof(Color) * width * height);
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
FILE* file = fopen("julia.ppm", "wb");
|
||
|
fprintf(file, "P6\n%d %d\n255\n", width, height);
|
||
|
fwrite(data, sizeof(Color), height * width, file);
|
||
|
fclose(file);
|
||
|
|
||
|
free(data);
|
||
|
}
|