-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
262 lines (176 loc) · 4.94 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
local F = ' x*sin(y/2) '
local G = ' y*cos(x/2) '
local f,g
--[[
Let f,g be scalar functions such that:
( a, b ) = P(x,y)
( a, b ) = (f(x,y) , g(x,y))
such that P(x,y) gives a vector field of vectors (a,b)
across the (x,y) plane
Now using finite differences for automatic differentiation
]]
setmetatable(_G,{__index=math})
local function makeFn(st)
return loadstring([[
u = function(x,y)
return ]] .. st .. [[
end
]])
end
local function df_dx(x,y) return (f(x+0.001,y) - f(x-0.001,y))/0.002 end
local function df_dy(x,y) return (f(x,y+0.001) - f(x,y-0.001))/0.002 end
local function dg_dx(x,y) return (g(x+0.001,y) - g(x-0.001,y))/0.002 end
local function dg_dy(x,y) return (g(x,y+0.001) - g(x,y-0.001))/0.002 end
local function divergence(x,y)
return df_dx(x,y) + dg_dy(x,y)
end
local function curl(x,y)
return dg_dx(x,y) - df_dy(x,y)
end
local function P(x,y)
return f(x,y), g(x,y)
end
local function magnitude(x, y)
local x,y = P(x,y)
return (x^2 + y^2)^0.5
end
local x = -4
local y = -4
local w = 8
local h = 8
local SAMPLES = 80; -- there will be SAMPLES ^ 2 samples made.
-- DONT MAKE THIS NUMBER LARGE!!!
local CAP_Y = 750
local CAP_X = 1000
love.window.setMode(CAP_X,CAP_Y) -- (CAP,CAP)
local CLAMP_VAL = 100
local max = math.max
local min = math.min
local function clamp(n)
return min(max(-CLAMP_VAL, n), CLAMP_VAL)
end
local avg_mag
local avg_curl
local avg_div
local var_mag
local var_curl
local var_div
local n
local function load()
local c = 0 -- average sums
local d = 0
local m = 0
local ct = 0
for X=x, x+w, w/SAMPLES do
for Y=y, y+h, h/SAMPLES do
c = c + clamp(curl(X, Y))
d = d + clamp(divergence(X, Y))
m = m + clamp(magnitude(X, Y))
ct = ct + 1
end
end
avg_mag = m / ct
avg_curl = c / ct
avg_div = d / ct
local vc = 0 -- variances * (n-1)
local vd = 0
local vm = 0
for X=x, x+w, w/SAMPLES do
for Y=y, y+h, h/SAMPLES do
vc = vc + (clamp(curl(X, Y)) - avg_curl)^2
vd = vd + (clamp(divergence(X, Y)) - avg_div)^2
vm = vm + (clamp(magnitude(X, Y)) - avg_mag)^2
end
end
var_curl = (vc / (ct-1))^0.5
var_mag = (vm / (ct-1))^0.5 -- originally ^0.75 (idk y, ^0.75 looked cooler)
var_div = (vd / (ct-1))^0.5
n=ct
end
--[[
git branch -m main master
git fetch origin
git branch -u origin/master master
git remote set-head origin -a
]]
local function changeFunction( F, G )
F=F:gsub(" ",""):gsub("f(x,y)",""):gsub("=","")
G=G:gsub(" ",""):gsub("f(x,y)",""):gsub("=","")
makeFn(F)()
f=u
makeFn(G)()
g=u
-- create custom shader string from template
local autoeffect = io.open("template.glsl","r")
assert(autoeffect,"eh?")
local autostr = autoeffect:read("*all")
autostr=autostr:gsub("%@F", F):gsub("%@G", G)
autoeffect:close()
-- push custom shader code into temp GLSL file
local tmp = io.open("_TEMP.glsl", "w+")
tmp:write(autostr)
tmp:close()
if sh then sh:release() end
sh = love.graphics.newShader("_TEMP.glsl")
love.graphics.setShader(sh)
load()
end
changeFunction(F,G)
local function toPlotCoords(X,Y)
X=CAP_X-X
Y=CAP_Y-Y
return (w*X)/CAP_X + x, (h*Y)/CAP_Y + y
end
load()
local theta = 0
local mega = 0
local nodraw = false
function love.draw()
love.graphics.setShader(sh)
love.graphics.setColor(1,1,1,1)
love.graphics.rectangle("fill",-200,-200,CAP_X*2,CAP_Y*2)
if nodraw then return end
love.graphics.setColor(0,0,0,1)
for x=1,CAP_X,CAP_X/50 do
for y=1,CAP_Y,CAP_Y/50 do
local px, py = toPlotCoords(x, y)
love.graphics.line(x,y, x + 5*f(px,py), y + 5*g(px,py))
end
end
love.graphics.setShader( )
love.graphics.setColor(1,1,1)
love.graphics.rectangle("fill",8,10,220,50)
love.graphics.setColor(0,0,0)
love.graphics.print(("F(x,y) = (x)*sin(y/2 + %f)"):format(theta),10,26)
love.graphics.print(("G(x,y) = (y)*cos(x/2 + %f)"):format(mega), 10,10)
love.graphics.print("(x,y) --> ( F(x,y), G(x,y) )",10,42)
end
function love.keypressed(k)
if k=="d" then
nodraw = not nodraw
end
end
function love.update(dt)
local A = ("(x)*sin(y/2 + %f)"):format(theta)
local B = ("(y)*cos(x/2 + %f)"):format(mega)
changeFunction(
A,
B
)
local change = 50*((2*math.pi)/(9.5*60))*dt
theta = (theta + change) % (2*math.pi)
mega = (mega + change) % (2*math.pi)
sh:send("dscale", max(1,var_div))
sh:send("doffset", avg_div)
sh:send("cscale", max(1,var_curl))
sh:send("coffset", avg_curl)
sh:send("mscale", max(1,var_mag))
sh:send("moffset", avg_mag)
sh:send("sx", x)
sh:send("sy", y)
sh:send("w", w)
sh:send("h", h)
sh:send("nodraw", nodraw)
sh:send("CAP_X",CAP_X)
sh:send("CAP_Y",CAP_Y)
end