Creating Your Own Julia Sets in Visual C++

Frontiers 2002

July 16,2002

 

                        First, start Microsoft Visual C++.

 

                   Next, create a new project. I used the name julia1 for mine. The project name

                   gets built into all file names associated with the project.

 

                   Next you are going to cut and paste code from this page into the file julia1vw.cpp

                   or yournamevw.cpp  in your project. Note carefully the ‘vw” part of the filename. Make sure

                   and open the one with that as part of the name.

 

What follows was put into the file  julia1vw.cpp  which was generated by

Microsoft Visual C++ when a new project was created.  Only the code between the

BLUE LETTERS needs to be entered by you. Leave the rest alone so it runs ok.

 

When you have finished entering the code in, under Project in MSVC, save the modified file, then take the option

 

Build  yournamevw.exe  to create an executable. Then run it from the same menu.

___________________________

// find the code that follows:

 

void CJulia1View::OnDraw(CDC* pDC)

{

            CJulia1Doc* pDoc = GetDocument();

            ASSERT_VALID(pDoc); 

 

// now copy what follows into your file

 

// Beginning of Julia Graphic Algorithm

// my code follows:

            // variable declarations:

           

    char *message = "Julia Set #1";

            int len = strlen(message);

           

    COLORREF CLR;

                             

  

 

    int numits=100;       // determines refinement

  

    float x,y,newx,newy;    // real, imaginary components of z

    float  a,b,c,d,M1,M2;   // (a,b) and (c,d) determine the rectangle

                                       // in parameter space where the set is built

                                       // Note a<c and b<d

                                       // M1 and M2 are for resolution and will have

                                       // values of 640 and 480, for VGA monitors

    float cx=0,cy=0;        // user supplied coordinates of center of window

  

    int R=10,p,q,n;            // R will determine if a sequence has diverged

                                       // depending on how far it has strayed from the

                                       // origin.

                                       // p is the horizontal variable and will got

                                       // from a to c; q vertical, from b to d

    float k,l;

  

 

    // Computations:

   

          // calculate  a,b,c and d based upon the above

 

    a = cx -1.5;     

    c = cx +1.5;                                       

    b = cy -1.5;

    d = cy +1.5;

 

 

 

 

  

 

//  parameters from page 314 Barnsely 

// reference: "Fractals Are Everywhere"  Barnsley, M.  1988 Academic Press

 

 

    M1 = 640;     // size of Window 

    M2 = 480;

 

//Julia algorithm follows: 

 

    for(p=150;p<=790;p++)             /* for VGA, offset by 150,70   */

      {                               /* for centering purposes      */

            for(q=70;q<=550;q++)

             {

               k = a + (c - a)*((float)p)/M1;

               l = b + (d - b)*((float)q)/M2;

               // what follows is the Julia algorithm for a single pixel. The algorithm

               // recursively generates a complex sequence whose initial value is 0 + 0i 

               // and whose kth iteration is given by  zk+1 = f(zk)  where the function

               // f is defined in this example by  

               //                       2

               //               f(z) = z  - L

               //

               // and   L = k + il depends on the coordinates of the pixel

               // assumed to be located at (p-150, q-70)

               // Standard complex notation is used, so z = x + iy

               //                                                      

               // The values for the k+1st member of the sequence are temporarily

               // stored in newx + newy i   and then moved to x + y i

               //                                    2

               // Note that due to complex algebra, z - L breaks down to

               //     2  2

               //   (x -y  - k) + i( 2xy - l)

               // leading to the code below.

               //

               // Note the definition of "divergence" is that the sequence is

               // R or more units away from the origin. As soon as that happens,

               // we stop and see how many iterations it took to have this

               // happen. This is stored in the integer variable n.

               // The Julia algorithm then says that depending on what n is,

               // we color the pixel differently. This is reflected in the

               // various "if" statements.  The colors there can be anything  the

               // user is interested in. Ideally, there is a unique color for

               // each different value of n.

              

              

               x=0;  // initially the sequence begins at complex origin

               y=0;

               for(n=1;n<=numits;n++)

                {

   

                 newx = x*x - y*y - k;

                     newy = 2*x*y    -  l;

                     x = newx;              // iteration done here

                     y = newy;

                 if( (x*x + y*y)>R*R)   // is the sequence diverging?

                   {

                      // coloring of pixel comes next - much room to play here

                      // RGB function requires 3 parameters, each an integer

                      //    in the 0 to 255 range, for Red,Green, Blue, respectively

                  

                    if (n<3) CLR = RGB(0,255,0);   

                   

                    if ((n>2)&&(n<7)) CLR = RGB(100,200,250);

                    

                    if (n>6) CLR = RGB(100,0,0);                              

                   

           // what follows actually puts a color on the pixel. Three arguments,

           // the pixel coordinates and the color you want as  returned by the

           // RGB function discussed earlier

                                        

                   pDC->SetPixel(p-150,q-70,CLR);

                         

                                                             // depending on how long it took

                         n =numits;    // set n so loop quits    

                   }

                 }

             }

      }

 

 

 

 

 

 

pDC->TextOut(150,0,message,len);   // paste a title on it

           

 

// end of  Julia graphic algorithm

           

           

           

           

// what follows was generated by MSVC     

 

            // TODO: add draw code for native data here

}