Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Original C Code By Curtis Upshall Go Check It Out At https://github.com/curtisupshall/mandelbrot-ascii
- -- Ported To OpenComputers By nonogamer9
- local component = require("component")
- local gpu = component.gpu
- local term = require("term")
- local RES = 60
- local Complex = {}
- Complex.__index = Complex
- function Complex.new(r, i)
- local self = setmetatable({}, Complex)
- self.r = r
- self.i = i
- return self
- end
- function Complex:square()
- return Complex.new(self.r * self.r - self.i * self.i, 2 * self.r * self.i)
- end
- function Complex:add(other)
- return Complex.new(self.r + other.r, self.i + other.i)
- end
- local origin = Complex.new(0.0, 0.0)
- function f_z(z, c)
- return z:square():add(c)
- end
- function distance(z1, z2)
- return math.sqrt((z1.r - z2.r) ^ 2 + (z1.i - z2.i) ^ 2)
- end
- function calculateStability(iteration, z, c, threshold)
- if distance(z, origin) > 2 then
- return 0
- end
- local zn = f_z(z, c)
- if distance(z, zn) < threshold or iteration > 200 then
- return 1 / iteration
- else
- return calculateStability(iteration + 1, zn, c, threshold)
- end
- end
- local function main()
- local c = Complex.new(0, 0)
- local stability
- local scale = 1
- local pScale = 1
- local re = -2.0
- local pRe = -2.0
- local im = 1
- local pIm = 1
- local height = RES * 2 / 3
- local width = RES * 2
- local n = 0
- gpu.setResolution(width, height)
- term.clear()
- while true do
- for j = 0, height - 1 do
- for k = 0, width - 1 do
- c.r = re + k * scale * 3 / (RES * 9 / 4)
- c.i = im - j * scale * 3 / RES
- stability = calculateStability(1, f_z(origin, c), c, 0.1)
- local shade = stability > 0 and "%" or " "
- term.write(shade)
- end
- term.write("\n")
- end
- io.write("Enter a number (negative to quit): ")
- n = tonumber(io.read())
- if n < 0 then
- return
- elseif n == 0 then
- scale = pScale
- re = pRe
- im = pIm
- else
- pScale = scale
- scale = scale / 2
- pIm = im
- pRe = re
- if n >= 7 then
- re = re + scale * (n - 7)
- elseif n >= 4 then
- im = im - scale
- re = re + scale * (n - 4)
- elseif n >= 1 then
- im = im - scale * 2
- re = re + scale * (n - 1)
- end
- end
- term.clear()
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement