Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Mandelbrot Rendering Graphics

  • 05-06-2003 12:41AM
    #1
    Registered Users, Registered Users 2 Posts: 1,766 ✭✭✭


    Hi All,

    I recently dug up an ancient college C programming assignment of mine. This simply drew a Mandelbrot on screen. I think this was done on FreeBSD ver? back in 1995. I'm trying to render this on my copy of RedHat. Now, I haven't touch c in awhile but the below is easy enough except for the 4 graphical operations below:

    These graphical operations were provided to us students in manlib.h library file which contained the following 4 functions:
    makewindow(), drawpixel(), keepwindow() and getmouse(). I never saw the internally working of these 4 functions.

    #define COLORBLACK 0 /*the standard value for black*/
    #define NRCOLORS 21 /*nr colors available, from 0 to NRCOLORS-1 */


    /*Create a window with pixel dimensions (size * size). Its top lefthand corner is (0,0) and bottom right is (size,size).
    */
    void makewindow(unsigned int size);

    /*
    * draw a pixel, in the window created by makewindow, at point
    * (x,y) with colour given by color.
    * note that color must be in range BLACK to NRCOLORS-1
    */
    void drawpixel(unsigned int x, unsigned int y, int color);

    /*
    * Keep the window displayed until a mouse button is `clicked'
    * in the window.
    */
    void keepwindow ();



    /*
    * Get the next event from the mouse. Function returns values
    * in the range 1..3 ( left, middle and right mouse button pressed).
    * (x,y) gives the coordinates of the pointer when the button
    * was pressed.
    */
    int getmouse(unsigned int *x, unsigned int *y);





    As I said, I never saw the internally working of these 4 functions but I would love to replace them with my own but I haven't a clue under c for graphics. I have redhat 9 with the gcc compiler working. How difficult would it be to replace the 4 operations above? Are there closely native operations in c that would do the above? ie
    i) getting the mouse co-ordinates
    ii) keeping the window. I don't understand this one. Basically a terminal-like window holds the image until it is clicked closed
    iii) drawpixel... is there a draw command?
    iv) oh oh... making a window... I think this involves creating initially an empty terminal-like window to hold the rendering imagine.

    As you can see my experience in graphics in c was limited. Any help would be greatly appreciated.



    The original code example, using "manlib.h" for those hidden graphical operations that I don't have.



    /*
    * mandel.c
    *
    * A program to i) Plot the Mandelbrot set in the Complex Plane
    * ii) Allow the functionality of a scaleable Fractal
    * iii) Allow user to set Interactive Zoom Rate
    *
    * Date: 7/12/95
    *
    */

    #include <stdio.h>
    #include "manlib.h"

    #define WINSIZE 250 /* Set window size to 250 x 250 pixels */
    #define INFINITY 100 /* Set infinity to beyond 100 iterations */

    double acorn,bcorn,size, /* Co-ordinates a and b, size S */
    ca,cb, /* 1st and 2nd part of Complex Number */
    zx,zy, /* x and y component of Complex Number */
    xtemp, /* zx is computed in temporary value */
    mag, /* User can change Zoom Rate */
    temp=(2.5/WINSIZE), /* Conversion depending on WINSIZE */
    compval_x,compval_y; /* Complex conversion of x,y co-ords */

    int j,k, /* Screen co-ordinates j,k */
    count, /* Number of iterations */
    colorfactor; /* Colour designer */
    unsigned int button=1, /* Reads a value from a mouse button */
    x,y; /* Reads mouse click co-ordinates */

    void print_instructions() /* Program objectives and operations */
    {
    printf("\n%s"," Welcome to MANDELBROT BROWSER.");
    printf("\n\n%s","A program to plot the Mandelbrot set in the complex");
    printf("\n%s","plane. The top-left hand corner of the plane is given");
    printf("\n%s","by complex number (a+bi).");
    printf("\n%s","The size of the plane is given by non-zero S.");
    printf("\n\n%s","Input the real numbers a b and S");
    printf("\n%s","(Try -2 -1.25 2.5 ): ");
    }

    void initialization() /* Set up colours, Ask user for co-ords, Zoom rate */
    {
    colorfactor=(INFINITY/(NRCOLORS-1)); /* Defines colour range */
    scanf("%lf %lf %lf",&acorn,&bcorn,&size); /* Read in a,b and size */
    printf("\n%s","Please enter interactive Zoom Factor (from 2 to 20): ");
    scanf("%lf",&mag); /* Ask user for Zoom Rate */
    while ((mag<2) || (mag>20)) /* If user enters a value outside range */
    {
    printf("\n%s","Sorry enter a Zoom rate between 2 to 20 : ");
    scanf("%lf",&mag);
    }
    }

    void draw_mandelbrot() /* The Mandelbrot designer engine */
    {
    for (j=1; j<=WINSIZE; j++) /* Draw (1,*)..(2,*)..etc */
    {
    for (k=1; k<=WINSIZE; k++) /* Draw (*,1)..(*,2)..etc */
    {
    count=0;
    ca=acorn+((j*size)/WINSIZE); /* 1st part of Complex Number */
    cb=bcorn+((k*size)/WINSIZE); /* 2nd part of Complex Number */
    zx=0; /* Initialize x component */
    zy=0; /* Initialize y component */

    while ((count<INFINITY) && ((zx*zx)+(zy*zy)) <=4 )
    {
    count++; /* Next Iteration */
    xtemp=(zx*zx)-(zy*zy); /* X part is computed in temp */
    zy=(2*zx*zy)+cb; /* RE and IM parts are added */
    zx=xtemp+ca; /* to zy and zx in that order */
    }

    if (count==INFINITY)
    drawpixel (j,k,0); /* Draw black pixels in lake */
    else /* else draw non-black pixel */
    drawpixel (j,k,((count/colorfactor)+1));

    } /* For K */
    } /* For J */
    } /* Draw_mandelbrot */

    void print_options() /* User mouse options */
    {
    printf("\a\n%s","Finished plotting Mandelbrot set.");
    printf("\n\n%s","Click middle mouse button to exit.");
    printf("\n%s","Click left mouse button at a region");
    printf("\n%s","to enlarge it.");
    printf("\n%s","Click right mouse button at a region");
    printf("\n%s\n","to zoom out from it.");
    } /* print_options */

    void zoom() /* Zoom in/out with new acorn and bcorn */
    {
    compval_x=(x*(temp)+acorn); /* Adding on old acorn */
    compval_y=(y*(temp)+bcorn); /* Adding on old acorn */
    acorn=compval_x-(size/2); /* New acorn, bcorn */
    bcorn=compval_y-(size/2);
    temp=temp*(1/mag); /* Creates a finer draw mesh */
    printf("\a\n%s%lf %s %lf%s %lf\n","Plotting from (",acorn,"+",bcorn,
    "i) at size",size);
    draw_mandelbrot(); /* Draw new zoomed fractal */
    } /* zoom */

    void magnify_mandelbrot()
    {
    while (button!=2) /* Terminate with middle mouse button */
    {
    button=getmouse(&x,&y); /* Records mouse clicked */
    if (button==1) /* If left mouse button is clicked */
    {
    size=(size/mag); /* Zoom in Fractal by a factor of mag */
    zoom();
    }
    if (button==3) /* If right mouse button is clicked */
    {
    size=(size*mag); /* Zoom out Fractal by a factor of mag */
    zoom();
    }
    }
    } /* Magnify_mandelbrot */

    void main()
    {
    print_instructions(); /* Introduction */
    initialization(); /* Set up Initial parameters */
    makewindow(WINSIZE); /* Draws window at WINSIZE */
    draw_mandelbrot(); /* Draw initial Mandelbrot */
    print_options(); /* User decides next move using mouse */
    magnify_mandelbrot(); /* Zoom in/out or quit Mandelbrot */

    printf("\n%s\n","Bye! Thanks for using Mandelbrot Browser.");
    }


