asdsa

Ruby code posted by asda
created at 13 Oct 09:31

Edit | Back
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
n=1000 #vertical and horizontal max
xx=[] #array for real values of complex
n.times{ |i|
 xx[i]=(i-500.0)/(n/5).to_f
}
yy=xx # array for imag values of complex

max_intens=255
c=Complex(-0.35,0.6)   #constant

File.open("img.pgm", 'w') {|f| 
f.write("P2 \n# img.pgm\n#{n} #{n}\n{max_intens}\n") #image parameters
  #nested loops for calculating julia set
  n.times { |i|           
     n.times { |j|
      z=Complex(xx[i],yy[j]) #current value of z
      iter=0 #iterator & intensity of colour that would be written
      while( z.abs < 2 && iter<max_intens ) 
       z=z*z+c 
       iter+=1
      end
      f.write("#{max_intens-iter} ") #write to file (0 - 255)
     }
     f.write("\n")
    }
}
695 Bytes in 3 ms with coderay