Clojure / Fragmented Java Mandelbrot drawing

I am trying to port this algorithm to clojure.

My code

(defn calc-iterations [x y] 
  (let [c (struct complex x y)]
    (loop [z (struct complex 0 0) 
           iterations 0]
      (if (and (< 2.0 (abs z))
               (> max-iterations iterations))
        iterations
        (recur (add c (multiply z z)) (inc iterations))))))

The multiply, add, and paragraph functions work as they should. I checked them with a calculator. However, for the following values:

(calc-iterations 0.60703135 -0.33984375) ; should give me 2, instead I get 4
(calc-iterations -1.8421874 0.3515625 )  ; should give me 1, instead I get 3

I am checking the correct iteration numbers using another Java applet that I found on the net. it works, since it produces the correct conclusion. Its iterative function

protected int calcIterations( float x, float y ) {
    int iterations = 0;

    float xn = x, yn = y;

    while ( iterations < MAX_ITERATIONS ) {
        float xn1 = xn*xn - yn*yn;
        float yn1 = 2*xn*yn;

        xn = xn1 + x;
        yn = yn1 + y;

        float magsq = xn*xn + yn*yn;

        if ( magsq > 4 )
            break;

        iterations++;
    }

    System.out.println( x + " " + y + " " + iterations );
    return iterations;
}

Can anyone spot my mistake?

+3
source share
1 answer

I noticed two differences.

  • Java z = (x, y), , (0, 0). z = z ^ 2 + c, (0, 0) ^ 2 + (x, y) = (x, y), (x, y) , . - .
  • Java , z 2 , . - , .

, , , .

, , , | z | > 2 (.. | (x, y) | > 2), | z | > 2 (.. | (x ^ 2-y ^ 2 + x, 2xy + y) | > 2), Java , (x ^ 2-y ^ 2 + x, 2xy + y) , , .

+8

Source: https://habr.com/ru/post/1712340/


All Articles