ForeverDev

graph

Oct 27th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.33 KB | None | 0 0
  1. game.StarterGui:SetCoreGuiEnabled("All", false)
  2.  
  3. local graph = script.Parent:WaitForChild("Graph")
  4.  
  5. local axis = {
  6.     x = Instance.new("TextLabel", graph),
  7.     y = Instance.new("TextLabel", graph),
  8.     size = 456
  9. }
  10.  
  11. local grid = {
  12.     size = 20,
  13.     equations = 0,
  14. }
  15.  
  16. local colors = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {1, 1, 0}, {1, 0, 1}, {0, 1, 1}}
  17. for i, v in ipairs(colors) do
  18.     colors[i] = Color3.new(v[1], v[2], v[3])
  19. end
  20.  
  21. local attr = function(objects, properties)
  22.     if type(objects) ~= "table" then
  23.         objects = {objects}
  24.     end
  25.     for _, this in pairs(objects) do
  26.         for key, value in pairs(properties) do
  27.             this[key] = value
  28.         end
  29.     end
  30. end
  31.  
  32. local offset = function(x, y)
  33.     return Vector2.new((axis.size / 2) + x, (axis.size / 2) - y)
  34. end
  35.  
  36. local appendHeader = function(expression)
  37.     local colorHeader = Instance.new("Frame", graph.Box)
  38.     local equationHeader = Instance.new("TextLabel", graph.Box)
  39.     attr(colorHeader, {
  40.         Size = UDim2.new(0, 15, 0, 15),
  41.         Position = UDim2.new(0, 5, 0, (20 * grid.equations) + 5),
  42.         BackgroundColor3 = colors[grid.equations + 1] or Color3.new(0, 0, 0),
  43.         ZIndex = 3
  44.     })
  45.     attr(equationHeader, {
  46.         Size = UDim2.new(1, -30, 0, 15),
  47.         Position = UDim2.new(0, 25, 0, (20 * grid.equations) + 5),
  48.         Text = expression,
  49.         TextXAlignment = Enum.TextXAlignment.Left,
  50.         BackgroundTransparency = 1,
  51.         BorderSizePixel = 0,
  52.         ZIndex = 3
  53.     })
  54.     grid.equations = grid.equations + 1
  55.     return colorHeader
  56. end
  57.  
  58. local func = function(expression)
  59.     local ticks = 0
  60.     local disp = expression
  61.     local mathPatterns = {"cos", "tan", "sin", "pow", "sqrt", "abs"}
  62.     for _, v in pairs(mathPatterns) do
  63.         expression = expression:gsub("(" .. v .. ")", "math.%1")
  64.     end
  65.     if not pcall(function() return 1 + tonumber(loadstring("return " .. expression:gsub("x", 0))()) end) then
  66.         print("Error graphing function '" .. expression .. "'")
  67.         return
  68.     end
  69.     local color = appendHeader(disp).BackgroundColor3
  70.     coroutine.wrap(function()
  71.         for i = -grid.size / 2, grid.size / 2, 0.005 do
  72.             ticks = ticks + 1
  73.             if ticks % 100 == 0 then
  74.                 wait()
  75.             end
  76.             local x, y = i, loadstring("return " .. expression:gsub("x", i))()
  77.             local point = Instance.new("TextLabel", graph)
  78.             attr(point, {
  79.                 Text = "",
  80.                 Size = UDim2.new(0, 2, 0, 2),
  81.                 Position = UDim2.new(0, offset(x * (axis.size / grid.size), y).X, 0, offset(x, y * (axis.size / grid.size)).Y),
  82.                 BorderSizePixel = 0,
  83.                 BackgroundColor3 = color
  84.             })
  85.         end
  86.     end)()
  87. end
  88.  
  89. attr({axis.x, axis.y}, {
  90.     Text = "",
  91.     BorderSizePixel = 0
  92. })
  93.  
  94. attr(axis.x, {
  95.     Size = UDim2.new(1, 0, 0, 1),
  96.     Position = UDim2.new(0, 0, 0.5, 0) 
  97. })
  98.  
  99. attr(axis.y, {
  100.     Size = UDim2.new(0, 1, 1, 0),
  101.     Position = UDim2.new(0.5, 0, 0, 0)
  102. })
  103.  
  104. local getFunc = Instance.new("TextBox", graph.Box)
  105. local submit = Instance.new("TextButton", graph.Box)
  106. attr(getFunc, {
  107.     Size = UDim2.new(1, -10, 0, 20),
  108.     Position = UDim2.new(0, 5, 1, -120),
  109.     BackgroundColor3 = Color3.new(0.8, 0.8, 0.8),
  110.     Text = "Enter function here",
  111.     TextColor3 = Color3.new(0, 0, 0),
  112.     ZIndex = 4
  113. })
  114.  
  115. attr(submit, {
  116.     Size = UDim2.new(1, -10, 0, 20),
  117.     Position = UDim2.new(0, 5, 1, -95),
  118.     BackgroundColor3 = Color3.new(0.8, 0.8, 0.8),
  119.     Text = "Graph function!",
  120.     TextColor3 = Color3.new(0, 0, 0),
  121.     ZIndex = 4
  122. })
  123. submit.MouseButton1Click:connect(function()
  124.     local expression = getFunc.Text
  125.     getFunc.Text = ""
  126.     func(expression)
  127. end)
Advertisement
Add Comment
Please, Sign In to add comment