implement write_ppm
This commit is contained in:
+36
-5
@@ -1,11 +1,17 @@
|
||||
#include "stdio.h"
|
||||
#include "math.h"
|
||||
#include "stdint.h"
|
||||
|
||||
#define MAX_DEPTH 50
|
||||
#define WIDTH 3
|
||||
#define HEIGHT 2
|
||||
|
||||
typedef struct {
|
||||
double real;
|
||||
double imag;
|
||||
/////////////////////
|
||||
// complex numbers //
|
||||
/////////////////////
|
||||
|
||||
typedef struct Complex {
|
||||
double real, imag;
|
||||
} Complex;
|
||||
|
||||
void cx_print(Complex z) {
|
||||
@@ -24,10 +30,32 @@ static inline double cx_abs(Complex z) {
|
||||
return sqrt(z.real * z.real + z.imag * z.imag);
|
||||
}
|
||||
|
||||
////////////////
|
||||
// mandelbrot //
|
||||
////////////////
|
||||
|
||||
Complex f(Complex z, Complex c) {
|
||||
return cx_add(cx_square(z), c);
|
||||
}
|
||||
|
||||
////////////////
|
||||
// ppm-writer //
|
||||
////////////////
|
||||
|
||||
typedef struct Color {
|
||||
uint8_t r, g, b;
|
||||
} Color;
|
||||
|
||||
void write_ppm(char *path, Color img[HEIGHT * WIDTH]) {
|
||||
FILE *fptr;
|
||||
fptr = fopen(path, "w");
|
||||
fprintf(fptr, "P3\n%d %d\n255\n", WIDTH, HEIGHT);
|
||||
for (int i = 0; i < WIDTH * HEIGHT; i++) {
|
||||
fprintf(fptr, "%d %d %d\n", img[i].r, img[i].g, img[i].b);
|
||||
}
|
||||
fclose(fptr);
|
||||
}
|
||||
|
||||
// return iteration count, MAX_DEPTH if diverge
|
||||
int diverge(Complex c) {
|
||||
int counter = 0;
|
||||
@@ -38,7 +66,10 @@ int diverge(Complex c) {
|
||||
}
|
||||
|
||||
int main() {
|
||||
Complex z = {2.0, 4.0};
|
||||
cx_print(z);
|
||||
Color img[WIDTH * HEIGHT] = {
|
||||
(Color){255, 0, 0}, (Color){0, 255, 0}, (Color){0, 0, 255},
|
||||
(Color){255, 0, 0}, (Color){0, 255, 0}, (Color){0, 0, 255}
|
||||
};
|
||||
write_ppm("test.ppm", img);
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user