Harry Seldon's blog

Fractals, Chaos, and Control Systems on Rails


How to draw Mandelbrot's fractal with Rails and Open Flash Chart?

Posted by Harry Seldon on April 18, 2009

Mandelbrot set

Let us draw the Mandelbrot set with OFC. The point is to make a new example of use of OFC and it is also to have some fun with the so interesting fractal theory. To see how fashionable it is, read this post: Good news from the combat against the crisis. This post is highly inspired from this nice French page about the Mandelbrot set. I am using the algorithm given there.

Definition of the Mandelbrot set
For each point of the complex plane we associate the sequence: zn+1=zn2+A with z0=0 and A=a+ib the point affix. Iff the sequence is bounded the point A belongs to the Mandelbrot”s set. Problem is, it is not easy for a computer to say if a sequence remains bounded. It would require an infinite number of calculations. For each point, we are simply going to make a ‘large’ number of calculation on the sequence. It can be shown analytically that if the module of zn is greater than 2 the sequence will diverge. So during nmax iterations, if the sequence goes above 2, the sequence diverges and it has diverged all the quicker as the number of iteration is low. If it is quick to diverge it is far from the set. A color will be associated to each point according to its distance from the set. If the algorithm reaches nmax the probability for the point to belong to the set is maximal with respect to our computation.

To code without complex numbers we will use the real coordinates, the sequence being written equivalently as:
yn+1=2 xn yn+b

Code for Open Flash Chart
The implementation with Rails and OFC is explained hereafter. It is using a scatter chart.

in test_it_controller.rb

  def index_mandelbrot_fractal
    # from http://perso.numericable.fr/~haasjn/haasjn/AlgoMandel.txt 
    @graph = open_flash_chart_object(500,500,"/test_it/graph_code_Mandelbrot_fractal")     

Also in test_it_controller.rb

  def graph_code_Mandelbrot_fractal
    #    Algorithm to draw Mandelbrot's fractal 
    #    variables a,b,x,y,xmin,ymax,cx,cy,width,step:real
    #              i,j,nx,ny,n:int
    #              r: table
    #    cx,cy    coordinates of the image center in the complex plane
    #    xmin     image left limit 
    #    ymax     image upper limit
    #    width    image width in the complex plane
    #    nx       image horizontal resolution 
    #    ny       image vertical resolution
    #    nmax     maximum number of loops to compute the convergence of the complex sequence
    #    step     step between 2 points 
    #    r        table containing the result for each point
    #    in the loop 
    #    i,j  indices of the point 
    #    a,b  point coordinates
    #    x,y  values of the complex sequence
    #    x1   next value of x 
    #    n    indice of the complex sequence
    #    Algorithm beginning 
    #    cx,cy,width,nx,ny,nmax are given at start 
    cx = 0
    cy = 0
    width = 4.to_f
    nx = 100.to_f
    ny = 100.to_f
    nmax = 250.to_f

    xmin = cx-width/2
    ymax = cy+width/2*ny/nx
    step  = width/nx

    # Preparation of the chart
    chart = OpenFlashChart.new
    title = Title.new("Mandelbrot set")
    r = Array.new # results 

    # The loop asks : does the point (a,b) belong to the Mandelbrot set ? 
    # The bigger n, the more probable the point belongs to  the set 
    for j in 0..ny-1
      for i in 0..nx-1
        #  while x*x+y*y<4 and n<=nmax
        while x*x+y*y<4 && n<=nmax

        # Adding the point to the chart
        # Needs to associate a color according to n 
        # that is according to the time needed to converge
        amplif = 1
        c = (16.0.+n/nmax*(255.0-16.0)*amplif).to_int
        c = [255,c].min
        col_gray = (255-c).to_int.to_s(16).to_s
        col =  "#"+col_gray+col_gray+col_gray
        scatter = Scatter.new(col, 2);
        chart.add_element( scatter )

        # if you want to store the result 
      end #i
    end #j

    x_axis = XAxis.new
    chart.x_axis = x_axis

    y_axis = YAxis.new
    y_axis.set_range( -ymax, ymax )
    chart.y_axis = y_axis
    render :text => chart.to_s      

in index_mandelbrot_fractal.html.erb

<script type="text/javascript" src="/javascripts/swfobject.js"></script>
<%= @graph %>

Notice the code for the computation and the graph are made together in the controller code. This is just for simplicity to write this post. Technically, the computation is more a work for your model. Preparing the graph is also a work for the model. With a DRY code you would have one model method to prepare the data, one to prepare the chart. Then in the controller you call the method that prepares the chart and you sends the data to the view.

Here is the Mandelbrot set chart. Beware it takes about 10 seconds to load. Indeed the algorithm is computationally intensive. The code is available in the OFC test app on github. Here is the controller code.

Thanks for having read this world first: drawing the Mandelbrot set with Ruby on Rails and OFC.

Posted in , | 2 comments | Tags , , , , , | atom



Recent Comments

Recent Posts


actuators aircraft atc blog chaos chaos_theory charts control controllers controls crisis economy finance flight fractals git gnc gs guidance linux mandelbrot marketing navigation ns ofc on pilot rails ruby sas scs sensors statistics systems techcrunch thinkosphere tutorial typo ubuntu wifi