Mandelbrot set

You are encouraged to solve this task according to the task description, using any language you may know.


Task

Generate and draw the Mandelbrot set.



Note that there are many algorithms to draw Mandelbrot set and there are many functions which generate it .




put 0 into oReal # Real origin
put 0 into oImag # Imaginary origin
put 0.5 into mag # Magnification
 
put oReal - .8 / mag into leftReal
put oImag + .5 / mag into topImag
put 1 / 200 / mag into inc
 
put [
	(0,255,255),	# aqua	
	(0,0,255), # blue 
	(255,0,255), # fuchsia
	(128,128,128), # gray	
	(0,128,0), # green
	(0,255,0), # lime	
	(128,0,0), # maroon
	(0,0,128), # navy	
	(128,128,0), # olive
	(128,0,128), # purple
	(255,0,0), # red
	(192,192,192), # silver
	(0,128,128), # teal
	(255,255,255), # white
	(255,255,0) #	yellow
] into colors
 
put "mandelbrot.ppm" into myFile
 
open file myFile for writing
write "P3" & return to file myFile # PPM file magic number
write "320 200" & return to file myFile # Width and height
write "255" & return to file myFile # Max value in color channels
 
put topImag into cImag
repeat with each item in 1 .. 200
	put leftReal into cReal
	repeat with each item in 1 .. 320
		put 0 into zReal
		put 0 into zImag
		put 0 into count
		put 0 into size
		repeat at least once until size > 2 or count = 100
			put zReal squared + zImag squared * -1 into newZreal
			put zReal * zImag + zReal * zImag into newZimag
			put newZreal + cReal into zReal
			put newZimag + cImag into zImag
			put sqrt(zReal squared + zImag squared) into size
			add 1 to count
		end repeat
		if size > 2 then # Outside the set - colorize
			put item count mod 15 + 1 of colors into color
			write color joined by " " to file myFile
			write return to file myFile
		else # Inside the set - black
			write "0 0 0" & return to file myFile
		end if
		add inc to cReal
	end repeat
	subtract inc from cImag
end repeat
 
close file myFile
 