# Go About Drawing a Graph?

i wanna be able to draw a graph on a coordinate plane on my screen.

like say i wanna graph

``````
y = 2x^2 + 6

``````

how could i go about doing this?

Edit:

heres the code
[lua]x = {}
x.showgraph = true
x.midx = ScrW()/2
x.midy = ScrH()/2

function x.equation( n )
return 0.001*n^2
end
x.dy = nil

function x.DrawGraph()
if !x.showgraph then return end
surface.SetDrawColor( 255, 255, 255, 255 )
surface.DrawLine( x.midx - 500, x.midy, x.midx + 500, x.midy )
surface.DrawLine( x.midx, x.midy - 500, x.midx, x.midy + 500 )

``````for i = -500, 500 do
x.dy = x.equation(i) - x.equation(i - 1)
surface.SetDrawColor( 0, 255, 0, 255 )
surface.DrawLine( x.midx + i, x.midy - x.equation(i), x.midx + i + 1, x.midy - x.equation(i) + x.dy )
end
``````

end
concommand.Add( “x_test”, function() x.showgraph = !x.showgraph end )[/lua]

Deco wrote a graph thingy somewhen, lemme see…

GraphPanel.lua (lua/vgui/)
[lua]require(“interpolation”)
local PANEL = {}
function PANEL:Init()
self.PointsTable = {[0] = .5, [1/3] = .5, [1/3*2] = .5, [1] = .5}

``````self.StepSize = 0.0025 -- The lower the laggier
self.Bias = 0
self.Tension = 1
self.InterpolationType = interpolation.HERMITE
interpolation.create(self.PointsTable, self.InterpolationType, true, nil, nil, nil, self.Bias, self.Tension)

self.MouseDown = false
self.CursorWithin = false
self.CursorX, self.CursorY = 0, 0
self.DraggedPoint = nil
self.PointOccupied = false

self.DotSize = 4
self.DotColor = Color(80, 80, 255, 255)
self.SelectedDotColor = Color(255, 80, 80, 255)
self.BackgroundColor = Color(76, 255, 139, 255)
self.LineColor = Color(0, 0, 0, 255)
self.ValueDotColor = Color(255, 255, 0, 255)
self.GridColor = Color(150, 150, 150, 255)

self.Grid = true
self.XGrids = 10
self.YGrids = 10
self:SetMouseInputEnabled(true)

self.Tooltip = vgui.Create("DTooltip", self)
self.Tooltip:SetVisible(false)
self.Tooltip.ArrowTall = 8
self.Tooltip.ArrowWide = 16
self.Tooltip.ArrowPosX = 0
self.Tooltip.ArrowPosY = 0
function self.Tooltip:Update(x, y)
self.Contents:SizeToContents()
self:SetWide(self.Contents:GetWide()+8)
self.ArrowTall = 8
self.ArrowWide = 16
self.ArrowPosX = 0
self.ArrowPosY = self:GetTall()
self.StartShowingAt = SysTime()
self:SetPos(x, y-self:GetTall()-16)
end
self.TooltipLabel = vgui.Create("DLabel", self.Tooltip)
self.Tooltip:SetContents(self.TooltipLabel)
``````

end
function PANEL:ShowGrid(grid)
self.Grid = grid
end
function PANEL:SetXGridLines(num)
self.XGrids = num
end
function PANEL:SetYGridLines(num)
self.YGrids = num
end
function PANEL:SetInterpolationType(t)
self.InterpolationType = t
interpolation.set_t_method(self.PointsTable, t)
end
function PANEL:GetBias()
return self.Bias
end
function PANEL:SetBias(bias)
self.Bias = bias
interpolation.set_t_bias(self.PointsTable, bias)
end
function PANEL:GetTension()
return self.Tension
end
function PANEL:SetTension(tension)
self.Tension = tension
interpolation.set_t_tension(self.PointsTable, tension)
end
function PANEL:Paint()
surface.SetDrawColor(self.BackgroundColor)
surface.DrawRect(0, 0, self:GetWide(), self:GetTall())
if self.Grid then
surface.SetDrawColor(self.GridColor)
local each_x, each_y = self:GetWide()/self.XGrids, self:GetTall()/self.YGrids
for gx = 1, self.XGrids do
surface.DrawLine(each_xgx, 0, each_xgx, self:GetTall())
end
for gy = 1, self.YGrids do
surface.DrawLine(0, each_ygy, self:GetWide(), each_ygy)
end
end
surface.SetDrawColor(self.LineColor)
surface.DrawOutlinedRect(0, 0, self:GetWide(), self:GetTall())
local last, last_x, curr, curr_x
for i = 0, 1, self.StepSize do
if i == 0 then
last = self.PointsTable[0]
last_x = 0
else
curr = self.PointsTable*
curr_x = iself:GetWide()
surface.DrawLine(last_x, last
self:GetTall(), curr_x, currself:GetTall())
last = curr
last_x = curr_x
end
end
local x, y, closest, closest_diff, diff
if self.CursorWithin then
for k,v in pairs(self.PointsTable) do
if k ~= “__interpolation_data" then diff = math.abs(k-self.CursorX) if not closest or diff &lt; closest_diff then closest = k closest_diff = diff end end end if not closest then error("WTF2?!?!?!?") end surface.SetDrawColor(self.ValueDotColor) interpolation.LOLDEBUG = true surface.DrawRect(self.CursorX*self:GetWide()-self.DotSize, self.PointsTable[self.CursorX]*self:GetTall()-self.DotSize, self.DotSize*2, self.DotSize*2) interpolation.LOLDEBUG = false end local x, y for k,v in pairs(self.PointsTable) do if k ~= "__interpolation_data” then
x, y = k
self:GetWide(), vself:GetTall()
surface.SetDrawColor(k == closest and self.SelectedDotColor or self.DotColor)
surface.DrawRect(x-self.DotSize, y-self.DotSize, self.DotSize
2, self.DotSize*2)
end
end
end
function PANEL:OnMousePressed(code)
if code == MOUSE_LEFT then
self.MouseDown = true
end
end
function PANEL:OnMouseReleased(code)
if code == MOUSE_LEFT then
self.MouseDown = false
if not self.DraggedPoint then
self.PointsTable[self.CursorX] = self.CursorY
end
self.DraggedPoint = nil
elseif code == MOUSE_RIGHT and not self.DraggedPoint and not self.MouseDown and self.CursorWithin then
local closest, closest_diff, diff
for k,v in pairs(self.PointsTable) do
if k ~= “__interpolation_data" then diff = math.abs(k-self.CursorX) if not closest or diff &lt; closest_diff then closest = k closest_diff = diff end end end if not closest then error("WTF2?!?!?!?") end if closest ~= 0 and closest ~= 1 and table.Count(self.PointsTable) &gt; 4 then self.PointsTable[closest] = nil end end end function PANEL:OnCursorMoved(x, y) self.Tooltip:SetVisible(true) self.CursorWithin = true self.LastCursorX = self.CursorX x = math.Clamp(x, 0, self:GetWide()) y = math.Clamp(y, 0, self:GetTall()) self.CursorX = x/self:GetWide() self.CursorY = y/self:GetTall() local tooltip_pos = self.PointsTable[self.CursorX] self.TooltipLabel:SetText((math.floor(tooltip_pos*100000)/1000).."%") self.Tooltip:Update(x, self.PointsTable[self.CursorX]*self:GetTall()) if self.MouseDown then if self.DraggedPoint then if self.DraggedPoint == 0 or self.DraggedPoint == 1 then self.PointsTable[self.DraggedPoint] = y/self:GetTall() elseif self.DraggingPoint ~= self.CursorX then if not self.PointOccupied then self.PointsTable[self.DraggedPoint] = nil end self.DraggedPoint = self.CursorX self.PointOccupied = rawget(self.PointsTable, self.DraggedPoint) if not self.PointOccupied then self.PointsTable[self.DraggedPoint] = self.CursorY end end else local mousedown_point = x/self:GetWide() local closest, closest_diff, diff for k,v in pairs(self.PointsTable) do if k ~= "__interpolation_data” then
diff = math.abs(k-mousedown_point)
if not closest or diff < closest_diff then
closest = k
closest_diff = diff
end
end
end
if not closest then error(“WTF?!?!?!?”) end
self.DraggedPoint = closest
end
end
end
function PANEL:OnCursorEntered()
self.CursorWithin = true
self.Tooltip:SetVisible(true)
end
function PANEL:OnCursorExited()
self.CursorWithin = false
self.Tooltip:SetVisible(false)
end
derma.DefineControl(“InterpolationGraphPanel”, “”, PANEL, “Panel”)
[/lua]

