簡體   English   中英

我的Mandelbrot設置代碼出了什么問題?

[英]What's wrong with my Mandelbrot set code?

我正在嘗試用C語言實現Mandelbrot,但是我遇到了一個奇怪的問題。 我的代碼如下:

#include <stdio.h>
#include <math.h>
#include <complex.h>

int iterate_pt(complex c);

int main() {
FILE *fp;
fp = fopen("mand.ppm", "w+");


double crmin = -.75;
double crmax = -.74;
double cimin = -.138;
double cimax = -.75; //Changing this value to -.127 fixed my problem.

int ncols = 256;
int nrows = 256;
int mand[ncols][nrows];
int x, y, color;
double complex c;

double dx = (crmax-crmin)/ncols;
double dy = (cimax-cimin)/nrows;

for (x = 0; x < ncols; x++){
    for (y = 0; y < nrows; y++){
        double complex imaginary = 0+1.0i;
        c = crmin+(x*dx) + (cimin+(y*dy)) * imaginary;
        mand[x][y] = iterate_pt(c);
    }
}

printf("Printing ppm header.");
fprintf(fp, "P3\n");
fprintf(fp, "%d %d\n255\n\n", ncols, nrows);

for (x = 0; x < ncols; x++) {
    for (y = 0; y < nrows; y++){
        color = mand[x][y];
        fprintf(fp, "%d\n", color);
        fprintf(fp, "%d\n", color);
        fprintf(fp, "%d\n\n", color); //Extra new line added, telling the ppm to go to next pixel.
    }
}
fclose(fp);

return 0;
}

int iterate_pt(double complex c){
double complex z = 0+0.0i;
int iterations = 0;
int k;
for (k = 1; k <= 255; k++) {
    z = z*z + c;
    if (sqrt( z*conj(z) ) > 50){
        break;
    }
    else
        ++iterations;
}
return iterations;
}

但是,此程序的輸出(存儲為ppm文件)如下所示:

使用GIMP轉換為GIF。我可以確認GIF和原始PPM看起來與PPM和GIF完全相同

謝謝你的幫助!

嘗試將cimax設置為-0.127,我也正在研究這個項目,它似乎可以做到這一點;)

代碼看起來不錯。 但你的起始矩形看起來不正確!

您正在使用

Real ranage [  -.75 ,  -.74 ]
Imag range  [ -.138 ,  -.75 ]

你確定這是你的意圖嗎? 對我來說,這似乎是一個非常緊張的y尺度。

此外,標准的mandelbrot算法傾向於使用

magnitude > 2

而不是50作為逃脫檢查。 雖然這不應該影響集合的實際形狀。

順便說一下,計算z * conj(z)的sqrt是沒有意義的。 簡單地對不等式兩邊的表達式進行平方,給出if (z*conj(z) > 2500)並且你提高了性能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM