2016-05-02 18 views
6

Ich arbeite an dem folgenden Code in C. Bisher hat alles funktioniert und es ist auf das richtige Niveau gezoomt, etc, aber ich habe Probleme mit den Farben zu arbeiten, wie ich will. Im Idealfall würde ich so etwas, um am Ende wie unabhängig von Hautfarbe:C Mandelbrot Set Coloring

Mandelbrot Set w/ correct colors

aber mein Programm, wie es unten ist derzeit so etwas wie dies erzeugt:

Current Mandelbrot Set

Deshalb würde ich schätze jede Hilfe, die ich bekommen könnte, wenn ich die Farben so machen würde, wie ich es möchte.

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

#define ITERMAX 100.0 
#define DIVERGING 1.1 
#define XMAX 500 
#define YMAX 500 
#define COLORINTENSITY 255 

/* allow up to ITERMAX feedbacks searching for convergence 
for the feedback 
    z0 = 0 + 0i 
    znew = z^2 + c 
If we have not diverged to distance DIVERGING before ITERMAX feedbacks 
we will assume the feedback is convergent at this value of c. 
We will report divergence if |z|^2 > DIVERGING 
*/ 

/* We will print color values for each pixel from (0, 0) to (XMAX, YMAX) 
The color of pixel (cx, cy) will be set by convergent() 
            or by divergent() 
depending on the convergence or divergence of the feedback 
when c = cx + icy 
*/ 

/* The max value of the red, green, or blue component of a color */ 

void convergent(); /* one color for convergence */ 
void divergent(); /* a different color for divergence */ 

void feedback(double *x, double *y, double cx, double cy); 
void pixel(char red, char green, char blue); 
FILE *fp; 


int main() 
{ 
    fp = fopen("mandelbrot.ppm", "wb"); 
    double x, y, cx, cy; 
    int iteration,squarex, squarey, pixelx, pixely; 
    double grow=1.0; 

/* header for PPM output */ 
fprintf(fp, "P6\n# CREATOR: EK, BB, RPJ via the mandel program\n"); 
fprintf(fp, "%d %d\n%d\n",XMAX, YMAX, COLORINTENSITY); 

for (pixely = 0; pixely < YMAX; pixely++) { 
    for (pixelx = 0; pixelx < XMAX; pixelx++) { 
     cx = (((float)pixelx)/((float)XMAX)-0.5)/grow*3.0-0.7; 
     cy = (((float)pixely)/((float)YMAX)-0.5)/grow*3.0; 
     x = 0.0; y = 0.0; 
     for (iteration=1;iteration<ITERMAX;iteration++) { 
        feedback(&x, &y, cx, cy); 
        if (x*x + y*y > 100.0) iteration = 1000; 
     } 
     if (iteration==ITERMAX) { 
      iteration = x*x + y*y; 
      pixel((char) 0, (char) 0, (char) 0); 
     } 
     else { 
      iteration = sqrt(x*x + y*y); 
      pixel((char) iteration, (char) 0, (char) iteration); 
       } 
      } 
    } 
} 

void feedback(double *x, double *y, double cx, double cy) { 
/* Update x and y according to the feedback equation 
xnew = x^2 - y^2 + cx 
ynew = 2xy + cy 
(these are the real and imaginary parts of the complex equation: 
    znew = z^2 + c) 
*/ 
double xnew = (*x) * (*x) - (*y) * (*y) + cx; 
double ynew = 2 * *x * *y + cy; 
*x = xnew; 
*y = ynew; 
} 

void pixel(char red, char green, char blue) { 
/* put a r-g-b triple to the standard out */ 
fputc(red, fp); 
fputc(green, fp); 
fputc(blue, fp); 
} 
+5

Ihre Farbe ist immer gleich, weil Sie immer die R- und B-Werte auf die Iterationsnummer und G auf Null setzen. Überdenken Sie das ... –

+1

Preset ein Farbfeld von 'ITERMAX' Elementen, um von 'R = 0, G = 0, B = 255' zu 'R = 255, G = 255, B = 255' über den Iterationsbereich zu verblassen . Es könnte ein Array von 'struct' sein, oder ein Array für jede Farbe. Wenn Sie möchten, dass * jede * Karte unabhängig vom Zoom von Blau auf Weiß wechselt, müssen Sie die kleinste Anzahl von Iterationen in jeder Karte finden und die Farben von dort aus eingrenzen. –

+0

Nur eine Seite über Codierungsstil: Sie sollten einen Einzug Betrag pro Block-Ebene auswählen und dabei bleiben. 4 pro Blocklevel ist ziemlich üblich. – lurker

Antwort

3

das Banding Um dies zu beheben, müssen Sie Ihre Tabellen iterieren Ihren Maximalwert für die Iterationszahlcode zu finden, die dann andere Werte skalieren auf diesen max relativ zu sein (dh. normalisieren die Werte). Sie können die Werte auch logarithmisch neu skalieren, um die Steigung der Farbänderung anzupassen.

Und Sie möchten wahrscheinlich nicht direkt im RGB-Raum arbeiten. Wenn Sie Ihre Farben im HSB-Raum definieren, können Sie einen konstanten Farbton und eine konstante Sättigung festlegen und die Helligkeit proportional zu den normalisierten Iterationszahlen variieren.

+0

erstes Bild verwendet smoth Farbverlauf: http://StackOverflow.com/Questions/369438/Smooth-Spectrum-for-Mandelbrot-Set-Rendering, Second Level Set-Methode – Adam