This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
FunshineX's profile picture

Mandelbrot in CC1.45

Started by FunshineX, 16 October 2012 - 07:46 PM
FunshineX #1
Posted 16 October 2012 - 09:46 PM
This is a teaser. I'll post full code and a video soon!



[media]http://youtu.be/AkCCwoPPlds[/media]

Code - note the zooming doesn't quite work right.

Spoiler
monitor = peripheral.wrap("top")

function clearMon()
  monitor.setBackgroundColor(colors.black)
  monitor.clear()
end

toMonitor = false

w,h = monitor.getSize()
if not toMonitor then
w,h = term.getSize()
end

originX, originY, zoom = w/2,h/2,1

function debugLog(message)
  local file = fs.open("debug.log","a")
  file.writeLine(message)
  file.close()
end

function scale(x,y)
  x0 = (3.5 * (x-1+(originX-w/2)) / w) - 2.5
  y0 = (2 * (y-1+(originY-h/2)) / h) - 1
  --debugLog(x..","..y.."="..x0..","..y0)
  return x0/zoom,y0/zoom
end

function getColor(iter)
  if iter == 1000 then
	return colors.black
  end

  return bit.blshift(1,(math.floor(iter/2))%15+1)  
end

debugLog("w: "..w.." h: "..h)
function drawMandelbrot()
for j = 1,h do
  for i = 1,w do

	x0,y0 = scale(i,j,zoom)
	
	x = 0
	y = 0
	
	iter = 0
	maxIter = 1000
	
	while x*x + y*y < 2*2 and iter < maxIter do
	
	  xTemp = x*x - y*y + x0
	  y = 2*x*y + y0
	  x = xTemp
	  
	  iter = iter + 1

	end
	
	bgColor = getColor(iter)
--	print(iter)

	if toMonitor then	
	  monitor.setBackgroundColor(bgColor)
	  monitor.setCursorPos(i,j)
	  monitor.write(" ")  
	else
	  term.setBackgroundColor(bgColor)
	  term.setCursorPos(i,j)
	  term.write(" ")
	end

  end
  sleep(.1)
end
end

clearMon()
drawMandelbrot()

while true do
  event, button, mx, my = os.pullEvent("mouse_click")

  if button == 1 then
	zoom = zoom + 1
  elseif button == 2 then
	zoom = zoom - 1
  end

  originX, originY = mx/zoom,my/zoom

  if zoom < 1 then zoom = 1 end

  clearMon()
  drawMandelbrot()
end

Orwell #2
Posted 16 October 2012 - 10:28 PM
Nice. : ) Makes me nostalgic, I had to code this for a physics course once. :D/>/> Would've never thought of implementing it in CC though >.<. Nicely done. : )
FunshineX #3
Posted 16 October 2012 - 10:54 PM
I have to yield at the end of each row or CC complains the operation is too long. If you zoom in a few times it starts getting really slow to render.
Noodle #4
Posted 16 October 2012 - 11:53 PM
Cool, but this might lag possibly?
FunshineX #5
Posted 16 October 2012 - 11:55 PM
Won't lag the world, but the program does take a few seconds to run even at very small zoom factors.
Cloudy #6
Posted 18 October 2012 - 12:05 AM
I have to yield at the end of each row or CC complains the operation is too long. If you zoom in a few times it starts getting really slow to render.

You can do a quick yield without waiting 0.05 using sleep though - do this instead:

os.queueEvent("blah") os.pullEvent("blah")

It is what alongtimeago does.
Cranium #7
Posted 18 October 2012 - 10:52 PM
You sir, deserve a +1.
dan200 #8
Posted 19 October 2012 - 09:08 AM
It is what alongtimeago does.

No it doesn't :s
Cloudy #9
Posted 19 October 2012 - 10:49 AM
It is what alongtimeago does.

No it doesn't :s

I stand corrected. Maybe I should have checked the code first instead of relying on my crappy memory.
sjele #10
Posted 20 October 2012 - 10:11 PM
Nice