Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Complex = {}
- Complex.__index = Complex
- local argAssert = "'%s' must be a %s"
- local floor = math.floor
- local function assertType(value, label, expectedType)
- assert(type(value) == expectedType, argAssert:format(label, expectedType))
- end
- local function assertComplex(complex)
- if type(complex) == "number" then
- complex = Complex.new(complex, 0)
- end
- assertType(complex, "input", "table")
- assert(getmetatable(complex) == Complex, argAssert:format("input", "Complex"))
- return complex
- end
- function Complex.new(real, imaginary)
- local real = real or 0
- assertType(real, "real", "number")
- local imaginary = imaginary or 0
- assertType(imaginary, "imaginary", "number")
- local complex = {}
- complex.Real = real
- complex.Imaginary = imaginary
- return setmetatable(complex, Complex)
- end
- function Complex:__newindex(k,v)
- error("Complex is read-only.", 0)
- end
- function Complex:__tostring()
- local real = self.Real
- local imaginary = self.Imaginary
- if real == 0 then
- return imaginary .. 'i'
- elseif imaginary == 0 then
- return real
- end
- imaginary = imaginary .. 'i'
- if imaginary:sub(1, 1) == '-' then
- imaginary = imaginary:gsub('-', " - ")
- else
- imaginary = " + " .. imaginary
- end
- return real .. imaginary
- end
- function Complex:__add(complex)
- self = assertComplex(self)
- complex = assertComplex(complex)
- return Complex.new(self.Real + complex.Real, self.Imaginary + complex.Imaginary)
- end
- function Complex:__sub(complex)
- self = assertComplex(self)
- complex = assertComplex(complex)
- return Complex.new(self.Real - complex.Real, self.Imaginary - complex.Imaginary)
- end
- function Complex:__mul(complex)
- -- This computes the distributed multiplication of...
- -- [ (a + bi)(c + di) ] = [ ac + (ad)i + (bc)i + (bd)(i^2) ] =
- -- [ ac + (ad + bc)i - bd ] = [ (ac - bd) + (ad + bc)i ]
- self = assertComplex(self)
- complex = assertComplex(complex)
- local a = self.Real
- local b = self.Imaginary
- local c = complex.Real
- local d = complex.Imaginary
- local ac = a * c
- local ad = a * d
- local bc = b * c
- local bd = b * d
- return Complex.new(ac - bd, ad + bc)
- end
- function Complex:__eq(complex)
- self = assertComplex(self)
- complex = assertComplex(complex)
- return self.Real == complex.Real and
- self.Imaginary == complex.Imaginary
- end
- ---------------------------------------------------------------------------------------------------------------------
- local Mandelbrot =
- {
- CharacterSet = " .,=;:%$@&# ";
- Iterations = 20;
- Threshold = 10e20;
- Horizontal =
- {
- Min = -1.5;
- Max = 0.5;
- Scale = 118;
- };
- Vertical =
- {
- Min = -1.1;
- Max = 1.1;
- Scale = 80;
- };
- Zoom = 1;
- }
- function Mandelbrot:Sample(real, imaginary)
- local c = Complex.new(real, imaginary)
- local z = c
- local i = 0
- while i < self.Iterations and z.Real < self.Threshold do
- z = (z * z) + c
- i = i + 1
- end
- local char = 1 + floor((#self.CharacterSet - 1) * (i / self.Iterations))
- return self.CharacterSet:sub(char, char)
- end
- function Mandelbrot:Render()
- local h = self.Horizontal
- local v = self.Vertical
- for imaginary = v.Min, v.Max, (v.Max - v.Min) / v.Scale do
- local row = ""
- for real = h.Min, h.Max, (h.Max - h.Min) / h.Scale do
- local sample = self:Sample(real, imaginary)
- row = row .. sample
- end
- print(row)
- if wait then
- wait()
- end
- end
- end
- Mandelbrot:Render()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement