diff options
Diffstat (limited to 'main.lua')
-rw-r--r-- | main.lua | 77 |
1 files changed, 45 insertions, 32 deletions
@@ -1,26 +1,5 @@ local G = love.graphics -local function plot(f,x0,xn) - local n = 30 - x0 = x0 or G.inverseTransformPoint(0,0) - xn = xn or G.inverseTransformPoint(G.getDimensions(),0) - - if xn < x0 then - xn,x0 = x0,xn - end - - local h = (xn-x0)/n - local ps = {} - for i=0,n do - local x = x0 + i*h - local y = f(x) - table.insert(ps,x) - table.insert(ps,y) - end - G.line(ps) -end - - local function atanh(x) return (1/2) * math.log((1+x)/(1-x)) end @@ -38,12 +17,27 @@ local function solve_for_A(r) return A end -local L = 3 -function love.wheelmoved(x,y) - L = L + (y/5) +local function plot(f,x0,xn) + local n = 30 + x0 = x0 or G.inverseTransformPoint(0,0) + xn = xn or G.inverseTransformPoint(G.getDimensions(),0) + + if xn < x0 then + xn,x0 = x0,xn + end + + local h = (xn-x0)/n + local ps = {} + for i=0,n do + local x = x0 + i*h + local y = f(x) + table.insert(ps,x) + table.insert(ps,y) + end + return ps end -local function catenary0(x1,y1,x2,y2) +local function catenary0(x1,y1,x2,y2, L) -- https://math.stackexchange.com/a/3557768 y1 = -y1 y2 = -y2 @@ -53,7 +47,7 @@ local function catenary0(x1,y1,x2,y2) local my = (y1+y2)/2 local D = math.sqrt(dx^2+dy^2) - -- local L = D+(1/D) + L = L or D+(1/(D+0.3)) local r = math.sqrt(L^2 - dy^2)/dx local A = solve_for_A(r) @@ -64,14 +58,27 @@ local function catenary0(x1,y1,x2,y2) return function(x) return -(a * math.cosh((x-b)/a) + c) end end -local function catenary(x1,y1,x2,y2) + + +local function catenary(x1,y1, x2,y2, L) + local D = math.sqrt((x2-x1)^2 + (y2-y1)^2) + L = L or D+(1/(D+0.3)) + if x2 < x1 then x1,x2 = x2,x1 y1,y2 = y2,y1 end - return catenary0(x1,y1,x2,y2) + + local f = catenary0(x1,y1,x2,y2,L) + return plot(f, x1,x2) end +return { + catenary = catenary +} + + +--[[ local Ps = {{-1,1}, {1,1}} function love.update(dt) @@ -88,10 +95,15 @@ function love.draw() G.clear(1,1,1) G.setColor(0,0,0) G.origin() - G.print(L,10,10) + local dx = Ps[1][1] - Ps[2][1] + local dy = Ps[1][2] - Ps[2][2] + local D = math.sqrt(dx^2+dy^2) + local L = D+(1/(D+0.3)) + -- local L = D+1 + G.print(("%.2f\n%.2f\n%.2f"):format(L,D,L-D),10,10) G.setLineWidth(0.01) G.translate(W/2,H/2) - G.scale(150) + G.scale(50) G.setColor(0.8,0.8,0.8) for i=-10,10 do @@ -108,11 +120,12 @@ function love.draw() -- plot(atanh) G.setColor(0,0,0) - plot(catenary(Ps[1][1],Ps[1][2], Ps[2][1], Ps[2][2]), - Ps[1][1], Ps[2][1]) + G.line(catenary(Ps[1][1],Ps[1][2], Ps[2][1], Ps[2][2])) G.setColor(1,0,0) G.circle('fill',Ps[1][1],Ps[1][2],0.05) G.setColor(0,0,1) G.circle('fill',Ps[2][1],Ps[2][2],0.05) end +--]] + |