Lair Of The Multimedia Guru

2006-02-10

ASCII Mandelbrot Realtime Zoom

not only that, but its 447 bytes of C source (with license header, copyright statement and indented code), and you can even specify the spot to which the realtime zoom will go on the command line
Update: new version with more nasty tricks and less whitespace only needs 285 bytes of C source

Filed under: Off Topic — Michael @ 03:14

2 Comments »

  1. I recently explained the source code to someone, I hope you don’t mind me posting it here in case someone stumbles across this code and wants to understand it..

    ————–

    > Can you please decipher the code for me?
    > What do we see on the screen? Is it supposed to be a mandelbrot set?
    > It seems that odd lines and even lines have different roles.

    It is pretty straightforward: The entire code is 4 nested loops –
    1. an entire picture, at different zoom
    2. a single line of a picture
    3. a single character/pixel inside a line
    4. the calculation of the color of a single pixel

    > #define A c>2?atof(*++v):
    > main(int c,int**v){float
    > atof(),x,y,p=1,r,i,t,X=A.105,Y=A.928;
    so far, initialization and reading params – X and Y is the zoom-in
    location, and they have a default of “0.105” and “0.928”. what “A”
    does is attempt to read from command line, otherwise uses the default
    written after it

    > for(;p>.003;p*=.98,printf(“33[35A”))
    This is the biggest loop, running once for each zoom in level.
    Starting at zoom “p=1”, and exponentially (at 0.98) zooming in until
    “0.003”.
    The “printf” clears the screen for the next zoom in at every iteration

    > for(y=-1.2;y for(x=-1.6;x printf(“33[4%dm%c33[0m”,c&7,x>1?’\n’:32))
    The actual printing of a single pixel, done once for every ‘x’ in the
    loop above. The color is decided by variable “c”, explained below. For
    all “x for(r=i=0,c=6;c<256&r*r+i*i2, then the color would be a certain
    color, and if it took 133 iterations, it would be a different colors.
    if c=256, then the color is black, the position is in the Mandlebrot
    set.

    > t=r*r-i*i-X+(x+X)*p,i=r*i*2+Y+(y-Y)*p,r=t;}
    The actual calculation of “Zn+1=Zn^2+c”. “c” here is calculated by
    “p”, “x”, “y”, “X” and “Y” – zoom level, current pixel, and target
    location.

    I didn’t notice any difference between odd and even lines… If you
    meant “c&7”, then that is the color calculation – there are 7
    different available colors, and “c&7” means taking the lowest 3 bits
    of “c” for deciding the color (i.e. when c=130, it gives the same
    color as c=138, c=146, c=2, …)

    Oded

    Comment by Oded — 2009-05-12 @ 20:12

  2. ouch, the html <> mangled it :(

    I’ll post again, feel free to delete the previous message

    ——————-

    > Can you please decipher the code for me?
    > What do we see on the screen? Is it supposed to be a mandelbrot set?
    > It seems that odd lines and even lines have different roles.

    It is pretty straightforward: The entire code is 4 nested loops –
    1. an entire picture, at different zoom
    2. a single line of a picture
    3. a single character/pixel inside a line
    4. the calculation of the color of a single pixel

    > #define A c>2?atof(*++v):
    > main(int c,int**v){float
    > atof(),x,y,p=1,r,i,t,X=A.105,Y=A.928;
    so far, initialization and reading params – X and Y is the zoom-in
    location, and they have a default of “0.105” and “0.928”. what “A”
    does is attempt to read from command line, otherwise uses the default
    written after it

    > for(;p>.003;p*=.98,printf(“33[35A”))
    This is the biggest loop, running once for each zoom in level.
    Starting at zoom “p=1”, and exponentially (at 0.98) zooming in until
    “0.003”.
    The “printf” clears the screen for the next zoom in at every iteration

    > for(y=-1.2;y<1.2;y+=.07)
    The y axis, this loops runs once for every line shown, a total of
    2.4/0.07= 34 lines high

    > for(x=-1.6;x<1;x+=.03,
    The x axis, this runs once for every *pixel* – single character. total
    width 2.6/0.03=86 characters wide

    > printf(“33[4%dm%c33[0m”,c&7,x>1?’\n’:32))
    The actual printing of a single pixel, done once for every ‘x’ in the
    loop above. The color is decided by variable “c”, explained below. For
    all “x<=1” a space is printed (character 32), otherwise, a newline –
    meaning, we reached the end of the line, and the “x” loop breaks and
    starts all over again for the next line

    > for(r=i=0,c=6;c<256&r*r+i*i<4;c++)
    Calculation loop – runs at most 256 times for each pixel. Stops when
    the absolute value of the Zn is bigger than 2, or after 256 iterations
    and the absolute value is still small.
    The color is decided by how many times this loop ran until the
    absolute value got too high, counted by “c”. If “c” was 132, meaning,
    it took 132 iterations until |Zn|>2, then the color would be a certain
    color, and if it took 133 iterations, it would be a different colors.
    if c=256, then the color is black, the position is in the Mandlebrot
    set.

    > t=r*r-i*i-X+(x+X)*p,i=r*i*2+Y+(y-Y)*p,r=t;}
    The actual calculation of “Zn+1=Zn^2+c”. “c” here is calculated by
    “p”, “x”, “y”, “X” and “Y” – zoom level, current pixel, and target
    location.

    I didn’t notice any difference between odd and even lines… If you
    meant “c&7”, then that is the color calculation – there are 7
    different available colors, and “c&7” means taking the lowest 3 bits
    of “c” for deciding the color (i.e. when c=130, it gives the same
    color as c=138, c=146, c=2, …)

    Oded

    Comment by Oded — 2009-05-12 @ 20:15

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress