Just For Fun: Mandelbrot Generator for Python and Notepad++

Ron Jensen
2014-01-28
2014-05-19
  • Ron Jensen
    Ron Jensen
    2014-01-28

    I wrote this as an exercise in Python a few months back. It might be useful or fun for somebody:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    #!/usr/bin/python
    ####
    ## Mandelbrot generator for Python and notepad++
    ## Ron Jensen
    ## Adjust the variables below to change the window
    ####
    x_width = 82
    x_min=-2.
    x_max= 0.5
    
    y_width=32
    y_min =-1.
    y_max = 1.
    ## 
    ## Code starts below
    ##
    x_step=(x_max-x_min)/x_width
    y_step=(y_max-y_min)/y_width
    
    y=y_min
    while (y < y_max):
     x=x_min
     while (x < x_max):
      tc=complex(x,y)
      tcn=tc
      i=0
      ch='*'
      while (i<100):
       tcn=tcn*tcn+tc
       if (abs(tcn) >= 2.0):
        ch=(' ')
        break
       i+=1
      sys.stdout.write(ch)
    
      x += x_step
     sys.stdout.write('\n')
     y += y_step
    
    # Done
    
     
    Last edit: Ron Jensen 2014-01-28
  • Ron Jensen
    Ron Jensen
    2014-01-28

    ooops

     
    Last edit: Ron Jensen 2014-01-28
  • Ron Jensen
    Ron Jensen
    2014-01-28

    This is sort of what it prints out. Make sure you're at a blank document before you execute it:

                                                                  *                   
                                                               *****                  
                                                              *******                 
                                                               ******                 
                                                          * *********** *             
                                                    *** ********************  ***     
                                                     ***************************      
                                                 * *****************************      
                                                *********************************     
                                                ***********************************   
                                ** ****       ************************************    
                                ***********   *************************************   
                              *************** ************************************    
                             **************** ***********************************     
     *************************************************************************        
                             **************** ***********************************     
                              *************** ************************************    
                                ***********   *************************************   
                                ** ****       ************************************    
                                                ***********************************   
                                                *********************************     
                                                 * *****************************      
                                                     ***************************      
                                                    *** ********************  ***     
                                                          * *********** *             
                                                               ******                 
                                                              *******                 
                                                               *****                  
                                                                  *
    
     
  • Ron Jensen
    Ron Jensen
    2014-05-19

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    #!/usr/bin/python
    ####
    ## Mandelbrot generator for Python and notepad++
    ## Ron Jensen
    ## Adjust the variables below to change the window
    ####
    x_width = 80
    x_min=-2.
    x_max= 0.5
    
    y_width=32
    y_min =-1.
    y_max = 1.
    ## 
    ## Code starts below
    ##
    x_step=(x_max-x_min)/x_width
    y_step=(y_max-y_min)/y_width
    
    y=y_min
    while (y <= y_max):
     x=x_min
     line_out = ""
     while (x < x_max):
      tc=complex(x,y)
      tcn=tc
      i=0
      ch='*'
      while (i<100):
       tcn=tcn*tcn+tc
       if (abs(tcn) >= 2.0):
        ch=(' ')
        break
       i+=1
      line_out += ch
      x += x_step
     print line_out
     y += y_step
    
    # Done
    

    This is an optimized version of the other code. Instead of writing out single characters with sys.stdout.write() I concatenate them into a line, then print the each line. Writing character at a time is extremely slow...