문제

I am trying to adapt the code here : http://code.activestate.com/recipes/577166-newton-fractals/ into C and am having some trouble. I am using C99's complex type.

I basically have tried a few different approaches which haven't worked. On every pixel it goes all the way to the maximum iteration every time, so the image comes out as a solid color.

Is there soemthing I have fundementally wrong about the way the types work in C, it seems like it should work, I reconstructed the algorithm pretty exactly.

    //newt.c

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

complex f(complex z); 


const int WIDTH = 512, HEIGHT = 512; 

const double SCALED_X_MAX = 1.0;
const double SCALED_X_MIN = -1.0;
const double SCALED_Y_MAX = 1.0;
const double SCALED_Y_MIN = -1.0;



const int MAX_ITERATIONS = 20;
const int EPSILON = 1e-3;

int main(int argc, char **argv) { 
    const double SCALED_WIDTH = SCALED_X_MAX - SCALED_X_MIN ;
    const double SCALED_HEIGHT = SCALED_Y_MAX - SCALED_Y_MIN ; 

    FILE * image = fopen("newton.ppm", "w") ; 
    fprintf(image, "P3\n");
    fprintf(image, "%d %d\n" , WIDTH , HEIGHT ) ; 
    fprintf(image, "%d\n", 255) ; 
    for ( int y = 0 ; y < HEIGHT ; ++y) { 
        double zy = y * (SCALED_HEIGHT)/(HEIGHT-1) + SCALED_Y_MIN;
        for ( int x = 0 ; x < WIDTH ; ++x ) { 
            double zx = x * (SCALED_WIDTH)/(WIDTH-1) + SCALED_X_MIN; 
            complex z = zx + zy*I;
            int iteration = 0;
            while(iteration < MAX_ITERATIONS ) { 
//                complex h=sqrt(DBL_EPSILON) + sqrt(DBL_EPSILON)*I;
                double h=1e-6;
/*
                complex zph = z + h;
                complex dz = zph - z;
                complex slope = (f(zph) - f(z))/dz; 
*/              
                complex volatile dz = (f(z + (h+h*I))  - f(z)) / (h+I*h) ;
                complex z0 = z - f(z) / dz; 
                //fprintf(stdout,"%f \n", cabs(z0 - z ));
                if ( cabs(z0 - z) < EPSILON){
                    break; 
                }
                z = z0;
                iteration++;
            }
            if (iteration != MAX_ITERATIONS) fprintf(stdout, "%d " , iteration ); 
            fprintf(image,"%3d %3d %3d ", iteration % 4 * 64 , 
                                       iteration % 8 * 32 , 
                                       iteration % 16 * 16);
        }
       fprintf(image, "\n") ; 
    }
    fclose(image) ; 

    exit(0); 

}
complex f(complex z ) { 
    return cpow(z,3)-1.0 ;
}
도움이 되었습니까?

해결책

After checking over the complex maths and not seeing any problems I noticed that your error is simply due to the integer division in the line

zy = y * (SCALED_HEIGHT)/(HEIGHT-1) + SCALED_Y_MIN;

and similarly for zx. Because (SCALED_HEIGHT) and (HEIGHT-1) are both integers this is not going to give you the floating point result you require. Try using:

zy = y * SCALED_HEIGHT * 1.0/(HEIGHT-1) + SCALED_Y_MIN;

and similarly for zx.

EDIT: Sorry the above was in error. Your SCALED_HEIGHT was in fact double so the above was actually ok. The real problem is simply in the line

const int EPSILON = 1e-3;

This will in fact always return zero, because it's an integer. You need to make EPSILON a floating point type.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top