Tweetable mathematical art: Julia sets
3/Aug 2014
This post is my first entry to this challenge on Stackexchange’s PPCG: Tweetable mathematical art.
Julia sets
If there’s a Mandelbrot, there should be a Julia set too.
You can spend hours tweaking the parameters and functions, so this is just a quick one that looks decent.
Inspired from Martin’s participation.
unsigned short red_fn(int i, int j){
#define D(x) (x-DIM/2.)/(DIM/2.)
float x=D(i),y=D(j),X,Y,n=0;while(n++<200&&(X=x*x)+(Y=y*y)<4){x=X-Y+.36237;y=2*x*y+.32;}return log(n)*256;}
unsigned short green_fn(int i, int j){
float x=D(i),y=D(j),X,Y,n=0;while(n++<200&&(x*x+y*y)<4){X=x;Y=y;x=X*X-Y*Y+-.7;y=2*X*Y+.27015;}return log(n)*128;}
unsigned short blue_fn(int i, int j){
float x=D(i),y=D(j),X,Y,n=0;while(n++<600&&(x*x+y*y)<4){X=x;Y=y;x=X*X-Y*Y+.36237;y=2*X*Y+.32;}return log(n)*128;}
Would you like some RNG?
OK, Sparr’s comment put me on the track to randomize the parameters of these little Julias. I first tried to do bit-level hacking with the result of time(0)
but C++ doesn’t allow hexadecimal floating point litterals so this was a dead-end (with my limited knowledge at least). I could have used some heavy casting to achieve it, but that wouldn’t have fit into the 140 bytes.
I didn’t have much room left anyway, so I had to drop the red Julia to put my macros and have a more conventional RNG (time
d seed and real rand()
, woohoo!).
Whoops, something is missing. Obviously, these parameters have to be static or else you have some weird results (but funny, maybe I’ll investigate a bit later if I find something interesting).
So here we are, with only green and blue channels:
Now let’s add a simple red pattern to fill the void. Not really imaginative, but I’m not a graphic programer … yet :-)
And finally the new code with random parameters:
unsigned short red_fn(int i, int j){
static int n=1;if(n){--n;srand(time(0));}
#define R rand()/16384.-1
#define S static float r=R,k=R;float
return _cb(i^j);}
unsigned short green_fn(int i, int j){
#define D(x) (x-DIM/2.)/(DIM/2.),
S x=D(i)y=D(j)X,Y;int n=0;while(n++<200&&(X=x)*x+(Y=y)*y<4){x=X*X-Y*Y+r;y=2*X*Y+k;}return log(n)*512;}
unsigned short blue_fn(int i, int j){
S x=D(i)y=D(j)X,Y;int n=0;while(n++<200&&(X=x)*x+(Y=y)*y<4){x=X*X-Y*Y+r;y=2*X*Y+k;}return log(n)*512;}
There’s still room left now …