Skip to content

Instantly share code, notes, and snippets.

@GabrielFreeze
Created July 23, 2022 21:58
Show Gist options
  • Save GabrielFreeze/6c94f97ae43778bbee5332c1ba4431dc to your computer and use it in GitHub Desktop.
Save GabrielFreeze/6c94f97ae43778bbee5332c1ba4431dc to your computer and use it in GitHub Desktop.
Julia Set Animation. Please resize terminal window accordingly =). Only works on linux system.
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <math.h>
#include <inttypes.h>
#include <time.h>
#include <stdlib.h>
#define ITERNUM 50
#define THRESH 10
#define GRAN 0.015
/*
......: :......
..... :.........
........ #...........
.........:- ::................
......... ::..........................
........::: -: : ............................
...... * :....................................................
......: :...................:: ........................:=- ...
........:: =--::............. - + ................... .....
..........: :::::...:::...:== :.................. :.......
...........: +=# -::- -:::-# .........: :::: .......
..............::: -- -- ::......: = + +::..........
................:: -::::::::::- -::...........
................::::--= -::- - * -: ........
...............: :-::- % +-= ..........
............. ::- +-= % + *-:::::.. =-...
...............- + + ....
..................: *-.....
....................: ....
..........................:: ..
..............................:::- = =..
.................................::::--=* -: ::: ::..::.::..
..... :........................:::: -- :...::- :..........
....... = :.......: :..........: % +% *-:::::................
.......: :::::: :::......:::- -- =::................
........:: *- -::::::::- -:::::- -:.............
........: -:::::::::: % -:::::: ..........
.........:: --::::--::--- @#=---::::....:: - .........
........ =-+ +-==+ -::::............: ........
......... = + ::.......................
............: :.....................
.............: %= + :....................
................:::- -- ::::.................
..................:::- # =--:::::..................
...................::::::- + :::..................
...................* - === %-:::...............
..................... ..............
...................... # * =:::..........
.......::..............:: @ = ==+ :........
........: =:..::.......:::::-** +==--- = --- - .......
........... :::::::: - - --:::::::::--= :.........
........... -::::: ::::::::: + .........
..............: ----:-= -::::::::+ - = :.........
................::: * -= :::........:- = :..::- ......
......... .......- -- + * -- :.....................: :......
.......... :..:: --:::::..........................:......
..+ :: ::- = -::= ++ ==--::::................................
... + #- ::.............................
.. -- - ........................
.... :...................
.....: :.................
.... - - - *+ ..............
...........: @==+ --- :::: ............
..........- -- #= -::::::...............
.......:: :: =-- ::::--= * = :::.................
...........:: :::::::::-= + =::...............
......... - -:= :......:: -: --= #% --: .............
....... -::..:=..........: -::::- ::::----+ =...........
......: :..................: ::...........:::: =::..........
..... = - ...................: ::.: ..............: - -.......
....................................................:= ......
..................................................: - .....
............................: ::= =:..........
.....................:: .........
............... :.........
.......... - :......
........ :.:....
^Z
*/
int main() {
//[-2 0.5]
//[-1 1]
float y1 = -1;
float y2 = 1;
float x1 = -2;
float x2 = 2;
char pixelmap[] = {' ',' ',' ',' ',' ',
'.','.','.','.','.',
':',':',':',':',':',
'-','-','-','-','-',
'=','=','=','=','=',
'+','+','+','+','+',
'*','*','*','*','*',
'#','#','#','#','#',
'%','%','%','%','%',
'@','@','@','@','@'};
srand(time(NULL));
float ca,cb;
ca = -0.79;
cb = 0.15;
while (1) {
//Change fractal's shape by randomly changing c.
if (rand()%2) ca += 0.01;
else cb += 0.01;
for (float b = y1; b < y2; b+=GRAN*2) {
for (float a = x1; a < x2; a+=GRAN) {
//z_0
float za = a;
float zb = b;
//z_(n+1)
float wa,wb;
//Max num of iterations where the result does not tend to infinity.
int max_n = 0;
//As n -> ∞, does z_n tend to infinity?
for (int n = 0; n < ITERNUM; n++) {
//Compute z_(n+1) using the value of z_n and the current point
wa = (za*za - zb*zb) + ca;
wb = 2*za*zb + cb;
if (sqrt(wa*wa + wb*wb) > THRESH) {
//z_n is tending to infinity. C is not in Mandelbrot set.
max_n = n;
break;
}
za = wa;
zb = wb;
}
//Display point according to max_n.
putc(pixelmap[max_n],stdout);
}
putc('\n',stdout);
}
usleep(100000);
system("clear");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment