Fractals

You don't have to directly control hardware with Espruino - you can use it for calculations too.

Using the code below you can plot a mandelbrot fractal:

function mandelbrot(sizex,sizey) {
 for (var y=0;y<sizey;y++) {
  var line="";
  for (var x=0;x<sizex;x++) {
   var Xr=0;
   var Xi=0;
   var Cr=(4.0*x/sizex)-2.0;
   var Ci=(4.0*y/sizey)-2.0;
   var i=0;
   while ((i<16) && ((Xr*Xr+Xi*Xi)<4)) {
    var t=Xr*Xr - Xi*Xi + Cr;
    Xi=2*Xr*Xi+Ci;
    Xr=t;
    i++;
   }
   line += (i&1) ? "*" : " ";
  }
  print(line);
 }
}

mandelbrot(32,32);

It'll draw something like this on the console:

********************************
***********           **********
*********               ********
*******                   ******
******                     *****
*****                       ****
****     *******             ***
***   ******* ** **           **
***  ******  * *   *          **
** *******   ** **  **         *
** ******  * *   ** **         *
* *****  ***       * **         
****** ***         *****        
***  * * *          * **        
*   * *   *         * **        
*   **              * **        
*                  ** **        
*   **              * **        
*   * *   *         * **        
***  * * *          * **        
****** ***         *****        
* *****  ***       * **         
** ******  * *   ** **         *
** *******   ** **  **         *
***  ******  * *   *          **
***   ******* ** **           **
****     *******             ***
*****                       ****
******                     *****
*******                   ******
*********               ********
***********           **********

This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.