# Dans cette version du programme, nous n'utilisons pas la # classe Complex de Ruby. # De ce fait, nous perdons en lisibilité mais gagnons en vitesse # d'exécution require 'sdl' Taille=200 $amin=-1.25 $amax=1.25 $bmin=-1.25 $bmax=1.25 def convert(x,y) return [x*($amax-$amin)/Taille+$amin, y*($bmax-$bmin)/Taille+$bmin] end def iterations(m,c) u=m.clone for i in 1..100 tmp=u[0] u[0]=u[0]*u[0]-u[1]*u[1]+c[0] u[1]=2*tmp*u[1]+c[1] return i if u[0]*u[0]+u[1]*u[1]>=4 end return -1 end def trace(screen,c) for i in 0...Taille for j in 0...Taille m=convert(i,j) n=iterations(m,c) if n==-1: screen[i,j]=[0,0,0] else screen[i,j]=[(4*n)%256,2*n,(6*n)%256] end end screen.flip end end def zoom(c0,f) l=($amax-$amin)*f h=($bmax-$bmin)*f $amin=c0[0]-l/2.0 $bmin=c0[1]-h/2.0 $amax=$amin+l $bmax=$bmin+h end SDL.init( SDL::INIT_VIDEO ) screen = SDL::Screen.open(Taille,Taille,16,SDL::SWSURFACE) trace(screen,[-0.5,0.6]) while true event = SDL::Event.poll if event.class==SDL::Event::MouseButtonDown z0=convert(event.x,event.y) f=1 f=0.5 if event.button==SDL::Mouse::BUTTON_LEFT f=2 if event.button==SDL::Mouse::BUTTON_RIGHT zoom(z0,f) trace(screen,[-0.5,0.6]) end end