Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --ComputerCraft MandelBrot renderer
- --by Tomlacko
- --pastebin UnZrcfER
- -----------------------
- --1 optional parameter: external monitor side
- --Zoom by clicking on any spot
- --move by arrow keys, zoom by num+/num-
- --press space to save image as screenshot openable in paint program
- -----------------------
- function drawBar()
- m.setBackgroundColor(colors.black)
- m.setCursorPos(1, h)
- m.clearLine()
- m.write(" < " .. "ZoomFactor: " .. tostring(zoomspeed) .. " > ")
- m.setCursorPos(w-3,h)
- m.write("Exit")
- end
- function Screenshot()
- if isMonitor then
- print("Enter screenshot filename: ")
- else
- m.setCursorPos(1,h)
- m.setBackgroundColor(colors.black)
- m.clearLine()
- m.write("Enter screenshot filename: ")
- end
- name = read()
- if name=="" then
- if not isMonitor then
- drawBar()
- return true
- end
- print("Canceled...")
- return false
- end
- file = fs.open(name, "w")
- for y=1, h-1 do
- for x=1, w do
- if pixels[y][x]<10 then
- file.write(tostring(pixels[y][x]))
- elseif pixels[y][x]==10 then
- file.write("a")
- elseif pixels[y][x]==11 then
- file.write("b")
- elseif pixels[y][x]==12 then
- file.write("c")
- elseif pixels[y][x]==13 then
- file.write("d")
- elseif pixels[y][x]==14 then
- file.write("e")
- elseif pixels[y][x]==15 then
- file.write("f")
- end
- end
- file.write("\n")
- end
- file.close()
- if not isMonitor then
- drawBar()
- return true
- end
- print("Saved successfully...")
- return false
- end
- args = {...}
- isMonitor = false
- if args[1]=="top" or args[1]=="bottom" or args[1]=="left" or args[1]=="right" or args[1]=="front" or args[1]=="back" then
- m = peripheral.wrap(args[1])
- m.setTextScale(0.5)
- isMonitor = true
- elseif args[1]~=nil then
- print("Invalid monitor side argument!")
- return
- else
- m = term
- end
- w, h = m.getSize()
- if m.isColor() then
- gamut = 16
- else
- gamut = 2
- end
- colordiff = 8
- maxiter = 512
- midx = w/2
- midy = h/2
- zoom = math.min(w,h)/3
- offX = -0.5
- offY = 0
- zoomspeed = 4
- interupt = 5
- pixels = {}
- for y=1, h-1 do
- pixels[y] = {}
- for x=1, w do
- pixels[y][x] = "0"
- end
- end
- while true do
- m.setBackgroundColor(colors.black)
- m.clear()
- m.setBackgroundColor(colors.white)
- drawBar()
- skip = false
- for y=1, h-1 do
- for x=1, w do
- xm = x-midx
- ym = y-midy
- x0 = xm/zoom + offX
- y0 = (ym*1.5)/zoom + offY
- a = 0
- b = 0
- rx = 0
- ry = 0
- c = 0
- while (c<maxiter) and (rx*rx+ry*ry <= 4) do
- rx = a*a - b*b + x0
- ry = 2*a*b + y0
- a = rx
- b = ry
- c = c + 1
- end
- if gamut <= 2 then
- cl = (c%2)*32767 + 1
- m.setBackgroundColor(cl)
- pixels[y][x] = (c%2)*15
- else
- if c==1 or c == maxiter then
- cl = 15
- else
- cl = math.floor(c/colordiff+math.log(c)^2)%(gamut-1)
- end
- --cl = math.abs((c-1+gamut-1)%((gamut-1)*2)-(gamut-1))
- --print(math.pow(2,cl))
- m.setBackgroundColor(math.pow(2,cl))
- pixels[y][x] = cl
- end
- m.setCursorPos(x, y)
- m.write(" ")
- end
- if y%interupt==0 then
- os.startTimer(0.05)
- ev, s, xpos, ypos = os.pullEvent()
- if ev == "monitor_touch" or ev == "key" or ev == "mouse_click" or ev == "mouse_scroll" then
- skip = true
- break
- end
- end
- if skip then break end
- end
- while true do
- if skip then
- skip = false
- else
- ev, s, xpos, ypos = os.pullEvent()
- end
- if (not isMonitor and (ev=="mouse_click" or ev=="mouse_scroll")) or (isMonitor and ev=="monitor_touch" and args[1]==s) then
- if ypos==h then
- if xpos>=w-3 then
- m.setBackgroundColor(colors.black)
- if isMonitor then
- m.setTextScale(1)
- end
- m.clear()
- m.setCursorPos(1,1)
- return
- elseif xpos <= 3 then
- zoomspeed = zoomspeed - 0.5
- elseif xpos > 3+12+#tostring(zoomspeed) and xpos <= 3+12+3+#tostring(zoomspeed) then
- zoomspeed = zoomspeed + 0.5
- end
- drawBar()
- else
- xpos = xpos - midx
- ypos = ypos - midy
- offX = offX + xpos/zoom
- offY = offY + (ypos*1.5)/zoom
- if (zoomspeed<0) or (zoomspeed>0 and ((ev=="mouse_click" and s==2) or (ev=="mouse_scroll" and s==1))) then
- zoom = zoom/math.abs(zoomspeed)
- elseif zoomspeed ~= 0 then
- zoom = zoom*math.abs(zoomspeed)
- end
- break
- end
- elseif ev=="key" then
- if s==200 then
- offY = offY - (midy/2)/zoom
- break
- elseif s==208 then
- offY = offY + (midy/2)/zoom
- break
- elseif s==203 then
- offX = offX - (midx/2)/zoom
- break
- elseif s==205 then
- offX = offX + (midx/2)/zoom
- break
- elseif (s==78 and zoomspeed>0) or (s==74 and zoomspeed<0) then
- zoom = zoom*math.abs(zoomspeed)
- break
- elseif (s==78 and zoomspeed<0) or (s==74 and zoomspeed>0) then
- zoom = zoom/math.abs(zoomspeed)
- break
- elseif s==57 then
- sleep(0.1)
- if Screenshot() then
- break --read moves page down
- end
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment