Skip to content

Instantly share code, notes, and snippets.

@Gro-Tsen
Created January 29, 2020 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gro-Tsen/2f2a178213563a4632b775c2d67a07ed to your computer and use it in GitHub Desktop.
Save Gro-Tsen/2f2a178213563a4632b775c2d67a07ed to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
#include <complex.h>
#include <assert.h>
#ifndef WIDTH
#define WIDTH 800
#endif
#ifndef HEIGHT
#define HEIGHT 800
#endif
#ifndef MAXITER
#define MAXITER 1024
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
int
main (void)
{
printf ("P3\n%d %d\n255\n", WIDTH, HEIGHT);
for ( int i=0 ; i<HEIGHT ; i++ )
{
for ( int j=0 ; j<WIDTH ; j++ )
{
#if 1
double complex c
= (((double)j)/(WIDTH-1) * 2 - 1) * 1.3 - 0.75
- I * (((double)i)/(HEIGHT-1) * 2 - 1) * 1.3;
#else
double complex c
= (((double)j)/(WIDTH-1) * 2 - 1) * 0.1 + 0.36
- I * (((double)i)/(HEIGHT-1) * 2 - 1) * 0.1 + I * 0.60;
#endif
double complex z = c;
double complex argtab[MAXITER];
int iter;
for ( iter=0 ; iter<MAXITER ; iter++ )
{
argtab[iter] = carg(z) / (M_PI*2);
if ( cabs(z) >= 1.e6 )
break;
z = z*z + c;
}
if ( iter < MAXITER )
{
double arg = carg(z) / (M_PI*2);
for ( int k=0 ; k<iter ; k++ )
{
arg /= 2;
double altarg = arg + (signbit(arg) ? 0.5 : -0.5);
// We have two determinations of the argument: arg
// and altarg: pick the one closest to the argument
// of the corresponding past iterate.
double checkarg = argtab[iter-k-1];
if ( ( altarg > checkarg-0.25 && altarg < checkarg+0.25 )
|| ( altarg > checkarg-1.25 && altarg < checkarg-0.75 )
|| ( altarg > checkarg+0.75 && altarg < checkarg+1.25 ) )
arg = altarg;
}
// The rest is just color gradient...
unsigned char colr, colg, colb;
double p = arg*6 + 3;
while ( p >= 6 )
p -= 6;
while ( p < 0 )
p += 6;
if ( p < 1 )
{
colr = 0;
colg = 255*(1-p);
colb = 255;
}
else if ( p < 2 )
{
colr = 255*(p-1);
colg = 0;
colb = 255;
}
else if ( p < 3 )
{
colr = 255;
colg = 0;
colb = 255*(3-p);
}
else if ( p < 4 )
{
colr = 255;
colg = 255*(p-3);
colb = 0;
}
else if ( p < 5 )
{
colr = 255*(5-p);
colg = 255;
colb = 0;
}
else
{
colr = 0;
colg = 255;
colb = 255*(p-5);
}
printf ("\t%d %d %d", colr, colg, colb);
}
else
printf ("\t0 0 0");
}
printf ("\n");
}
}
@adammaj1
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment