test_mandelbrot.lua: Generate mandelbrot set demo

Wed, 27 Oct 2010 14:11:35 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 27 Oct 2010 14:11:35 +0100
changeset 4
caf89bc9100a
parent 3
7f619444bc2a
child 5
fa4d448b07d3

test_mandelbrot.lua: Generate mandelbrot set demo

test_mandelbrot.lua file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test_mandelbrot.lua	Wed Oct 27 14:11:35 2010 +0100
@@ -0,0 +1,26 @@
+-- Code originally by Luiz Henrique de Figueiredo
+-- At http://lua-users.org/lists/lua-l/2010-10/msg00754.html
+-- Ported to lua-xpm by Matthew Wild
+
+local xpm = require "xpm";
+
+local N=255
+
+local pic = xpm.new(N,N);
+
+for i=1,N do
+	local y=2-4*(i-1)/(N-1)
+	for j=1,N do
+		local x=-2+4*(j-1)/(N-1)
+		local xc,yc=x,y
+		local X,Y=0,0
+		local p=0
+		for n=1,255 do
+			X,Y = X^2-Y^2+xc, 2*X*Y+yc
+			if X^2+Y^2>4 then p=n break end
+		end
+		pic:setpixel(j, i, ("#%02x%02x%02x"):format(p,p,p));
+	end
+end	
+
+pic:render(io.stdout);

mercurial