-- draw a mandelbrot fractal in a raw file (512x512) -- ugly LUA code by www.astrofra.com ------------------------------ function contrast(color_value) ------------------------------ return ((color_value * color_value) / 256); end ---------------------------- function invert(color_value) ---------------------------- return (255 - color_value); end ------------------------------------------------ function write_rgb_pixel(file_handler, r , g, b) ------------------------------------------------ if (r < 1) then r = 1; end; -- ugly hack because string.char(0) doesn't seem to write anything in the file if (g < 1) then g = 1; end; if (b < 1) then b = 1; end; if (r > 255) then r = 255; end; if (g > 255) then g = 255; end; if (b > 255) then b = 255; end; io.write(string.format("%s%s%s", string.char(r),string.char(g),string.char(b))); end -------------------------------------------------- -- main -------------------------------------------------- print("Fractal test"); file_name = "mandel_out.raw"; file_handler = nil; bmp_size_x = 512; bmp_size_y = 512; origin_x = bmp_size_x / 2; origin_y = bmp_size_y / 2; cx=0; cy=0; scale=0.02; limit=4; print("open file "..file_name.." in write mode"); bmp_handler = io.open(file_name,"wb"); if not(bmp_handler) then print("io.open returned error"); return; end io.output(bmp_handler); for i=0,bmp_size_x-1,1 do -- for j=0,bmp_size_y-1,1 do -- x = (i - origin_x) * 0.5; y = (j - origin_y) * 0.5; -- ax=cx+x*scale; ay=cy+y*scale; a1=ax; b1=ay; lp=0; repeat -- lp=lp+1; a2=a1*a1-b1*b1+ax; b2=2*a1*b1+ay; a1=a2; b1=b2; -- until (lp>255) or ((a1*a1)+(b1*b1)>limit); write_rgb_pixel(bmp_handler,lp*16,lp*8,lp*4); -- end -- end io.close(bmp_handler); print("Done");