# Grapher

Here is a graphing calculator I made, feel free to modify it.

``````

--# Main
-- graph

-- Use this function to perform your initial setup
function setup()
watch("f")
input = Textbox("sin(x)", WIDTH / 2, HEIGHT - 15)
lineCapMode(SQUARE)
font("CourierNewPSMT")
fontSize(18)
textMode(CORNER)
iparameter("zoom", 1, 100, 10)

-- making some functions easier to type
sin = math.sin
cos = math.cos
tan = math.tan
sqrt = math.sqrt
pi = math.pi
end

-- This function gets called once every frame
function draw()
f = "return " .. input.text
background(255, 255, 255, 255)
stroke(0, 0, 0, 255)
strokeWidth(4)
line(0, HEIGHT / 2, WIDTH, HEIGHT / 2)
line(WIDTH / 2, 0, WIDTH / 2, HEIGHT)
fill(0, 0, 0, 255)
text("-x", 0, HEIGHT / 2)
text("x", WIDTH - 15, HEIGHT / 2)
text("-y", WIDTH / 2 + 5, 0)
text("y" , WIDTH / 2 + 5, HEIGHT - 63)

strokeWidth(2)
for i = 1, zoom * 2 do
local x = WIDTH / 2 / zoom * i
local y = HEIGHT / 2 / zoom * i
line(x, 0, x, HEIGHT)
line(0, y, WIDTH, y)
end
translate(WIDTH / 2, HEIGHT / 2)
fill(255, 0, 0, 255)
pointSize(3)
for i = -zoom, zoom, zoom / (WIDTH / 2) do
x = i
if type(pcall(funct) and funct() or "") == "number" then
point(x * WIDTH / 2 / zoom, funct() * WIDTH / 2 / zoom)
end
end
translate(-WIDTH / 2, -HEIGHT / 2)
input:draw()
end

function touched(touch)
input:touched(touch)
end
--# Textbox
Textbox = class()

function Textbox:init(def, x, y)
self.text = def or ""
self.x = x
self.y = y
self.w = WIDTH
self.h = 30
end

function Textbox:draw()
pushStyle()
textMode(CENTER)
font("Inconsolata")
fill(255, 255, 255, 255)
stroke(63, 63, 63, 255)
strokeWidth(2)
fontSize(self.h / 3 * 2)
rect(self.x, self.y, self.w, self.h)
fill(0, 0, 0, 255)
text(self.text, self.x, self.y)
if isKeyboardShowing() then
self.text = keyboardBuffer()
end
popStyle()
end

function Textbox:touched(touch)
if math.abs(touch.x - self.x) < self.w / 2 and math.abs(touch.y - self.y) < self.h / 2 then
showKeyboard()
end
end
``````