Skip to content

Instantly share code, notes, and snippets.

@Gro-Tsen
Created January 30, 2020 15:44
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/7287d2466da5f93941296cce451061ed to your computer and use it in GitHub Desktop.
Save Gro-Tsen/7287d2466da5f93941296cce451061ed to your computer and use it in GitHub Desktop.
// Draw a graph of argument or modulus of p_n(z)
// where p_0(z) = z and p_{i+1}(z) = p_i(z)^2 + z
// Written by David A. Madore, 2020-01-29. Public Domain.
#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 NBITER
#define NBITER 8
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef SHOW_ABS
#define SHOW_ABS 0 // Set to 1 to produce a plot of modulus rather than arg
#endif
#ifndef DIVIDE
#define DIVIDE 0 // Set to 1 to divide p_n(z) by z^(2^n) on output
#endif
int
main (void)
{
#if SHOW_ABS
printf ("P2\n%d %d\n255\n", WIDTH, HEIGHT);
#else
printf ("P3\n%d %d\n255\n", WIDTH, HEIGHT);
#endif
for ( int i=0 ; i<HEIGHT ; i++ )
{
for ( int j=0 ; j<WIDTH ; j++ )
{
double complex c
= (((double)j)/(WIDTH-1) * 2 - 1) * 1.3 - 0.75
- I * (((double)i)/(HEIGHT-1) * 2 - 1) * 1.3;
double complex z = c;
#if DIVIDE
double complex u = c;
#endif
int iter;
for ( iter=0 ; iter<NBITER ; iter++ )
{
z = z*z + c;
#if DIVIDE
u = u*u;
#endif
}
if ( 1 )
{
#if DIVIDE
z /= u;
#endif
#if SHOW_ABS
double abs = log(cabs(z)) / (M_PI*2);
unsigned char gray;
double p = abs - floor(abs);
if ( p < 0 || p != p )
p = 0;
else if ( p > 1 )
p = 1;
if ( p < 0.5 )
gray = 255 * (2*p);
else
gray = 255 * (2-2*p);
printf (" %d", gray);
#else
double arg = carg(z) / (M_PI*2);
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);
#endif
}
}
printf ("\n");
}
}
@Gro-Tsen
Copy link
Author

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