t8rt0t

catenaryLib.lua

Jun 25th, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. local function newton(func,deriv,init,err)
  2. local err,diff = (tonumber(err) or 0.01)
  3. repeat
  4. local nullstelle = init
  5. init = init - func(init)/deriv(init)
  6. diff = math.abs(init-nullstelle)
  7. until diff <= err
  8. return init
  9. end
  10. local function atanh(val,iter)
  11. local sum = 0
  12. for i=1,tonumber(iter) or 7,2 do
  13. sum = sum + val^i/i
  14. end
  15. return sum
  16. end
  17. local cat = {}
  18. function cat.fromLength(L,x1,y1,x2,y2)
  19. 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
  20. if L < math.sqrt( (x1-x2)^2 + (y1-y2)^2) then error("Length not long enough to span the inputs") end
  21. local r = math.sqrt(L^2 - (y2-y1)^2)/math.abs(x2-x1)
  22. local A0
  23. if r<3 then
  24. A0 = math.sqrt(6*(r-1))
  25. else
  26. A0 = math.log(2*r) + math.log(math.log(2*r))
  27. end
  28. 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
  29. local a = math.abs(x2-x1)/(2*A)
  30. local b = (x1+x2)/2 - a*atanh(math.abs(y2-y1)/L,13)
  31. local c = (y1+y2)/2 - L/(2*math.tanh(A))
  32. return function(x) return a*math.cosh((x-b)/a) + c end, (a .. "*cosh((x-"..b..")/"..a..")+"..c):gsub("%-%-","+"):gsub("%+%-","-")
  33. end
  34. function cat.fromMultiplier(lenScale,x1,y1,x2,y2)
  35. 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
  36. return cat.fromLength( math.max(lenScale,1)*math.sqrt( (x2-x1)^2 + (y2-y1)^2),x1,y1,x2,y2)
  37. end
  38. return cat
Advertisement
Add Comment
Please, Sign In to add comment