ForeverDev

Untitled

Oct 28th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.23 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 = 10,
  13.     equations = 0,
  14. }
  15.  
  16. local plots = {}
  17. local stopGraph = false
  18.  
  19. local colors = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {1, 1, 0}, {1, 0, 1}, {0, 1, 1}}
  20. for i, v in ipairs(colors) do
  21.     colors[i] = Color3.new(v[1], v[2], v[3])
  22. end
  23.  
  24. local wipe = function()
  25.     for _, v in ipairs(plots) do
  26.         v:Destroy()
  27.     end
  28.     plots = {}
  29.     for _, v in ipairs(graph.Box:GetChildren()) do
  30.         if v.Name ~= "Display" then
  31.             v:Destroy()
  32.         end
  33.     end
  34.     grid.equations = 0
  35.     coroutine.wrap(function()
  36.         stopGraph = true
  37.         wait(0.2)
  38.         stopGraph = false
  39.     end)()
  40. end
  41.  
  42. local attr = function(objects, properties)
  43.     if type(objects) ~= "table" then
  44.         objects = {objects}
  45.     end
  46.     for _, this in pairs(objects) do
  47.         for key, value in pairs(properties) do
  48.             this[key] = value
  49.         end
  50.     end
  51. end
  52.  
  53. local offset = function(x, y)
  54.     return Vector2.new((axis.size / 2) + x, (axis.size / 2) - y)
  55. end
  56.  
  57. local appendHeader = function(expression)
  58.     local colorHeader = Instance.new("Frame", graph.Box)
  59.     local equationHeader = Instance.new("TextLabel", graph.Box)
  60.     attr(colorHeader, {
  61.         Size = UDim2.new(0, 15, 0, 15),
  62.         Position = UDim2.new(0, 5, 0, (20 * grid.equations) + 5),
  63.         BackgroundColor3 = colors[grid.equations + 1] or Color3.new(0, 0, 0),
  64.         ZIndex = 3
  65.     })
  66.     attr(equationHeader, {
  67.         Size = UDim2.new(1, -30, 0, 15),
  68.         Position = UDim2.new(0, 25, 0, (20 * grid.equations) + 5),
  69.         Text = expression,
  70.         TextXAlignment = Enum.TextXAlignment.Left,
  71.         BackgroundTransparency = 1,
  72.         BorderSizePixel = 0,
  73.         ZIndex = 3
  74.     })
  75.     grid.equations = grid.equations + 1
  76.     return colorHeader
  77. end
  78.  
  79. local func = function(expression)
  80.     local ticks = 0
  81.     local disp = expression
  82.     local mathPatterns = {"cos", "tan", "sin", "pow", "sqrt", "abs", "pi"}
  83.     expression = expression:gsub("%s+", ""):gsub("e", "2.718")
  84.     for _, v in pairs(mathPatterns) do
  85.         expression = expression:gsub("(" .. v .. ")", "math.%1")
  86.     end
  87.     expression = expression:gsub("(%d)x", "%1*x"):gsub("(%d)%(", "%1*("):gsub("and", " and "):gsub("or", " or ")
  88.     if not pcall(function() return 1 + tonumber(loadstring("return " .. expression:gsub("x", 1))()) end) then
  89.         print("Error graphing function '" .. expression .. "'")
  90.         return
  91.     end
  92.     local color = appendHeader(disp).BackgroundColor3
  93.     coroutine.wrap(function()
  94.         for i = -grid.size / 2, grid.size / 2, 0.002 do
  95.             ticks = ticks + 1
  96.             if ticks % 100 == 0 then
  97.                 wait()
  98.             end
  99.             local x, y = i, loadstring("return " .. expression:gsub("x", i))()
  100.             local point = Instance.new("TextLabel", graph)
  101.             attr(point, {
  102.                 Text = "",
  103.                 Size = UDim2.new(0, 2, 0, 2),
  104.                 Position = UDim2.new(0, offset(x * (axis.size / grid.size), y).X - 1, 0, offset(x, y * (axis.size / grid.size)).Y),
  105.                 BorderSizePixel = 0,
  106.                 BackgroundColor3 = color
  107.             })
  108.             table.insert(plots, point)
  109.             if stopGraph then
  110.                 point:Destroy()
  111.                 break
  112.             end
  113.         end
  114.     end)()
  115. end
  116.  
  117. attr({axis.x, axis.y}, {
  118.     Text = "",
  119.     BorderSizePixel = 0,
  120.     BackgroundColor3 = Color3.new(0, 0, 0)
  121. })
  122.  
  123. attr(axis.x, {
  124.     Size = UDim2.new(1, 0, 0, 1),
  125.     Position = UDim2.new(0, 0, 0.5, 0) 
  126. })
  127.  
  128. attr(axis.y, {
  129.     Size = UDim2.new(0, 1, 1, 0),
  130.     Position = UDim2.new(0.5, 0, 0, 0)
  131. })
  132.  
  133. local getFunc = Instance.new("TextBox", graph.Box)
  134. local submit = Instance.new("TextButton", graph.Box)
  135. local clear = Instance.new("TextButton", graph.Box)
  136. attr(clear, {
  137.     Size = UDim2.new(1, -10, 0, 20),
  138.     Position = UDim2.new(0, 5, 1, -165),
  139.     BackgroundColor3 = Color3.new(0.8, 0.8, 0.8),
  140.     Text = "Clear all graphs",
  141.     TextColor3 = Color3.new(0, 0, 0),
  142.     Name = "Display",
  143.     ZIndex = 4
  144. })
  145.  
  146. attr(getFunc, {
  147.     Size = UDim2.new(1, -10, 0, 20),
  148.     Position = UDim2.new(0, 5, 1, -120),
  149.     BackgroundColor3 = Color3.new(0.8, 0.8, 0.8),
  150.     Text = "Enter function here",
  151.     TextColor3 = Color3.new(0, 0, 0),
  152.     Name = "Display",
  153.     ZIndex = 4
  154. })
  155.  
  156. attr(submit, {
  157.     Size = UDim2.new(1, -10, 0, 20),
  158.     Position = UDim2.new(0, 5, 1, -95),
  159.     BackgroundColor3 = Color3.new(0.8, 0.8, 0.8),
  160.     Text = "Graph function!",
  161.     TextColor3 = Color3.new(0, 0, 0),
  162.     Name = "Display",
  163.     ZIndex = 4
  164. })
  165.  
  166. local nx, x, ny, y = Instance.new("TextLabel", graph), Instance.new("TextLabel", graph), Instance.new("TextLabel", graph) ,Instance.new("TextLabel", graph)
  167.  
  168. attr({nx, x, ny, y}, {
  169.     Size = UDim2.new(0, 100, 0, 30),
  170.     BorderSizePixel = 0,
  171.     BackgroundTransparency = 1,
  172.     TextColor3 = Color3.new(0, 0, 0),
  173.     FontSize = Enum.FontSize.Size10,
  174.     ZIndex = 4
  175. })
  176.  
  177. attr(nx, {
  178.     Position = UDim2.new(0, 10, 0.5, -30),
  179.     Text = "-" .. grid.size / 2,
  180.     TextXAlignment = Enum.TextXAlignment.Left,
  181. })
  182.  
  183. attr(x, {
  184.     Position = UDim2.new(1, -110, 0.5, -30),
  185.     Text = grid.size / 2    ,
  186.     TextXAlignment = Enum.TextXAlignment.Right
  187. })
  188.  
  189. attr(ny, {
  190.     Position = UDim2.new(0.5, 10, 1, -35),
  191.     Text = "-" .. grid.size / 2,
  192.     TextXAlignment = Enum.TextXAlignment.Left  
  193. })
  194.  
  195. attr(y, {
  196.     Position = UDim2.new(0.5, 10, 0, 5),
  197.     Text = grid.size / 2,
  198.     TextXAlignment = Enum.TextXAlignment.Left  
  199. })
  200.  
  201. submit.MouseButton1Click:connect(function()
  202.     local expression = getFunc.Text
  203.     getFunc.Text = ""
  204.     func(expression)
  205. end)
  206.  
  207. clear.MouseButton1Click:connect(wipe)
Advertisement
Add Comment
Please, Sign In to add comment