interpolation.lua (lua/includes/modules/)
[lua]local math_cos = math.cos
local math_pi = math.pi
local math_abs = math.abs
local pairs = pairs
local tonumber = tonumber
local type = type
–local print = print
local error = error
local rawset = rawset
local rawget = rawget
local setmetatable = setmetatable

module(“interpolation”)

LINEAR = 1
COSINE = 2
CUBIC = 3
HERMITE = 4

local function easy_call(func, t, m, o, te, value_converter, key_converter, loop_function)
if func == t_linear or func == t_cosine or func == t_cubic then
return func(t, m, value_converter, key_converter, loop_function)
elseif func == t_hermite then
return func(t, m, value_converter, key_converter, loop_function, o, te)
end
error(“WTF INVALID FUNCTION PASSED TO INTERPOLATION EASY CALL!?!?!?!?!”)
return linear(t, m, value_converter, key_converter, loop_function)
end
local interpolating_table_meta = {
__index = function(t, k)
local k = tonumber(k)
if k then
local data = rawget(t, “__interpolation_data") if data.cache then data.cache[k] = data.cache[k] or easy_call(data.func, t, k, data.bias, data.tension, data.value_c, data.key_c, data.loop) return data.cache[k] else return easy_call(data.func, t, k, data.bias, data.tension, data.value_c, data.key_c, data.loop) end else return rawget(t, k) end end, __newindex = function(t, k, v) rawset(rawget(t, "__interpolation_data”), “cache”, {})
rawset(t, k, v)
end,
}

– Sets the metatable of t so that it automatically calculates the interpolation values.
– If cache is true, it will create a cache table within the table to store previously calculated values. This can be memory expensive.
– The cache will be reset whenever a new value is added, bias or tension or tension is changed or the interpolation method is changed.
– If not true, it will not create a cache, which, depending on the situation, may be CPU expensive.
– f should be the approporiate enumeration value (i.e: interpolation.LINEAR).
– The index '__interpolation_data' is reserved for use and should be ignored. -- t can have a previous metatable, however it will removed and reset to the appropriate interpolation metatable. -- If f is interpolation.HERMITE, the bias and tension arguments must be passed. -- value_converter, key_converter and loop_function can be left nil if neccessary. function create(t, f, cache, value_converter, key_converter, loop_function, bias, tension) return setmetatable(rawset(t, "__interpolation_data", {
cache = cache and {},
func = f == LINEAR and t_linear
or f == COSINE and t_cosine
or f == CUBIC and t_cubic
or f == HERMITE and ((tonumber(bias) and tonumber(tension)) and t_hermite or error(“interpolaton.create(t, interpolation.HERMITE, …) requires numeric tension and bias values.”))
or error(“Invalid interpolation type passed to interpolation.create. Expected interpolation.LINEAR, interpolation.COSINE, interpolation.CUBIC or interpolation.HERMITE”),
bias = tonumber(bias),
tension = tonumber(tension),
value_c = value_converter,
key_c = key_converter,
loop = loop_function,
}), interpolating_table_meta)
end

