2013-04-20 14 views
6

私は以下のマンデルブロセットコードをCで持っています。計算をして、最終的なフラクタル画像用の.ppmファイルを作成しています。ポイントは、私のフラクタル画像は、90度回転していることを意味しています。あなたは私のコードを実行して確認することができます: ./mandel> test.ppm私のマンデルブロットセットコードの改善

一方、私はまた、色を変更したいと思います。私は、このフラクタル画像を達成したい:

enter image description here

私の最後の問題は、私のコードは私のコードの実行中の時間をチェックしないということです。私はこの部分のコードも持っていますが、コードの実行が終了すると実行時間は表示されません。誰かが自分のコードを適切に変更してこのフラクタルイメージを達成するのを手伝って、経過時間を表示すると喜んでもらえます。

#include <math.h> 
#include <stdlib.h> 
#include <time.h> 
#include <stdio.h> 

void color(int red, int green, int blue) 
{ 
    fputc((char)red, stdout); 
    fputc((char)green, stdout); 
    fputc((char)blue, stdout); 
} 

int main(int argc, char *argv[]) 
{ 
    int w = 600, h = 400, x, y; 
    //each iteration, it calculates: newz = oldz*oldz + p, where p is the current pixel, and oldz stars at the origin 
    double pr, pi;     //real and imaginary part of the pixel p 
    double newRe, newIm, oldRe, oldIm; //real and imaginary parts of new and old z 
    double zoom = 1, moveX = -0.5, moveY = 0; //you can change these to zoom and change position 
    int maxIterations = 1000;//after how much iterations the function should stop 

    clock_t begin, end; 
    double time_spent; 

    printf("P6\n# CREATOR: E.T/mandel program\n"); 
    printf("%d %d\n255\n",w,h); 

    begin = clock(); 

    //loop through every pixel 
    for(x = 0; x < w; x++) 
    for(y = 0; y < h; y++) 
    { 
     //calculate the initial real and imaginary part of z, based on the pixel location and zoom and position values 
    pr = 1.5 * (x - w/2)/(0.5 * zoom * w) + moveX; 
     pi = (y - h/2)/(0.5 * zoom * h) + moveY; 
     newRe = newIm = oldRe = oldIm = 0; //these should start at 0,0 
     //"i" will represent the number of iterations 
     int i; 
     //start the iteration process 
     for(i = 0; i < maxIterations; i++) 
     { 
      //remember value of previous iteration 
      oldRe = newRe; 
      oldIm = newIm; 
      //the actual iteration, the real and imaginary part are calculated 
      newRe = oldRe * oldRe - oldIm * oldIm + pr; 
      newIm = 2 * oldRe * oldIm + pi; 
      //if the point is outside the circle with radius 2: stop 
      if((newRe * newRe + newIm * newIm) > 4) break; 
     } 

     color(i % 256, 255, 255 * (i < maxIterations)); 

    } 

    end = clock(); 

    time_spent = (double)(end - begin)/CLOCKS_PER_SEC; 
    printf("Elapsed time: %.2lf seconds.\n", time_spent); 

    return 0; 
} 

答えて

7

パート1:あなたは正しい方向フラクタルを与える

for(y = 0; y < h; y++) 
for(x = 0; x < w; x++) 

: あなたはにあなたのループの順序を交換する必要があります。プリントアウトする時間を取得するには、あなたがstdoutにppmの出力を印刷しているので、標準エラー出力にそれを印刷する必要があり :

パート2

fprintf(stderr, "Elapsed time: %.2lf seconds.\n", time_spent); 

パート3: 連続スムーズな着色を得るために、正規化反復カウント方法などを使用する必要があります。ここであなたが望むものに似た何かを与え、あなたの着色部分の交換は、次のとおりです。

if(i == maxIterations) 
     color(0, 0, 0); // black 
    else 
    { 
     double z = sqrt(newRe * newRe + newIm * newIm); 
     int brightness = 256. * log2(1.75 + i - log2(log2(z)))/log2(double(maxIterations)); 
     color(brightness, brightness, 255); 
    } 

私は一種の正規化反復カウント方法の簡単なおおよその実装を行ったので、それはかなりありません。

Mandelbrot using some semi-continuous coloring

それは完全に連続着色はありませんが、それは近いの一種です。

+0

ありがとうございます!それは私の問題を解決しました。しかし、あなたのコードの最後の部分に括弧(小括弧)に関する小さなキャストエラーがあります。あなたが他の人のためにそれを修正できるなら、それは素晴らしいことかもしれません。 –

+0

@erkant、あなたはダブル(maxIterations)を意味しますか? gccでうまく動作します。それは別のものですか? –

+0

はい、私はそれについて話していました。それは私のために働かなかった、((ダブル)maxIterationsのような)括弧を使用しなければならなかった。 –