Advertisement
nonogamer9

mandelbrot set for OpenComputers

Jul 19th, 2024 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.59 KB | Software | 0 0
  1. -- Original C Code By Curtis Upshall Go Check It Out At https://github.com/curtisupshall/mandelbrot-ascii
  2. -- Ported To OpenComputers By nonogamer9
  3. local component = require("component")
  4. local gpu = component.gpu
  5. local term = require("term")
  6.  
  7. local RES = 60
  8.  
  9. local Complex = {}
  10. Complex.__index = Complex
  11.  
  12. function Complex.new(r, i)
  13.     local self = setmetatable({}, Complex)
  14.     self.r = r
  15.     self.i = i
  16.     return self
  17. end
  18.  
  19. function Complex:square()
  20.     return Complex.new(self.r * self.r - self.i * self.i, 2 * self.r * self.i)
  21. end
  22.  
  23. function Complex:add(other)
  24.     return Complex.new(self.r + other.r, self.i + other.i)
  25. end
  26.  
  27. local origin = Complex.new(0.0, 0.0)
  28.  
  29. function f_z(z, c)
  30.     return z:square():add(c)
  31. end
  32.  
  33. function distance(z1, z2)
  34.     return math.sqrt((z1.r - z2.r) ^ 2 + (z1.i - z2.i) ^ 2)
  35. end
  36.  
  37. function calculateStability(iteration, z, c, threshold)
  38.     if distance(z, origin) > 2 then
  39.         return 0
  40.     end
  41.     local zn = f_z(z, c)
  42.     if distance(z, zn) < threshold or iteration > 200 then
  43.         return 1 / iteration
  44.     else
  45.         return calculateStability(iteration + 1, zn, c, threshold)
  46.     end
  47. end
  48.  
  49. local function main()
  50.     local c = Complex.new(0, 0)
  51.     local stability
  52.     local scale = 1
  53.     local pScale = 1
  54.     local re = -2.0
  55.     local pRe = -2.0
  56.     local im = 1
  57.     local pIm = 1
  58.     local height = RES * 2 / 3
  59.     local width = RES * 2
  60.     local n = 0
  61.  
  62.     gpu.setResolution(width, height)
  63.     term.clear()
  64.  
  65.     while true do
  66.         for j = 0, height - 1 do
  67.             for k = 0, width - 1 do
  68.                 c.r = re + k * scale * 3 / (RES * 9 / 4)
  69.                 c.i = im - j * scale * 3 / RES
  70.                 stability = calculateStability(1, f_z(origin, c), c, 0.1)
  71.                 local shade = stability > 0 and "%" or " "
  72.                 term.write(shade)
  73.             end
  74.             term.write("\n")
  75.         end
  76.         io.write("Enter a number (negative to quit): ")
  77.         n = tonumber(io.read())
  78.  
  79.         if n < 0 then
  80.             return
  81.         elseif n == 0 then
  82.             scale = pScale
  83.             re = pRe
  84.             im = pIm
  85.         else
  86.             pScale = scale
  87.             scale = scale / 2
  88.             pIm = im
  89.             pRe = re
  90.             if n >= 7 then
  91.                 re = re + scale * (n - 7)
  92.             elseif n >= 4 then
  93.                 im = im - scale
  94.                 re = re + scale * (n - 4)
  95.             elseif n >= 1 then
  96.                 im = im - scale * 2
  97.                 re = re + scale * (n - 1)
  98.             end
  99.         end
  100.         term.clear()
  101.     end
  102. end
  103.  
  104. main()
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement