-
Notifications
You must be signed in to change notification settings - Fork 2
/
equationgrapher.lua
80 lines (67 loc) · 2.29 KB
/
equationgrapher.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
-- mod-version:3
local core = require "core"
local command = require "core.command"
local common = require "core.common"
local config = require "core.config"
local View = require "core.view"
local style = require "core.style"
config.plugins.equationgrapher = {
point_size = 3,
steps = 10000,
background_color = style.background,
cross_color = style.text,
graph_color = style.caret,
font = style.big_font
}
local GraphView = View:extend()
function GraphView:new(equation)
GraphView.super.new(self)
if equation == "" then error("No equation inputted.") end
self.name = equation
local eq = assert(load("return function(x) return "..equation.." end"))()
if type(eq(0)) ~= "number" then error("Why is your equation returning a "..type(eq(0)).."?!") end
self.equation = eq
self.range = 1000
end
function GraphView:get_name()
return "Graph: y = "..self.name
end
function GraphView:update()
GraphView.super.update(self)
end
function GraphView:draw()
self:draw_background(config.plugins.equationgrapher.background_color)
local conf = config.plugins.equationgrapher
local xPos, yPos = self.position.x, self.position.y
local xSize, ySize = self.size.x, self.size.y
local pointSize = conf.point_size
-- draw cross
local cross_color = conf.cross_color
local font = conf.font
local y = ySize/2-font:get_height()*0.2
renderer.draw_rect(xPos+xSize/2, yPos, pointSize, ySize, cross_color)
renderer.draw_rect(xPos, yPos+ySize/2, xSize, pointSize, cross_color)
renderer.draw_text(font, tostring(-self.range), xPos, y, cross_color)
renderer.draw_text(font, tostring(self.range), xPos+(xSize-(font:get_width(self.range))), y, cross_color)
renderer.draw_text(font, "0", xPos+xSize/2+font:get_width("0")*0.2, y, cross_color)
--draw equation
for t=0, 1, 1/math.max(500, conf.steps) do
renderer.draw_rect(
(xPos+xSize/2)+common.lerp(-xSize/2, xSize/2, t),
(yPos+ySize/2)-self.equation(common.lerp(-self.range, self.range, t)),
pointSize, pointSize, conf.graph_color
)
end
end
function GraphView:on_mouse_wheel(d)
self.range = self.range + d*-10
end
command.add(nil, {
["equation-grapher:graph-equation"] = function()
core.command_view:enter("Equation to graph", { submit = function(e)
local node = core.root_view:get_active_node()
node:add_view(GraphView(e))
end })
end,
})
return GraphView