2015-11-29 104 views
1

我正在研究逃逸時間分形作爲我的12年級項目,用C++編寫,使用簡單的graphics.h庫已經過時但似乎足夠了。 生成Mandelbrot集合的代碼似乎有效,我假定Julia集合將是相同的變體。下面是代碼: (這裏,fx和fy是簡單地起到將實際複雜座標等(-0.003,0.05)轉換爲屏幕上的像素的實際值)Julia設置渲染代碼

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()); 
    } 
} 

但這似乎並非如此。輸出窗口顯示半徑= 2的整個圓,其顏色對應於1次迭代的逸出時間。

此外,在試圖尋找一個解決這個問題,我已經看到了所有的算法其他人使用有點初始化初始座標如下:

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

有人能解釋一下我錯過了嗎?

+0

你有一個Mandelbrot發電機嗎?將其修改爲Julia涉及將變量更改爲常量。如果你減少了循環變量,這個'(int j = fy(-2); j> = fy(2); j - )'循環如何?這會表明'fy(-2)'大於'fy(2)'。 – karatedog

+0

@karatedog否,在BGI窗口中,(0.0)是左上角,(寬度,高度)是右下角。所以要從y = -2到y = +2,你必須減少j的值。是的,我有一個Mandelbrot集合發電機。 –

+0

'it'的價值是什麼?我建議通常,當你正在調試模擬每一個輸入數據,並使用常量值而不是變量(比如用50代替'it') – karatedog

回答

1

我猜測主要問題是變量i(虛部)被循環變量i錯誤地覆蓋。所以行

y= 2*x*y + i; 

給出了不正確的結果。這個變量應該重新命名爲im。更正後的版本附在下面,由於我沒有graphics.h,我使用屏幕作爲輸出。

#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; 
} 

並與輸入-0.75 0輸出在下面給出。

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 
+0

是的,這正是問題[我的一個非常愚蠢的問題]。非常感謝您指出! –

+0

@ Sh.A。別客氣。總是享受一點分形程序。 :-) – hbp

+0

然後,燃燒的船的分形也與mandelbrot相同,除了x和y的絕對值被採用 –