Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function newton(func,deriv,init,err)
- local err,diff = (tonumber(err) or 0.01)
- repeat
- local nullstelle = init
- init = init - func(init)/deriv(init)
- diff = math.abs(init-nullstelle)
- until diff <= err
- return init
- end
- local function atanh(val,iter)
- local sum = 0
- for i=1,tonumber(iter) or 7,2 do
- sum = sum + val^i/i
- end
- return sum
- end
- local cat = {}
- function cat.fromLength(L,x1,y1,x2,y2)
- L,x1,y1,x2,y2 = tonumber(L) or 1,tonumber(x1) or 1,tonumber(y1) or 1,tonumber(x2) or 0, tonumber(y2) or 0
- if L < math.sqrt( (x1-x2)^2 + (y1-y2)^2) then error("Length not long enough to span the inputs") end
- local r = math.sqrt(L^2 - (y2-y1)^2)/math.abs(x2-x1)
- local A0
- if r<3 then
- A0 = math.sqrt(6*(r-1))
- else
- A0 = math.log(2*r) + math.log(math.log(2*r))
- end
- local A = newton(function(x) return math.sinh(x^2)/x^2 - r end,function(x) return ( 2*x*math.cosh(x^2) * x^2 - 2*x*math.sinh(x^2) )/(x^4) end,A0)^2
- local a = math.abs(x2-x1)/(2*A)
- local b = (x1+x2)/2 - a*atanh(math.abs(y2-y1)/L,13)
- local c = (y1+y2)/2 - L/(2*math.tanh(A))
- return function(x) return a*math.cosh((x-b)/a) + c end, (a .. "*cosh((x-"..b..")/"..a..")+"..c):gsub("%-%-","+"):gsub("%+%-","-")
- end
- function cat.fromMultiplier(lenScale,x1,y1,x2,y2)
- lenScale,x1,y1,x2,y2 = tonumber(lenScale) or 1,tonumber(x1) or 1,tonumber(y1) or 1,tonumber(x2) or 0, tonumber(y2) or 0
- return cat.fromLength( math.max(lenScale,1)*math.sqrt( (x2-x1)^2 + (y2-y1)^2),x1,y1,x2,y2)
- end
- return cat
Advertisement
Add Comment
Please, Sign In to add comment