– Sets the bias of a interpolating table created by interpolation.create.
– Warning: Resets table’s cache (if it uses one).
function set_t_bias(t, bias)
rawset(rawget(t, “__interpolation_data"), "cache", {}) rawset(rawget(t, "__interpolation_data”), “bias”, tonumber(bias) and tonumber(bias) or error(“interpolation.set_t_bias requires a numeric value for argument #2 (bias)”))
end

– Sets the tension of a interpolating table created by interpolation.create.
– Warning: Resets table’s cache (if it uses one).
function set_t_tension(t, tension)
rawset(rawget(t, “__interpolation_data"), "cache", {}) rawset(rawget(t, "__interpolation_data”), “tension”, tonumber(tension) and tonumber(tension) or error(“interpolation.set_t_tension requires a numeric value for argument #2 (tension)”))
end

– Sets the interpolation method of a interpolating table created by interpolation.create.
– Warning: Resets table’s cache (if it uses one).
function set_t_method(t, f)
local data = rawget(t, “\$__interpolation_data”)
rawset(data, “cache”, {})
rawset(data, “func”, f == LINEAR and t_linear
or f == COSINE and t_cosine
or f == CUBIC and t_cubic
or f == HERMITE and t_hermite
or error(“Invalid interpolation type passed to interpolation.set_t_method. Expected interpolation.LINEAR, interpolation.COSINE, interpolation.CUBIC or interpolation.HERMITE”))
if f == HERMITE then
data.bias = tonumber(data.bias) or 0
data.tension = tonumber(data.tension) or 0
end
end

function ease(p, i, o)
i = tonumber(i) or error(“interpolation.ease requires a numeric value for second argument (EaseIn amount).”)
o = tonumber(o) or error(“interpolation.ease requires a numeric value for third argument (EaseOut amount).”)
local sum = i+o
return (p == 0 or p == 1 and p )
or (sum == 0 and p )
or (sum > 1
and (p < i and p^2/(2-i/sum-o/sum) )
or (p < 1-o and (2*p-i)/(2-i-o) )
or 1-(1-p)^2/(2-i-o)*o
)
end

– Using the linear method, gives the interpolation value of y1 to y2 at time m.
function linear(y1, y2, m)
return y1*(1-m)+y2*m
end

– Using the cosine method, gives the interpolation value of y1 to y2 at time m.
function cosine(y1, y2, m)
local m2 = (1-math_cos(mmath_pi))/2
return y1
(1-m2)+y2*m2
end

– Using the cubic method, gives the interpolation value of y1 to y2 at time m using y0 and y3 to provide smooth continuation of points.
function cubic(y0, y1, y2, y3, m)
local a0 = y3-y2-y0+y1
local a1 = y0-y1-a0
local a2 = y2-y0
return a0m^3+a1m^2+a2*m+y1
end

– Using the hermite method, gives the interpolation value of y1 to y2 at time m using y0 and y3 to provide smooth continuation of points
– with the tension controlled by t and the bias controlled by b.
function hermite(y0, y1, y2, y3, m, o, t)
local m2 = m^2
local m3 = m^3
return (2m3-3m2+1)y1+(m3-2m2+m)((y1-y0)(1+o)(1-t)/2+(y2-y1)(1-o)(1-t)/2)+(m3-m2)((y2-y1)(1+o)(1-t)/2+(y3-y2)(1-o)(1-t)/2)+(-2m^3+3m^2)*y2
end

– Using the linear function above, calculates the interpolated value at index (time) m using values (points) from the table t.
– The numeric equivilent of a key is obtained by a call to key_converter(table, key) which defaults to (a version of) tonumber.
– The numeric equivilent of a value is obtained by a call to value_converter(table, key) which defaults to (a version of) tonumber.
– A non-number return by key_converter will cause the index to be ignored and a non-numeric return by value_converter will cause the value
– to default to 0.
– The table is iterated via using loop_function(table) which defaults to pairs. See the Lua Manual for documentation on how to create an
– iterator function.
– NOTE: This recomputes internal data every time it is executed. For tables that are frequently interpolated, use the create function and
– index the resulting table according to associated documentation.
function t_linear(t, m, value_converter, key_converter, loop_function)
local a, av, ad, b, bv, bd, key, value, diff
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-m
if diff <= 0 and ((not ad) or ad < diff) then
a = k
av = key
elseif diff >= 0 and ((not bd) or bd > diff) then
bd = diff
b = k
bv = key
end
end
end
if b and a then
if av == bv then av = av-0.0001 end
return linear(value_converter and value_converter(t, a) or tonumber(t[a]), value_converter and value_converter(t, b) or tonumber(t**), (m-av)/(bv-av))
elseif a and not b then
b = a
bv = av
a, ad, av = nil, nil, nil
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-bv
if diff <= 0 and ((not ad) or ad < diff) then
a = k
av = key
end
end
end
elseif b and not a then
a = b
av = bv
b, bd, bv = nil, nil, nil
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-m
if diff >= 0 and ((not bd) or bd > diff) then
bd = diff
b = k
bv = key
end
end
end
end
if not a or not b then
error(“Table interpolation function given table without enough numeric values.”)
end
if av == bv then av = av-0.0001 end
return linear(value_converter and value_converter(t, a) or tonumber(t[a]), value_converter and value_converter(t, b) or tonumber(t**), (m-av)/(bv-av))
end

– Using the cosine function above, calculates the interpolated value at index (time) m using values (points) from the table t.
– The numeric equivilent of a key is obtained by a call to key_converter(table, key) which defaults to (a version of) tonumber.
– The numeric equivilent of a value is obtained by a call to value_converter(table, key) which defaults to (a version of) tonumber.
– A non-number return by key_converter will cause the index to be ignored and a non-numeric return by value_converter will cause the value
– to default to 0.
– The table is iterated via using loop_function(table) which defaults to pairs. See the Lua Manual for documentation on how to create an
– iterator function.
– NOTE: This recomputes internal data every time it is executed. For tables that are frequently interpolated, use the create function and
– index the resulting table according to associated documentation.
function t_cosine(t, m, value_converter, key_converter, loop_function)
local a, av, ad, b, bv, bd, key, value, diff
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-m
if diff <= 0 and ((not ad) or ad < diff) then
a = k
av = key
elseif diff >= 0 and ((not bd) or bd > diff) then
bd = diff
b = k
bv = key
end
end
end
if b and a then
if av == bv then av = av-0.0001 end
return cosine(value_converter and value_converter(t, a) or tonumber(t[a]), value_converter and value_converter(t, b) or tonumber(t**), (m-av)/(bv-av))
elseif a and not b then
b = a
bv = av
a, ad, av = nil, nil, nil
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-bv
if diff <= 0 and ((not ad) or ad < diff) then
a = k
av = key
end
end
end
elseif b and not a then
a = b
av = bv
b, bd, bv = nil, nil, nil
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-m
if diff >= 0 and ((not bd) or bd > diff) then
bd = diff
b = k
bv = key
end
end
end
end
if not a or not b then
error(“Table interpolation function given table without enough numeric values.”)
end
if av == bv then av = av-0.0001 end
return cosine(value_converter and value_converter(t, a) or tonumber(t[a]), value_converter and value_converter(t, b) or tonumber(t**), (m-av)/(bv-av))
end

– Using the cubic function above, calculates the interpolated value at index (time) m using values (points) from the table t.
– The numeric equivilent of a key is obtained by a call to key_converter(table, key) which defaults to (a version of) tonumber.
– The numeric equivilent of a value is obtained by a call to value_converter(table, key) which defaults to (a version of) tonumber.
– A non-number return by key_converter will cause the index to be ignored and a non-numeric return by value_converter will cause the value
– to default to 0.
– The table is iterated via using loop_function(table) which defaults to pairs. See the Lua Manual for documentation on how to create an
– iterator function.
– NOTE: This recomputes internal data every time it is executed. For tables that are frequently interpolated, use the create function and
– index the resulting table according to associated documentation.
function t_cubic(t, m, value_converter, key_converter, loop_function)
local a, av, ad, b, bv, bd, key, value, diff
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-m
if diff <= 0 and ((not ad) or ad < diff) then
a = k
av = key
elseif diff >= 0 and ((not bd) or bd > diff) then
bd = diff
b = k
bv = key
end
end
end
if b and a and a2 and b2 then
local a2, a2d, a2v, b2, b2d, b2v
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-av
if diff < 0 and ((not a2d) or a2d < diff) then
a2d = diff
a2 = k
a2v = key
end
end
end
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-bv
if diff > 0 and ((not b2d) or b2d > diff) then
b2d = diff
b2 = k
b2v = key
end
end
end
if not a2 then a2 = a a2v = av end
if not b2 then b2 = b b2v = bv end
if av == bv then av = av-0.0001 end
return cubic(
value_converter and value_converter(t, a2) or tonumber(t[a2]),
value_converter and value_converter(t, a) or tonumber(t[a]),
value_converter and value_converter(t, b) or tonumber(t**),
value_converter and value_converter(t, b2) or tonumber(t[b2]),
(m-av)/(bv-av)
)
elseif a and not b then
b = a
bv = av
a, ad, av = nil, nil, nil
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-bv
if diff <= 0 and ((not ad) or ad < diff) then
a = k
av = key
end
end
end
elseif b and not a then
a = b
av = bv
b, bd, bv = nil, nil, nil
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-av
if diff >= 0 and ((not bd) or bd > diff) then
bd = diff
b = k
bv = key
end
end
end
end
if not a or not b then
error(“Table interpolation function given table without enough numeric values.”)
end
local a2, a2d, a2v, b2, b2d, b2v
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-av
if diff < 0 and ((not a2d) or a2d < diff) then
a2d = diff
a2 = k
a2v = key
end
end
end
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-bv
if diff > 0 and ((not b2d) or b2d > diff) then
b2d = diff
b2 = k
b2v = key
end
end
end
if not a2 then a2 = a a2v = av end
if not b2 then b2 = b b2v = bv end
if av == bv then av = av-0.0001 end
return cubic(
value_converter and value_converter(t, a2) or tonumber(t[a2]),
value_converter and value_converter(t, a) or tonumber(t[a]),
value_converter and value_converter(t, b) or tonumber(t**),
value_converter and value_converter(t, b2) or tonumber(t[b2]),
(m-av)/(bv-av)
)
end

– Using the hermite function above, calculates the interpolated value at index (time) m using values (points) from the table t, using b as
– the bias and o as the tension. Defaults are 0 for bias and 0 for tension.
– The numeric equivilent of a key is obtained by a call to key_converter(table, key) which defaults to (a version of) tonumber.
– The numeric equivilent of a value is obtained by a call to value_converter(table, key) which defaults to (a version of) tonumber.
– A non-number return by key_converter will cause the index to be ignored and a non-numeric return by value_converter will cause the value
– to default to 0.
– The table is iterated via using loop_function(table) which defaults to pairs. See the Lua Manual for documentation on how to create an
– iterator function.
– NOTE: This recomputes internal data every time it is executed. For tables that are frequently interpolated, use the create function and
– index the resulting table according to associated documentation.
function t_hermite(t, m, value_converter, key_converter, loop_function, o, te)
local a, av, ad, b, bv, bd, key, value, diff
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-m
if diff <= 0 and ((not ad) or ad < diff) then
a = k
av = key
elseif diff >= 0 and ((not bd) or bd > diff) then
bd = diff
b = k
bv = key
end
end
end
if b and a and a2 and b2 then
local a2, a2d, a2v, b2, b2d, b2v
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-av
if diff < 0 and ((not a2d) or a2d < diff) then
a2d = diff
a2 = k
a2v = key
end
end
end
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-bv
if diff > 0 and ((not b2d) or b2d > diff) then
b2d = diff
b2 = k
b2v = key
end
end
end
if not a2 then a2 = a a2v = av end
if not b2 then b2 = b b2v = bv end
if av == bv then av = av-0.0001 end
return hermite(
value_converter and value_converter(t, a2) or tonumber(t[a2]),
value_converter and value_converter(t, a) or tonumber(t[a]),
value_converter and value_converter(t, b) or tonumber(t**),
value_converter and value_converter(t, b2) or tonumber(t[b2]),
(m-av)/(bv-av),
tonumber(o) or 0,
tonumber(te) or 0
)
elseif a and not b then
b = a
bv = av
a, ad, av = nil, nil, nil
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-bv
if diff <= 0 and ((not ad) or ad < diff) then
a = k
av = key
end
end
end
elseif b and not a then
a = b
av = bv
b, bd, bv = nil, nil, nil
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-av
if diff >= 0 and ((not bd) or bd > diff) then
bd = diff
b = k
bv = key
end
end
end
end
if not a or not b then
error(“Table interpolation function given table without enough numeric values.”)
end
local a2, a2d, a2v, b2, b2d, b2v
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-av
if diff < 0 and ((not a2d) or a2d < diff) then
a2d = diff
a2 = k
a2v = key
end
end
end
for k,v in (loop_function or pairs)(t) do
key = key_converter and key_converter(t, k) or tonumber(k)
if type(key) == “number” then
diff = key-bv
if diff > 0 and ((not b2d) or b2d > diff) then
b2d = diff
b2 = k
b2v = key
end
end
end
if not a2 then a2 = a a2v = av end
if not b2 then b2 = b b2v = bv end
if av == bv then av = av-0.0001 end
return hermite(
value_converter and value_converter(t, a2) or tonumber(t[a2]),
value_converter and value_converter(t, a) or tonumber(t[a]),
value_converter and value_converter(t, b) or tonumber(t**),
value_converter and value_converter(t, b2) or tonumber(t[b2]),
(m-av)/(bv-av),
tonumber(o) or 0,
tonumber(te) or 0
)
end
[/lua]

woah, thats bad ass haha thanks man!

thanks for the tip, but i ended up writing my own script. lol it works for any function and integrals for area! lol

heres the code
[lua]x = {}
x.showgraph = true
x.midx = ScrW()/2
x.midy = ScrH()/2

function x.equation( n )
return 0.001*n^2
end
x.dy = nil

function x.DrawGraph()
if !x.showgraph then return end
surface.SetDrawColor( 255, 255, 255, 255 )
surface.DrawLine( x.midx - 500, x.midy, x.midx + 500, x.midy )
surface.DrawLine( x.midx, x.midy - 500, x.midx, x.midy + 500 )

``````for i = -500, 500 do
x.dy = x.equation(i) - x.equation(i - 1)
surface.SetDrawColor( 0, 255, 0, 255 )
surface.DrawLine( x.midx + i, x.midy - x.equation(i), x.midx + i + 1, x.midy - x.equation(i) + x.dy )
end
``````

end
concommand.Add( “x_test”, function() x.showgraph = !x.showgraph end )[/lua]

im gonna add more features so i can graph out a health bar or something cool lol

Something to try later on is letting it parse the equation automatically. e.g.:

[lua]local f = Equation( “y=x^2+5” )
print( f( 3 ) )

> 14[/lua]

is that new? lol havnt been on gmod for over a year now haha

No, I’m just saying you could make something like that.

No, he suggested you make such a function:wink:

[editline]01:58PM[/editline]

:ninja:'d

oh i think i just did make that function haha

i think this is pretty cool cause i can graph anything i want and use it as a speedometer or something lol

-snip- I’m incredibly dumb

Yeah.

And PCCAMP I thought you rage quit GMod, because it was “boring” and you were never going to come back or some shit like that.

-snip- automerge fail.

^ true 3 day fail.

I’d like to see updates on this though…