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.

73 lines
1.5 KiB
C

#include <math.h>
#include <stdio.h>
#define MAX_ITER 80
#define ANCHO 640
#define ALTO 480
#define FOV 90
#define PI 3.14159265358979323846
typedef struct {
double r, g, b;
} color_t;
#define LIM(c) (int)(((c) < 1 ? (c) : 1) * 255)
void color_imprimir(color_t c) {
printf("%d %d %d", LIM(c.r), LIM(c.g), LIM(c.b));
}
color_t computar_intensidad(color_t ambiente, double x0, double y0) {
x0 /= ANCHO / 4;
y0 /= ALTO / 4;
double x = 0;
double y = 0;
int n = 0;
while(x*x + y*y <= 4 && n < MAX_ITER) {
double xtemp = x*x - y*y + x0;
y = 2*x*y + y0;
x = xtemp;
n++;
}
if(n == MAX_ITER)
return ambiente;
//float h = (n + 1 - log(log2(sqrt(x*x+y*y)))) / MAX_ITER * 360;
float h = n * 360.0 / MAX_ITER;
float xx = (1 - fabs(fmodf(h / 60.0, 2) - 1));
float r = 0, g = 0, b = 0;
if(h < 60)
r = 1, g = xx;
else if(h < 120)
g = 1, r = xx;
else if(h < 180)
g = 1, b = xx;
else if(h < 240)
b = 1, g = xx;
else if(h < 300)
b = 1, r = xx;
else
r = 1, b = xx;
return (color_t){r, g, b};
}
int main(void) {
color_t ambiente = {.05, .05, .05};
printf("P3\n%d %d\n255\n", ANCHO, ALTO);
float vz = ANCHO / 2 / tan(FOV/ 2 * PI / 180);
for(int vy = ALTO / 2; vy > - ALTO / 2; vy--)
for(int vx = - ANCHO / 2; vx < ANCHO / 2; vx++) {
color_imprimir(computar_intensidad(ambiente, vx, vy));
putchar('\n');
}
return 0;
}