title: Clojure/Java Mandelbrot Fractal drawing
body: <p>I am trying to port this <a href="https://stackoverflow.com/questions/425953/how-to-program-a-fractal">algorithm</a> to clojure.</p>
  
  <p>My code is</p>
  
  <pre><code>(defn calc-iterations [x y] 
    (let [c (struct complex x y)]
      (loop [z (struct complex 0 0) 
             iterations 0]
        (if (and (&lt; 2.0 (abs z))
                 (&gt; max-iterations iterations))
          iterations
          (recur (add c (multiply z z)) (inc iterations))))))
  </code></pre>
  
  <p>Multiply, add and abs functions are working as they should.  I have tested them with a calculator. However for the following values:</p>
  
  <pre><code>(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
  </code></pre>
  
  <p>I am checking the correct iteration numbers using another java applet that I have found on the net. it seems to be working since it produces correct output. Its iteration function is </p>
  
  <pre><code>protected int calcIterations( float x, float y ) {
      int iterations = 0;
  
      float xn = x, yn = y;
  
      while ( iterations &lt; 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 &gt; 4 )
              break;
  
          iterations++;
      }
  
      System.out.println( x + " " + y + " " + iterations );
      return iterations;
  }
  </code></pre>
  
  <p>Can anyone spot my error?</p>