Comments

  • Closed Accounts Posts: 16 Insectman2000


    Originally posted by hamster
    Hi All,


    /*
    * mandel.c
    *
    * A program to i) Plot the Mandelbrot set in the Complex Plane
    * ii) Allow the functionality of a scaleable Fractal
    * iii) Allow user to set Interactive Zoom Rate
    *
    * Date: 7/12/95
    *
    */

    #include <stdio.h>
    #include "manlib.h"

    #define WINSIZE 250 /* Set window size to 250 x 250 pixels */
    #define INFINITY 100 /* Set infinity to beyond 100 iterations */

    double acorn,bcorn,size, /* Co-ordinates a and b, size S */
    ca,cb, /* 1st and 2nd part of Complex Number */
    zx,zy, /* x and y component of Complex Number */
    xtemp, /* zx is computed in temporary value */
    mag, /* User can change Zoom Rate */
    temp=(2.5/WINSIZE), /* Conversion depending on WINSIZE */
    compval_x,compval_y; /* Complex conversion of x,y co-ords */

    int j,k, /* Screen co-ordinates j,k */
    count, /* Number of iterations */
    colorfactor; /* Colour designer */
    unsigned int button=1, /* Reads a value from a mouse button */
    x,y; /* Reads mouse click co-ordinates */

    void print_instructions() /* Program objectives and operations */
    {
    printf("\n%s"," Welcome to MANDELBROT BROWSER.");
    printf("\n\n%s","A program to plot the Mandelbrot set in the complex");
    printf("\n%s","plane. The top-left hand corner of the plane is given");
    printf("\n%s","by complex number (a+bi).");
    printf("\n%s","The size of the plane is given by non-zero S.");
    printf("\n\n%s","Input the real numbers a b and S");
    printf("\n%s","(Try -2 -1.25 2.5 ): ");
    }

    void initialization() /* Set up colours, Ask user for co-ords, Zoom rate */
    {
    colorfactor=(INFINITY/(NRCOLORS-1)); /* Defines colour range */
    scanf("%lf %lf %lf",&acorn,&bcorn,&size); /* Read in a,b and size */
    printf("\n%s","Please enter interactive Zoom Factor (from 2 to 20): ");
    scanf("%lf",&mag); /* Ask user for Zoom Rate */
    while ((mag<2) || (mag>20)) /* If user enters a value outside range */
    {
    printf("\n%s","Sorry enter a Zoom rate between 2 to 20 : ");
    scanf("%lf",&mag);
    }
    }

    void draw_mandelbrot() /* The Mandelbrot designer engine */
    {
    for (j=1; j<=WINSIZE; j++) /* Draw (1,*)..(2,*)..etc */
    {
    for (k=1; k<=WINSIZE; k++) /* Draw (*,1)..(*,2)..etc */
    {
    count=0;
    ca=acorn+((j*size)/WINSIZE); /* 1st part of Complex Number */
    cb=bcorn+((k*size)/WINSIZE); /* 2nd part of Complex Number */
    zx=0; /* Initialize x component */
    zy=0; /* Initialize y component */

    while ((count<INFINITY) && ((zx*zx)+(zy*zy)) <=4 )
    {
    count++; /* Next Iteration */
    xtemp=(zx*zx)-(zy*zy); /* X part is computed in temp */
    zy=(2*zx*zy)+cb; /* RE and IM parts are added */
    zx=xtemp+ca; /* to zy and zx in that order */
    }

    if (count==INFINITY)
    drawpixel (j,k,0); /* Draw black pixels in lake */
    else /* else draw non-black pixel */
    drawpixel (j,k,((count/colorfactor)+1));

    } /* For K */
    } /* For J */
    } /* Draw_mandelbrot */

    void print_options() /* User mouse options */
    {
    printf("\a\n%s","Finished plotting Mandelbrot set.");
    printf("\n\n%s","Click middle mouse button to exit.");
    printf("\n%s","Click left mouse button at a region");
    printf("\n%s","to enlarge it.");
    printf("\n%s","Click right mouse button at a region");
    printf("\n%s\n","to zoom out from it.");
    } /* print_options */

    void zoom() /* Zoom in/out with new acorn and bcorn */
    {
    compval_x=(x*(temp)+acorn); /* Adding on old acorn */
    compval_y=(y*(temp)+bcorn); /* Adding on old acorn */
    acorn=compval_x-(size/2); /* New acorn, bcorn */
    bcorn=compval_y-(size/2);
    temp=temp*(1/mag); /* Creates a finer draw mesh */
    printf("\a\n%s%lf %s %lf%s %lf\n","Plotting from (",acorn,"+",bcorn,
    "i) at size",size);
    draw_mandelbrot(); /* Draw new zoomed fractal */
    } /* zoom */

    void magnify_mandelbrot()
    {
    while (button!=2) /* Terminate with middle mouse button */
    {
    button=getmouse(&x,&y); /* Records mouse clicked */
    if (button==1) /* If left mouse button is clicked */
    {
    size=(size/mag); /* Zoom in Fractal by a factor of mag */
    zoom();
    }
    if (button==3) /* If right mouse button is clicked */
    {
    size=(size*mag); /* Zoom out Fractal by a factor of mag */
    zoom();
    }
    }
    } /* Magnify_mandelbrot */

    void main()
    {
    print_instructions(); /* Introduction */
    initialization(); /* Set up Initial parameters */
    makewindow(WINSIZE); /* Draws window at WINSIZE */
    draw_mandelbrot(); /* Draw initial Mandelbrot */
    print_options(); /* User decides next move using mouse */
    magnify_mandelbrot(); /* Zoom in/out or quit Mandelbrot */

    printf("\n%s\n","Bye! Thanks for using Mandelbrot Browser.");
    }


    stupidthreadwarning.gif


  • Closed Accounts Posts: 392 ✭✭Skyclad


    Try writing it in Prolog. Thats the way of the future I hear. Or openGL, but that one tend to wreck the head too much.

    Dave


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Insectman2000 wins the contest to find the stupidest and most pointless post ever to the programming board.

    Prize is a lifetime ban.

    Congratulations.


Advertisement