Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- game.StarterGui:SetCoreGuiEnabled("All", false)
- local graph = script.Parent:WaitForChild("Graph")
- local axis = {
- x = Instance.new("TextLabel", graph),
- y = Instance.new("TextLabel", graph),
- size = 456
- }
- local grid = {
- size = 20,
- equations = 0,
- }
- local colors = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {1, 1, 0}, {1, 0, 1}, {0, 1, 1}}
- for i, v in ipairs(colors) do
- colors[i] = Color3.new(v[1], v[2], v[3])
- end
- local attr = function(objects, properties)
- if type(objects) ~= "table" then
- objects = {objects}
- end
- for _, this in pairs(objects) do
- for key, value in pairs(properties) do
- this[key] = value
- end
- end
- end
- local offset = function(x, y)
- return Vector2.new((axis.size / 2) + x, (axis.size / 2) - y)
- end
- local appendHeader = function(expression)
- local colorHeader = Instance.new("Frame", graph.Box)
- local equationHeader = Instance.new("TextLabel", graph.Box)
- attr(colorHeader, {
- Size = UDim2.new(0, 15, 0, 15),
- Position = UDim2.new(0, 5, 0, (20 * grid.equations) + 5),
- BackgroundColor3 = colors[grid.equations + 1] or Color3.new(0, 0, 0),
- ZIndex = 3
- })
- attr(equationHeader, {
- Size = UDim2.new(1, -30, 0, 15),
- Position = UDim2.new(0, 25, 0, (20 * grid.equations) + 5),
- Text = expression,
- TextXAlignment = Enum.TextXAlignment.Left,
- BackgroundTransparency = 1,
- BorderSizePixel = 0,
- ZIndex = 3
- })
- grid.equations = grid.equations + 1
- return colorHeader
- end
- local func = function(expression)
- local ticks = 0
- local disp = expression
- local mathPatterns = {"cos", "tan", "sin", "pow", "sqrt", "abs"}
- for _, v in pairs(mathPatterns) do
- expression = expression:gsub("(" .. v .. ")", "math.%1")
- end
- if not pcall(function() return 1 + tonumber(loadstring("return " .. expression:gsub("x", 0))()) end) then
- print("Error graphing function '" .. expression .. "'")
- return
- end
- local color = appendHeader(disp).BackgroundColor3
- coroutine.wrap(function()
- for i = -grid.size / 2, grid.size / 2, 0.005 do
- ticks = ticks + 1
- if ticks % 100 == 0 then
- wait()
- end
- local x, y = i, loadstring("return " .. expression:gsub("x", i))()
- local point = Instance.new("TextLabel", graph)
- attr(point, {
- Text = "",
- Size = UDim2.new(0, 2, 0, 2),
- Position = UDim2.new(0, offset(x * (axis.size / grid.size), y).X, 0, offset(x, y * (axis.size / grid.size)).Y),
- BorderSizePixel = 0,
- BackgroundColor3 = color
- })
- end
- end)()
- end
- attr({axis.x, axis.y}, {
- Text = "",
- BorderSizePixel = 0
- })
- attr(axis.x, {
- Size = UDim2.new(1, 0, 0, 1),
- Position = UDim2.new(0, 0, 0.5, 0)
- })
- attr(axis.y, {
- Size = UDim2.new(0, 1, 1, 0),
- Position = UDim2.new(0.5, 0, 0, 0)
- })
- local getFunc = Instance.new("TextBox", graph.Box)
- local submit = Instance.new("TextButton", graph.Box)
- attr(getFunc, {
- Size = UDim2.new(1, -10, 0, 20),
- Position = UDim2.new(0, 5, 1, -120),
- BackgroundColor3 = Color3.new(0.8, 0.8, 0.8),
- Text = "Enter function here",
- TextColor3 = Color3.new(0, 0, 0),
- ZIndex = 4
- })
- attr(submit, {
- Size = UDim2.new(1, -10, 0, 20),
- Position = UDim2.new(0, 5, 1, -95),
- BackgroundColor3 = Color3.new(0.8, 0.8, 0.8),
- Text = "Graph function!",
- TextColor3 = Color3.new(0, 0, 0),
- ZIndex = 4
- })
- submit.MouseButton1Click:connect(function()
- local expression = getFunc.Text
- getFunc.Text = ""
- func(expression)
- end)
Advertisement
Add Comment
Please, Sign In to add comment