Note that there are some explanatory texts on larger screens.

plurals
  1. POCode golf: the Mandelbrot set
    text
    copied!<p>Usual rules for the code golf. Here is an implementation in python as an example</p> <pre><code>from PIL import Image im = Image.new("RGB", (300,300)) for i in xrange(300): print "i = ",i for j in xrange(300): x0 = float( 4.0*float(i-150)/300.0 -1.0) y0 = float( 4.0*float(j-150)/300.0 +0.0) x=0.0 y=0.0 iteration = 0 max_iteration = 1000 while (x*x + y*y &lt;= 4.0 and iteration &lt; max_iteration): xtemp = x*x - y*y + x0 y = 2.0*x*y+y0 x = xtemp iteration += 1 if iteration == max_iteration: value = 255 else: value = iteration*10 % 255 print value im.putpixel( (i,j), (value, value, value)) im.save("image.png", "PNG") </code></pre> <p>The result should look like this</p> <p><img src="https://imgur.com/6Oqvi.png" alt="Mandelbrot set"></p> <p>Use of an image library is allowed. Alternatively, you can use ASCII art. This code does the same</p> <pre><code>for i in xrange(40): line = [] for j in xrange(80): x0 = float( 4.0*float(i-20)/40.0 -1.0) y0 = float( 4.0*float(j-40)/80.0 +0.0) x=0.0 y=0.0 iteration = 0 max_iteration = 1000 while (x*x + y*y &lt;= 4.0 and iteration &lt; max_iteration): xtemp = x*x - y*y + x0 y = 2.0*x*y+y0 x = xtemp iteration += 1 if iteration == max_iteration: line.append(" ") else: line.append("*") print "".join(line) </code></pre> <p>The result</p> <pre><code>******************************************************************************** ******************************************************************************** ******************************************************************************** ******************************************************************************** ******************************************************************************** ******************************************************************************** ******************************************************************************** ******************************************************************************** ******************************************************************************** ******************************************************************************** **************************************** *************************************** **************************************** *************************************** **************************************** *************************************** **************************************** *************************************** **************************************** *************************************** **************************************** *************************************** **************************************** *************************************** *************************************** ************************************** ************************************* ************************************ ************************************ *********************************** *********************************** ********************************** ************************************ *********************************** ************************************* ************************************ *********************************** ********************************** ******************************** ******************************* **************************** *************************** ***************************** **************************** **************************** *************************** ************************ * * *********************** *********************** * * ********************** ******************** ******* ******* ******************* **************************** *************************** ****************************** ***************************** ***************************** * * * **************************** ******************************************************************************** ******************************************************************************** ******************************************************************************** ******************************************************************************** ******************************************************************************** ******************************************************************************** </code></pre> <p><strong>Edit</strong>:</p> <p>Rules for the ASCII art:</p> <ul> <li>size in rows/columns is parametrized and the code must work with any valid value.</li> <li>at least three level of differentiation in density depending on the iteration count (so my prototype up there is not compliant)</li> <li>oriented horizontally (so my prototype up there is not compliant)</li> <li>critical parameters are fixed (max iteration = 1000, runaway value x<em>x + y</em>y &lt;= 4.0)</li> </ul> <p>Rules for the graphic:</p> <ul> <li>size in rows/columns is parametrized and the code must work with any valid value.</li> <li>at least three level of colors, gray scale</li> <li>oriented horizontally (my prototype is compliant)</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload