Sh.A
Sh.A

Reputation: 97

Julia Set rendering code

I am working on escape-time fractals as my 12th grade project, to be written in c++ , using the simple graphics.h library that is outdated but seems sufficient. The code for generating the Mandelbrot set seems to work, and I assumed that Julia sets would be a variation of the same. Here is the code: (Here, fx and fy are simply functions to convert the actual complex co-ordinates like (-0.003,0.05) to an actual value of a pixel on the screen.)

int p;
x0=0, y0=0;
long double r, i;
cout<<"Enter c"<<endl;
cin>>r>>i;
for(int i= fx(-2); i<=fx(2); i++)
{
    for(int j= fy(-2); j>=fy(2); j--)
    {
        long double x=0.0, y= 0.0,t;
        x= gx(i), y= gy(j);
        int k= -1;

        while(( x*x + y*y <4)&& k<it-1)
        {
            t= x*x - y*y + r;
            y= 2*x*y + i ;
            x=t;
            k++;

        }
        p= k*pd;
        setcolor(COLOR(colour[p][0],colour[p][1],colour[p][2]));
        putpixel(i,j,getcolor());
    }
}

But this does not seem to be the case. The output window shows the entire circle of radius=2 with the colour corresponding to an escape time of 1 iteration.

Also, on trying to search for a solution to this problem, I've seen that all the algorithms others have used initializes the initial co-ordinates somewhat like this:

x = (col - width/2)*4.0/width;
y = (row - height/2)*4.0/width;

Could somebody explain what I'm missing out?

Upvotes: 1

Views: 5136

Answers (2)

hassan anwar
hassan anwar

Reputation: 119

would you please tell how you display the image by using these graphics.h library

//setcolor(COLOR(colour[p][0],colour[p][1],colour[p][2])); //putpixel(i,j,getcolor());

Upvotes: 0

hbp
hbp

Reputation: 255

I guess that the main problem is that the variable i (imaginary part) is mistakenly overridden by the loop variable i. So the line

y= 2*x*y + i;

gives the incorrect result. This variable should be renamed as, say im. The corrected version is attached below, Since I don't have graphics.h, I used the screen as the output.

#include <iostream>
using namespace std;

#define WIDTH  40
#define HEIGHT 60

/* real to screen */
#define fx(x) ((int) ((x + 2)/4.0 * WIDTH))
#define fy(y) ((int) ((2 - y)/4.0 * HEIGHT))

/* screen to real */
#define gx(i) ((i)*4.0/WIDTH - 2)
#define gy(j) ((j)*4.0/HEIGHT - 2)

static void julia(int it, int pd)
{
  int p;
  long double re = -0.75, im = 0;
  long double x0 = 0, y0 = 0;

  cout << "Enter c" << endl;
  cin >> re >> im;
  for (int i = fx(-2.0); i <= fx(2.0); i++)
  {
      for (int j = fy(-2.0); j >= fy(2.0); j--)
      {
          long double x = gx(i), y = gy(j), t;
          int k = 0;

          while (x*x + y*y < 4 && k < it)
          {
              t = x*x - y*y + re;
              y = 2*x*y + im;
              x = t;
              k++;
          }
          p = (int) (k * pd);
          //setcolor(COLOR(colour[p][0],colour[p][1],colour[p][2]));
          //putpixel(i,j,getcolor());
          cout << p; // for ASCII output
      }
      cout << endl; // for ASCII output
  }
}

int main(void)
{
  julia(9, 1);
  return 0;
}

and the output with input -0.75 0 is given below.

0000000000000000000000000000000000000000000000000000000000000
0000000000000000000001111111111111111111000000000000000000000
0000000000000000011111111111111111111111111100000000000000000
0000000000000001111111111111111111111111111111000000000000000
0000000000000111111111111122222222211111111111110000000000000
0000000000011111111111122222349432222211111111111100000000000
0000000001111111111112222233479743322222111111111111000000000
0000000011111111111222222334999994332222221111111111100000000
0000000111111111112222223345999995433222222111111111110000000
0000011111111111122222234479999999744322222211111111111100000
0000011111111111222222346899999999986432222221111111111100000
0000111111111111222223359999999999999533222221111111111110000
0001111111111112222233446999999999996443322222111111111111000
0011111111111112222233446999999999996443322222111111111111100
0011111111111122222333456899999999986543332222211111111111100
0111111111111122223334557999999999997554333222211111111111110
0111111111111122233345799999999999999975433322211111111111110
0111111111111122233457999999999999999997543322211111111111110
0111111111111122334469999999999999999999644332211111111111110
0111111111111122345999999999999999999999995432211111111111110
0111111111111122379999999999999999999999999732211111111111110
0111111111111122345999999999999999999999995432211111111111110
0111111111111122334469999999999999999999644332211111111111110
0111111111111122233457999999999999999997543322211111111111110
0111111111111122233345799999999999999975433322211111111111110
0111111111111122223334557999999999997554333222211111111111110
0011111111111122222333456899999999986543332222211111111111100
0011111111111112222233446999999999996443322222111111111111100
0001111111111112222233446999999999996443322222111111111111000
0000111111111111222223359999999999999533222221111111111110000
0000011111111111222222346899999999986432222221111111111100000
0000011111111111122222234479999999744322222211111111111100000
0000000111111111112222223345999995433222222111111111110000000
0000000011111111111222222334999994332222221111111111100000000
0000000001111111111112222233479743322222111111111111000000000
0000000000011111111111122222349432222211111111111100000000000
0000000000000111111111111122222222211111111111110000000000000
0000000000000001111111111111111111111111111111000000000000000
0000000000000000011111111111111111111111111100000000000000000
0000000000000000000001111111111111111111000000000000000000000
0000000000000000000000000000000000000000000000000000000000000

Upvotes: 2

Related Questions