Bug in Fractal Script

I wrote a little fractal in OSL, which I have previously done in Java, and it is severely warped for some reason. Help?



shader Fractral(
    
    float Hue = 0,
    point pos = P,
    output color Color = 0
    
)


{


    int i;
    float real = pos[0];
    float imag = pos[1];
    float x = real;
    float y = imag;
    for(i = 0; i < 20; i++) {
        real = pow(real,2) - pow(imag, 2) + x;
        imag = 2*imag*real + y;
        if(sqrt(pow(real,2)+pow(imag,2))>2) break;
    } 
    Color = color("hsv", Hue, 1, i/20.0);
}


Mandelbrot in OSL


Mandelbrot in Java


from what I can see there is no test to check whether i exceeds 20; if so the color should be black but here it will be white. For example the point (0,0) is white but should be black. If you move the color assigment into a clause together with the break statement you will keep the default if the loop runs the maximum number of iterations:


...
if( ...test...){   // you need these curly braves
   Color = color(.....);
   break;
}

I was hoping that you’d see this post. You’re totally right about the color, dumb mistake… But the problem I have is with the shape of the fractal; it’s all stretched out diagonally, as if converging to an asymptote.

I think you made a small mistake in calculating the complex product. When computing the imaginary part you need to use the original real part, not the newly computed real part. The code below works (the change is in the first line of the for-clause)


shader Fractal(
    float Hue = 0,
    point pos = P,
    output color Color = 0
){
    int i;
    float real = pos[0];
    float imag = pos[1];
    float x = real;
    float y = imag;
    real = 0;
    imag = 0;
    for(i = 0; i < 20; i++) {
        float realt = pow(real,2) - pow(imag, 2) + x;
        imag = 2*imag*real + y;
        real = realt;
        if(pow(real,2)+pow(imag,2)>4){
          Color = color("hsv", Hue, 1, i/20.0);
          break;
        }
    } 
}

Ah, that would do it… I side-stepped the problem in Java by using a class for complex numbers that included add and multiply methods.
Thanks so much for your help and all of your OSL articles.

Thank you so much!

I am working on a similar project (mandelbrot set), but in realtime with vertices displacement in the BGE. I couldn’t figure out why it converged so slowly and with such asymptotic artifacts… I had made the same mistake (but actually in Python). Now I figured out thanks to you! :yes:

Hey Matpi,

Would you be willing to share your script for this mandelbrot generation in python?

Regards

Hewi