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.
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") end
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") chart.set_title(title) 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 b=ymax-j*step for i in 0..nx-1 a=i*step+xmin x=0 y=0 n=0 # while x*x+y*y<4 and n<=nmax while x*x+y*y<4 && n<=nmax x1=x*x-y*y+a y=2*x*y+b x=x1 n=n+1 end # 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); scatter.set_values([ScatterValue.new(a,b)]) chart.add_element( scatter ) # if you want to store the result r.push([a,b,n,c]) end #i end #j x_axis = XAxis.new x_axis.set_range(xmin,-xmin) 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 end
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.
Use the following link to trackback from your own site:
Thx Pixgeeks for reminding me of this nice Google logo involving fractals. It was in memoriam to Gaston Julia’s Birthday. In case I need to precise, the fractals you see on the logo are called Julia sets because the French mathematician Gast...