--OOP ASCII graphing calculator

--too many equations may cause it to timeout (run on your own system for better performance)

--@boxrum on most platforms

local data = {
  chars = {
    [1] = "██",
    [.75] = "▒▒",
    [.5] = " ▒",
    [.25] = "░░",
    [.125] = " ░",
    [0] = "  "
  },
  blur = 1
}
data.__index = data

local function calculate(equation,x,y,dimensions,zoom,precision2)--this part may be a bit hacky
  
  local splitPoint = string.find(equation,"=")
  local equality,eq2 = string.find(equation,">") or string.find(equation,"<") or string.find(equation,">=") or string.find(equation,"<=")
  
  --local precision = ((dimensions.x^2+dimensions.y^2)^(1/2)/((zoom.x^2+zoom.y^2)^(1/2)))/100*(precision2^2)
  -- print(precision)
  
  local precision = precision2*((((zoom.x*2)^2+(zoom.y*2)^2)^(1/2))/25)
  
  local split = {
    a = string.sub(equation,1,(equality or splitPoint)-1),
    b = string.sub(equation,((eq2 or equality) or splitPoint)+1,#equation)
  }
  
  local mid = equality and string.sub(equation,equality,eq2 or equality) or nil
  
  local mag = 1--math.sqrt(x^2+y^2)
  
  local code = ""
  
  if equality then
    code = string.format("local x=%s local y=%s local p = %s return math.floor((%s)*p)/(p)%smath.floor((%s)*p)/p",tostring(x),tostring(y),tostring(precision),split.a,tostring(mid),split.b)
  else
    code = string.format("local x=%s local y=%s local d=%s local p = %s return (%s)<(%s)+(p*d)/2 and (%s)>(%s)-(p*d)/2 and (%s)<(%s)+(p*d)/2 and (%s)>(%s)-(p*d)/2",tostring(x),tostring(y),tostring(mag),tostring(precision),split.a,split.b,split.a,split.b,split.b,split.a,split.b,split.a)
    -- code = string.format("local x=%s local y=%s local p = %s local x = x local y = y local A = %s local B = %s local x = x local y = y+p/2 local A1 = %s local B1 = %s return (A)<=(B) and (A1)<=(B1)",tostring(x),tostring(y),tostring(precision),split.a,split.b,split.b,split.a)
  end
  --print(code)
  
  return load(code)()
end

function data.NewGraph(dimensions,zoom)
  
  local self = setmetatable({},data)
  
  self._meta = true
  
  self._zoom = {x = zoom[1], y = zoom[2]}
  self._dimensions = {x = dimensions[1],y = dimensions[2]}
  self._txt = ""
  
  self._equations = {}
  
  return self
end

function data:ChangeFill(text)
  
  if not self._meta then
    return
  end
  
  self.chars.fill = text
end
function data:ChangeEmpty(text)
  
  if not self._meta then
    return
  end
  
  self.chars.empty = text
end

function data:AddEquation(equation)
  
  if not self._meta then
    return
  end
  
  table.insert(self._equations,equation)
  
  self:Refresh()
end

function data:AddBlock(fill)
  
  if not self._meta then
    return
  end
  
  self._txt = self._txt..self.chars[fill]
end

function data:Refresh()
  
  if not self._meta then
    return
  end
  
  self._txt = ""
  
  for y1 = 1,self._dimensions.y do
    
    local y = (math.floor(self._dimensions.y/2+.5) - y1)/(self._dimensions.y/2)*(self._zoom.y*2)
    
    for x1 = 1,self._dimensions.x do
      
      local x = (x1-math.floor(self._dimensions.x/2+.5))/(self._dimensions.x/2)*(self._zoom.x*2)
      
      local found = 0
      
      if x1==math.floor(self._dimensions.x/2+.5) or y1==math.floor(self._dimensions.y/2+.5) then
        
          self:AddBlock(1)
        else
          for equationIndex = 1,#self._equations do
        
            local equation = self._equations[equationIndex]
            
            if calculate(equation,x,y,self._dimensions,self._zoom,.8) then
              
              found = 1
              break
            end
            
            if calculate(equation,x,y,self._dimensions,self._zoom,1) then
              
              found = .75
              break
            end
            
            if calculate(equation,x,y,self._dimensions,self._zoom,1.2) then
              
              found = .5
              break
            end
            
            if calculate(equation,x,y,self._dimensions,self._zoom,1.4) then
              
              found = .25
              break
            end
            
            if calculate(equation,x,y,self._dimensions,self._zoom,1.6) then
              
              found = .125
              break
            end
          end
          
          self:AddBlock(found)
      end
    end
    
    self._txt = self._txt.."\n"
  end
end

function data:Print()
  
  if not self._meta then
    return
  end
  
  print(self._txt)
end

local graph = data.NewGraph({100,100},{5,5})

--graph:AddEquation("x=1")
-- graph:AddEquation("y=x^2")
--graph:AddEquation("y=x")
-- graph:AddEquation("y=math.cos(2*x)")
-- graph:AddEquation("y=math.tan(x)")
-- graph:AddEquation("2*y=x/2")
-- graph:AddEquation("y=math.sqrt(1-x^2)")
graph:AddEquation("x^2+y^2=5")
--graph:Refresh()

graph:Print() 
by

Lua online compiler

Write, Run & Share Lua code online using OneCompiler's Lua online compiler for free. It's one of the robust, feature-rich online compilers for Lua language, running the latest Lua version 5.4. Getting started with the OneCompiler's Lua editor is easy and fast. The editor shows sample boilerplate code when you choose language as Lua and start coding.

Taking inputs (stdin)

OneCompiler's Lua online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Lua program which takes name as input and prints hello message with your name.

name = io.read("*a")
print ("Hello ", name)

About Lua

Lua is a light weight embeddable scripting language which is built on top of C. It is used in almost all kind of applications like games, web applications, mobile applications, image processing etc. It's a very powerful, fast, easy to learn, open-source scripting language.

Syntax help

Variables

  • By default all the variables declared are global variables
  • If the variables are explicitly mentioned as local then they are local variables.
  • Lua is a dynamically typed language and hence only the values will have types not the variables.

Examples

-- global variables
a = 10

-- local variables

local x = 30
Value TypeDescription
numberRepresents numbers
stringRepresents text
nilDifferentiates values whether it has data or not
booleanValue can be either true or false
functionRepresents a sub-routine
userdataRepresents arbitary C data
threadRepresents independent threads of execution.
tableCan hold any value except nil

Loops

1. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition)
do
--code
end

2. Repeat-Until:

Repeat-Until is also used to iterate a set of statements based on a condition. It is very similar to Do-While, it is mostly used when you need to execute the statements atleast once.

repeat
   --code
until( condition )

3. For:

For loop is used to iterate a set of statements based on a condition.

for init,max/min value, increment
do
   --code
end

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increase re-usuability and modularity.

optional_function_scope function function_name( argument1, argument2, argument3........, argumentn)
--code
return params with comma seperated
end