2017-10-31 3 views
0

ich versuche ein julia setzen bild und programmieren dies in C. Ich habe versucht, einen Algorithmus zu erforschen, um ein Bild des Julia-Sets zu erstellen, werde aber von den meisten Beispielen online verwirrt (da die meisten Beispiele kopiert zu werden scheinen, aber mit wenig Erklärung dafür, was Schritt für Schritt passiert).julia set berechnung mit reellen zahlen

Ich habe meinen Code mehrere Male neu geschrieben, um mit Algorithmen zu passen, die ich online gefunden habe, aber mit wenig Erfolg. Derzeit verwende ich eine Iteration Funktion eine Farbe jeden Pixel in dem folgenden zu definieren:

// Loop through each pixel to work out new image set 
int x, y; 
for(x = 0; x < SIZE; x++){ 
    for(y = 0; y < SIZE; y++){ 

     int i = iterate(x, y, maxIterations, c); 
     image[x][y] = i % 256 + 255 + 255 * (i < maxIterations); 

    } 
} 

/* Iterate function */ 
int iterate(int x, int y, int maxI, double c[]){ 
    double z, zx, zy, oldRe, oldIm; //real and imaginary parts of new and old 
    double xmin = -1.0, xmax = 1.0, ymin = -1.0, ymax = 1.0; 
    int k; // number of times iterated 

    //calculate the initial real and imaginary part of z 
    // z0 = (x + yi)^2 + c = (0 + 0i) + c = c 
    zx = 1.5*(x - SIZE/2)/(0.5*SIZE); 
    zy = 1.0*(y - SIZE/2)/(0.5*SIZE); 

    //start the iteration process 
    for(k = 1; k < maxI; k++){ 
     //remember value of previous iteration 
     oldRe = zx; 
     oldIm = zy; 
     z = zx*zx - zy*zy + c[0]; 

     //the actual iteration, the real and imaginary part are calculated 
     zx = oldRe * oldRe - oldIm * c[1] + c[0]; 
     zy = 2 * oldRe * oldIm + c[1]; 
     zy = 2.0*zx*zy + c[1]; 
     zx = z; 

     //if the point is outside the circle with radius 2: stop 
     if((zx * zx + zy * zy) > 4) break; 
} 

/* 
while(((zx * zx + zy * zy) > 4) && (k < maxI)) { 
    //remember value of previous iteration 
    z = zx*zx - zy*zy + c[0]; 
    zy = 2*x*y-c[1]; 
    zx = z; 
    //if the point is outside the circle with radius 2: stop 
} 
return k; 

} */ Die kommentierte out while-Schleife ein zweiter Algorithmus fand ich online, aber ist auch nicht korrekt. Ich bin mir nicht sicher, was ich in der Gleichung vermisse. Irgendwelche Ideen?

(Ich suche keine Lösung, nur ein Pseudocode, der die reale, imaginäre komplexe Zahl in eine Position in meiner Bounding Box umwandeln kann, in diesem Fall: -1,1). Hier

+0

komplexe Zahl Typ wird in Typ in c gebaut, so dass man z = z * z + c verwenden können – Adam

Antwort

0

ist die retranscription in lesbaren Code eines Schulprojektes Ich wurde gerade

validiert

YOUR_CR und YOUR_CI sind die beiden möglichen Variablen für Julia, LOWER_LIMIT und UPPER_LIMIT (X und Y) entspricht dem Fenster Sie wollen Zeichnen Sie den Code ein. Der Code sollte die Ausgabe auf das Fenster und die Zoomstufe, in die Sie das Fraktal zeichnen möchten, korrekt skalieren (die Grenzskala liegt in mathematischen Grenzen des Fraktals, nicht in Pixeln).

# define X 0 
# define Y 1 

# define ZR 0 
# define ZI 1 
# define CR 2 
# define CI 3 

static int   iterations(long double coords[]) 
{ 
    long double  pos[4]; 
    int    i; 
    double   tmp; 

    pos[CR] = YOUR_CR; 
    pos[CI] = YOUR_CI; 
    pos[ZR] = coords[X]; 
    pos[ZI] = coords[Y]; 
    i = 0; 
    while (i < MAX_ITERATIONS && (PYTHAGORE(pos[ZR], pos[ZI]) < 4)) 
    { 
     tmp = pos[ZR]; 
     pos[ZR] = pos[ZR] * pos[ZR] - pos[ZI] * pos[ZI] + pos[CR]; 
     pos[ZI] = 2 * pos[ZI] * tmp + pos[CI]; 
     i++; 
    } 
    return (i); 
} 

# define TRANSLATE_X_PIXEL(a, b) ((int)(((a - LOWER_LIMIT_X)/(UPPER_LIMIT_X - LOWER_LIMIT_X)) * WIDTH)) 
# define TRANSLATE_Y_PIXEL(a, b) ((int)(((a - LOWER_LIMIT_Y)/(UPPER_LIMIT_Y - LOWER_LIMIT_Y)) * HEIGHT)) 

void    julia() 
{ 
    long double  coords[2]; 
    int    itera; 

    coords[X] = LOWER_LIMIT_X; 
    while ((coords[X] += ((UPPER_LIMIT_X - LOWER_LIMIT_X)/
        (double)(WIDTH + 1))) <= UPPER_LIMIT_X) 
    { 
     coords[Y] = LOWER_LIMIT_Y; 
     while ((coords[Y] += ((UPPER_LIMIT_Y - LOWER_LIMIT_Y)/
         (double)(HEIGHT + 1))) <= UPPER_LIMIT_Y) 
     { 
       itera = iterations(coords); 
       set_pixel(TRANSLATE_X_PIXEL(coords[Y]), TRANSLATE_Y_PIXEL(coords[Y]), itera * DESIRED_COLOR); 
     } 
    } 
} 
0

Der out-kommentierten while Code falsch ist, wie es in der x,y Imaginärteil Berechnung anstelle von zx,zy verwendet. Ihr Code sieht so aus, als ob Sie einen Ansatz gewählt und einen anderen in der Mitte eingefügt hätten.

Was möchten Sie die quadratische klassische Julia Iteration, es ist die Realisierung in realen Komponenten von

ist
znext = z*z + c = (zx+*zy)*(zx+i*zy)+(cx+*cy) 
     = (zx*zx-zy*zy+cx) + i*(2*zx*zy+cy) 

Sie können nicht, da sowohl zx an Ort und Stelle Ersatz tun und zy verwendet werden, in beiden Formeln und müssen halten ihre alten Werte dort. Der richtige Code sollte

for(k = 1; k < maxI; k++){ 
    // compute the new real part, store temporarily in helper variable 
    double z = zx*zx - zy*zy + c[0]; 
    // compute the imaginary part, since no further operations, do it in place 
    zy = 2.0*zx*zy + c[1]; 
    //store the new real part in the real part variable 
    zx = z; 
    //if the point is outside the circle with radius 2: stop 
    if((zx * zx + zy * zy) > 4) break; 
} 

Sie würden weniger multplications benötigen, wenn Sie die Quadrate speichern,

for(k = 1; k < maxI; k++){ 
    double sqzx = zx*zx, sqzy = zy*zy; 
    // compute the imaginary part first, since the real part only uses the squares, not zy 
    zy = 2.0*zx*zy + c[1]; 
    // compute the new real part using the pre-computed squares 
    zx = sqzx - sqzy + c[0]; 
    //if the point is outside the circle with radius 2: stop 
    if((sqzx + sqzy) > 4) break; 
} 
Verwandte Themen